Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Forked from snuggs/.vimrc
Last active December 11, 2015 23:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netpoetica/4680039 to your computer and use it in GitHub Desktop.
Save netpoetica/4680039 to your computer and use it in GitHub Desktop.
" make Vim more useful
set nocompatible
" vundle
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" VUNDLES
Bundle 'gmarik/vundle'
Bundle 'vim-multiple-cursors'
" Bundle 'YouCompleteMe'
Bundle 'marijnh/tern_for_vim'
" Bundle 'syntastic'
Bundle 'rstacruz/sparkup', { 'rtp': 'vim/' }
Bundle 'Raimondi/delimitMate'
Bundle 'cakebaker/scss-syntax.vim'
" YouCompleteMe options
" let g:ycm_global_ycm_extra_conf = '/Users/krosenberg/.ycm_extra_conf.py'
filetype plugin indent on
" color me
colorscheme badwolf
au BufNewFile,BufRead * if &ft == '' | set ft=html | endif
au BufRead,BufNewFile *.md set filetype=markdown
au BufRead,BufNewFile *.go set filetype=go
set fileformats=unix
" Use the OS clipboard by default (on versions compiled with `+clipboard`)
set clipboard=unnamed
" Enhance command-line completion
set wildmenu
" Allow cursor keys in insert mode
set esckeys
" Allow backspace in insert mode
set backspace=indent,eol,start
" Optimize for fast terminal connections
set ttyfast
" Add the g flag to search/replace by default
set gdefault
" Use UTF-8 without BOM
set encoding=utf-8 nobomb
" Change mapleader
let mapleader=","
" Don’t add empty newlines at the end of files
set binary
set noeol
" Centralize backups, swapfiles and undo history
set backupdir=~/.vim/backups
set directory=~/.vim/swaps
if exists("&undodir")
set undodir=~/.vim/undo
endif
" Expand tabs to spaces
set shiftwidth=1
set expandtab
" Respect modeline in files
set modeline
set modelines=4
" Enable per-directory .vimrc files and disable unsafe commands in them
set exrc
set secure
" Enable line numbers
set number
" Enable syntax highlighting
syntax on
" Highlight current line
set cursorline
" Make tabs as wide as two spaces
set tabstop=2
" Show “invisible” characters
set lcs=tab:▸\ ,trail:·,eol:¬,nbsp:_
set list
" Highlight searches
set hlsearch
" Ignore case of searches
set ignorecase
" Highlight dynamically as pattern is typed
set incsearch
" Always show status line
set laststatus=2
" Enable mouse in all modes
set mouse=a
" Disable error bells
set noerrorbells
" Don’t reset cursor to start of line when moving around.
set nostartofline
" Show the cursor position
set ruler
" Don’t show the intro message when starting Vim
set shortmess=atI
" Show the current mode
set showmode
" Show the filename in the window titlebar
set title
" Show the (partial) command as it’s being typed
set showcmd
" Use relative line numbers
"if exists("&relativenumber")
" set relativenumber
" au BufReadPost * set relativenumber
" endif
" Start scrolling three lines before the horizontal window border
set scrolloff=3
" Strip trailing whitespace (,ss)
function! StripWhitespace()
let save_cursor = getpos(".")
let old_query = getreg('/')
:%s/\s\+$//e
call setpos('.', save_cursor)
call setreg('/', old_query)
endfunction
noremap <leader>ss :call StripWhitespace()<CR>
" Save a file as root (,W)
noremap <leader>W :w !sudo tee % > /dev/null<CR>
" Automatic commands
if has("autocmd")
" Enable file type detection
filetype on
" Treat .json files as .js
autocmd BufNewFile,BufRead *.json setfiletype json syntax=javascript
autocmd BufRead,BufNewFile *.css,*.scss,*.less setlocal foldmethod=marker foldmarker={,}
endif
Some common key bindings:
`za` - toggles
`zc` - closes
`zo` - opens
`zR` - open all
`zM` - close all
:NERDTree, :NERDTreeClose, and :NERDTreeToggle
map <F2> :NERDTreeToggle<CR> # maps :NERDTreeToggle to <F2> within .vimrc
All other keyboard shortcuts can be found by pressing ?. It will open a special help screen with the shortcut listings. Press ? again to get back to file tree.
* Use <C>+w to switch context between file and nerdtree
* Use the natural vim navigation keys hjkl to navigate the files.
* Press o to open the file in a new buffer or open/close directory.
* Press t to open the file in a new tab.
* Press i to open the file in a new horizontal split.
* Press s to open the file in a new vertical split.
* Press p to go to parent directory.
* Press r to refresh the current directory.
Plugins Using Pathogen
----------------------
NerdTree - http://www.vim.org/scripts/script.php?script_id=1658
Surround.vim - http://www.vim.org/scripts/script.php?script_id=1697
RagTag - https://github.com/tpope/vim-ragtag
zencoding - https://github.com/mattn/zencoding-vim
vim-javascript - https://github.com/pangloss/vim-javascript
Syntax Files
------------
css.vim - http://www.vim.org/scripts/script.php?script_id=2150
"+y[movement] " copy to clipboard buffer (including double quote)
" Similarly, to paste from clipboard, "+p
vim -p [file1 file2 file3 fileN] # vim opens file collection in tabs
vim -o [file1 file2 file3 fileN] # vim opens file collection in split buffers
* k – navigate upwards
* j – navigate downwards
* l – navigate right side
* h – navigate left side
go down by 10 lines, then type “10j”.
:n Jump to line number n. For example, to jump to line 42, you'd type :42
0 – go to the starting of the current line.
^ – go to the first non blank character of the line.
$ – go to the end of the current line.
g_ – go to the last non blank character of the line.
H – Go to the first line of current screen.
M – Go to the middle line of current screen.
L – Go to the last line of current screen.
* ctrl+f – Jump forward one full screen.
* ctrl+b – Jump backwards one full screen
* ctrl+d – Jump forward (down) a half screen
* ctrl+u – Jump back (up) one half screen
% – Go to the matching braces, or parenthesis inside code.
N% – Go to the Nth percentage line of the file.
NG – Go to the Nth line of the file.
G – Go to the end of the file.
” – Go to the position where you were in NORMAL MODE while last closing the file.
^ – Go to the position where you were in INSERT MODE while last closing the file.
g – Go to the beginning of the file.
e – go to the end of the current word.
E – go to the end of the current WORD.
b – go to the previous (before) word.
B – go to the previous (before) WORD.
w – go to the next word.
W – go to the next WORD.
zt - move current cursor placement to top
zb - move current cursor placement to bottom
zz - move current cursor placement to center screen
WORD – WORD consists of a sequence of non-blank characters, separated with white space.
word – word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 – single WORD
* 192.168.1.1 – seven words.
# Vim Paragraph Navigation
* { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
* } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.
Editing blocks of text
Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text.
Vim command Action
~ Change the case of characters. This works both in visual and command mode.
In visual mode, change the case of highlighted characters.
In command mode, change the case of the character uder cursor.
> (V) Shift right (indent).
< (V) Shift left (de-indent).
c (V) Change the highlighted text.
y (V) Yank the highlighted text. In Windows terms, "copy the selected text to clipboard."
yy or :y or Y Yank the current line. You don't need to highlight it first.
d (V) Delete the highlighted text. In Windows terms, "cut the selected text to clipboard."
x Delete characters under the cursor.
X Delete characters before the cursor.
dd or :d Delete the current line. Again, you don't need to highlight it first. Deleting text
p Put the text you yanked or deleted. In Windows terms, "paste the contents of the clipboard".
Put characters after the cursor. Put lines below the current line.
P Put characters before the cursor. Put lines above the current line.
u Undo the last action.
U Undo all the latest changes that were made to the current line.
Ctrl + r Redo.
v Start highlighting characters. Use the normal movement keys and commands
to select text for highlighting.
V Start highlighting lines.
:%! nl -ba # Line Numbers
Finding Files
---------------------
:pwd # print working directory
:cd # change directory
:find # file name (within path)
Search
---------------------
* /i – Search for a pattern which will you take you to the next occurrence of it.
* ?i – Search for a pattern which will you take you to the previous occurrence of it.
* * - Go to the next occurrence of the current word under the cursor.
* # - Go to the previous occurrence of the current word under the cursor.
re-apply - The . command repeats the last change. A change, in this context, is inserting, deleting or replacing text. Being able to repeat this is a very powerful mechanism. If you organise your editing around it, many changes will become a matter of hitting just that . key. Watch out for making other changes in between, because it will replace the change that you were repeating. Instead you might want to mark the location with the m command, continue your repeated change and come back there later.
registers - When you are typing a phrase or sentence multiple times, there is an even quicker approach. Vim has a mechanism to record a macro. You type qa to start recording into register 'a'. Then you type your commands as usual and finally hit q again to stop recording. When you want to repeat the recorded commands you type @a. There are 26 registers available for this.
:tabnew [filename] # and Vim will load the file in the new tab (or defaults to empty buffer)
:tabs # displays all tabs
:tabn and :tabp switch between tabs, or you can use gt while you're in normal mode.
If you have a lot of tabs open, you can use :tabfirst, or just :tabfir, to jump to the first tab, and :tablast to jump to the last tab that's open.
if you don't like the existing shortcuts for the tab commands, you can add your own. For instance, if you want to make it easy to open a new tab, you might insert this into your .vimrc:
- Rearranging tabs
If you're really meticulous and want to position tabs just so in Vim, you can move the tabs to a specific spot in the tab order using :tabm n , where n is the position number that you want to use. If you don't give the :tabm command an argument, then the current tab will be moved to the last spot.
Vim starts tab numbering from 0, so if you have six tabs open, you'll have tab 0 through tab 5. So, if you're in the first tab and want to move it to the fourth position, you'd run :tab 3.
map <leader>tn :tabnew %<cr>
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
:split [file] # Splits the window horizontally.
:vsplit [file] # Splits the window vertically.
<Ctrl>-w # (mnemonic: control window) To move between windows you use <Ctrl>-w (mnemonic: control window).
# To move in a specific direction, add the relevant movement key. So, to move upwards: <Ctrl>-w+k.
# To close the active window use :q, just as you would to close a window normally.
<Ctrl>-w [HJKL] # to actually move buffer (i.e. K will move current buffer to top. * notice caps*)
:only # if split buffers, close all buffers except current one.
You can reduce/enlarge the size of the current window with <Ctrl>-w+- and <Ctrl>-w++, respectively. To specify the size of a window when you open it, prefix the :split command with the desired height/width in lines. For example, to show README in a window of 5 lines high: :5 :split README.
When splitting a window you can prepend a modifier command to tell where the
window is to appear:
:leftabove {cmd} left or above the current window
:aboveleft {cmd} idem
:rightbelow {cmd} right or below the current window
:belowright {cmd} idem
:topleft {cmd} at the top or left of the Vim window
:botright {cmd} at the bottom or right of the Vim window
CTRL-W x
CTRL-W CTRL-X Without count: Exchange current window with next one. If there
is no next window, exchange with previous window.
With count: Exchange current window with Nth window (first
window is 1). The cursor is put in the other window.
Replace
Vim command Action
:rs/foo/bar/a Substitute foo with bar. r determines the range and a determines the arguments.
The range (r) can be
nothing Work on current line only.
number Work on the line whose number you give.
% The whole file.
Arguments (a) can be
g Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.
i Ignore case for the search pattern.
I Don't ignore case.
c Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution.
Examples
:452s/foo/bar/ Replace the first occurrence of the word foo with bar on line number 452.
:s/foo/bar/g Replace every occurrence of the word foo with bar on current line.
:%s/foo/bar/g Replace every occurrence of the word foo with bar in the whole file.
:%s/foo/bar/gi The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on.
:%s/foo/bar/gc Confirm every substitution.
:%s/foo/bar/c For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.
# Sessions
--------------------------
:mksession my_session.vim # creates session with current layout info
:source my_session.vim # restore session from file (or :~>vim -S my_session.vim)
BUFFERS
--------------------
:bn[ext] # Next buffer
:bp[revious] # Previous Buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment