zz to center the cursor vertically on your screen. Useful when you 250Gzz, for instance. % CTRL-w | and CTRL-W _ maximize your current split vertically and horizontally, respectively. CTRL-W = equalizes 'em. % %s/^\n// to delete all empty lines % :g/_pattern_/s/^/#/g will comment out lines containing _pattern_ (if '#' is your comment character/sequence) % vim -c [command] will launch vim and run a : command at launch, e.g. "vim -c NERDTree." % CTRL-w s CTRL-w T will open current buffer in new tab % CTRL-w K will switch vertical split into horizontal, CTRL-w H will switch a horizontal into a vertical. % :w !sudo tee % will use sudo to write the open file (have you ever forgotten to `sudo vim /path/to/file`?) % K runs a prgrm to lookup the keyword under the cursor. If writing C, it runs man. In Ruby, it (should) run ri, Python (perhaps) pydoc. % Edit and encrypt a file: vim -x filename % :%s//joe/igc substitute your last search with joe. % /fred\_s*joe/ will search for fred AND joe with whitespace (including newline) in between. % From a command-line, vim scp://username@host//path/to/file to edit a remote file locally. % /fred|joe/ will search for either fred OR joe. % /.*fred&.*joe/ will search for fred AND joe, in any order. % // will search for fred, but not alfred or frederick. % /joe/e will search for joe and place the cursor at the end of the match. % /joe/e+1 will search for joe and place the cursor at the end of the match, plus on character. % /joe/s-2 will search for joe and place the cursor at the start of the match, minus two characters. % /joe/+3 will search for joe and place the cursor three lines below the match. % /joe.*fred.*bill/ will search for joe AND fred AND bill, in that order. % /begin\_.*end will search for begin AND end over multiple lines. % :s/\(.*\):\(.*\)/\2 : \1/ will reverse fields separated by ':' % %s/a/bar/g will place the contents of register 'a' in the search, and replace it with 'bar'. % Edit a command output in Vim as a file: $ command | vim - % ggVG= will auto-format the entire document % '0 opens the last modified file ('1 '2 '3 works too) % [I (big i) shows lines containing the word under the cursor % In insert mode do Ctrl+r=53+17. This way you can do some calcs with vim. % "_d will delete the selection without adding it to the yanked stack (sending it to the black hole register). % Basic commands 'f' and 't' (like first and 'til) are very powerful. See :help t or :help f. % :40,50m30 will move lines 40 through 50 to line 30. Most visual mode commands can be used with line numbers. % To search for a URL without backslashing, search backwards! Example: ?http://somestuff.com % :%s/~/sue/igc substitute your last replacement string with 'sue'. % g; will cycle through your recent changes (or g, to go in reverse). % :%s/^\n\{3}// will delete a block of 3 empty lines. % :%s/^\n\+/\r/ will compress multiple empty lines into one. % :%s#<[^>]\+>##g will remove HTML tags, leaving the text. (non-greedy) % :%s#.*(hours).*#1# will delete everything on a line except for the string 'hours'. % "2p will put the second to last thing yanked, "3p will put the third to last, etc. % :wa will save all buffers. :xa will save all buffers and exit Vim. % After performing an undo, you can navigate through the changes using g- and g+. Also, try :undolist to list the changes. % You probably know that 'u' is undo. Do you know that Ctrl-R is redo? % ci{ – change text inside {} block. Also see di{, yi{, ci( etc. % :set autochdir instead of :lcd %:p:h in your vimrc to change directories upon opening a file. % :read [filename] will insert the contents of [filename] at the current cursor position % to use gvim to edit your git messages set git's core editor as follows: git config --global core.editor "gvim --nofork" % ci " inside a " " will erase everything between "" and place you in insertion mode. % :set guifont=* in gvim or MacVim to get a font selection dialog. Useful while giving presentations. % :h slash to get a list of all help topics containing the word 'slash'. % guu converts entire line to lowercase. gUU converts entire line to uppercase. ~ inverts case of current character. % '. jumps to last modified line. `. jumps to exact position of last modification. % : trace your movements backwards in a file. trace your movements forwards in a file. % :ju(mps) : list your movements {{help|jump-motions}} % :history lists the history of your recent commands, sans duplicates. % "+y to copy to the X11 (or Windows) clipboard. "+p to paste from it. % noremap ' ` and noremap ` ' to make marks easier to navigate. Now ` is easier to reach! % 2f/ would find the second occurrence of '/' in a line. % :tab sball will re-tab all files in the buffers list. % :%s/joe|fred/jerks/g will replace both 'fred' and 'joe' with 'jerks'. % * # g* g# each searches for the word under the cursor (forwards/backwards) % :vimgrep pattern **/*.txt will search all *.txt files in the current directory and its subdirectories for the pattern. % == will auto-indent the current line. Select text in visual mode, then = to auto-indent the selected lines. % Count the number of occurences of a word in a file with :%s///gn % :set foldmethod=syntax to make editing long files of code much easier. zo to open a fold. zc to close it. See more % Need to edit and run a previous command? q: then find the command, edit it, and Enter to execute the line. % @: to repeat the last executed command. % :e $MYVIMRC to directly edit your vimrc. :source $MYVIMRC to reload. Mappings may make it even easier. % g to see technical information about the file, such as how many words are in it, or how many bytes it is. % gq{movement} to wrap text, or just gq while in visual mode. gqap will format the current paragraph. % :E to see a simple file explorer. (:Ex will too, if that's easier to remember.) % :vimgrep pattern *.txt will search all .txt files in the current directory for the pattern. % :match ErrorMsg '\%>80v.\+' uses matching to highlight lines longer than 80 columns. % :%s/\ r//g to remove all those nasty ^M from a file, or :%s/\ r$//g for only at the end of a line. % % matches opening and closing chars (){}[], and with matchit.vim, def/end, HTML tags, etc. as well! % offers word-completion while in insert mode. % offers line completion in insert mode. % / will pull the word under the cursor into search. % gf will open the file under the cursor. (Killer feature.) % Ctrl-a, Ctrl-x will increment and decrement, respectively, the number under the cursor. % :scriptnames will list all plugins and _vimrcs loaded. % :tabdo [some command] will execute the command in all tabs. Also see windo, bufdo, argdo. % :vsplit filename will split the window vertically and open the file in the left-hand pane. Great when writing unit tests! % qa starts a recording in register 'a'. q stops it. @a repeats the recording. 5@a repeats it 5 times. % :%s/\v(.*\n){5}/&\r will insert a blank line every 5 lines % Ctrl-c to quickly get out of command-line mode. (Faster than hitting ESC a couple times.) % Use '\v' in your regex to set the mode to 'very magic', and avoid confusion. (:h \v for more info.) % ; is a motion to repeat last find with f. f' would find next quote. c; would change up to the next ' % /\%>80v.\+ with search highlighting (:set hlsearch) will highlight any text after column 80. % ga will display the ASCII, hex, and octal value of the character under the cursor. % :%s/[.!?]\_s\+\a/\U&\E/g will uppercase the first letter of each sentence (except the very first one). % :r !date will insert the current date/time stamp (from the 'date' command -- a bit OS-specific). % ggguG will lower case the entire file (also known as the Jerry Yang treatment). % :lcd %:p:h will change to the directory of the current file. % :Sex will open a file explorer in a split window. I bet you'll remember that one. % % matches brackets {} [] (), and with matchit.vim, also matches def/end, < ?php/?>, < p>/< /p>, etc. % :g/search_term/# display each line containing 'search_term' with line numbers. % :%s/// will delete HTML comments, potentially spanning multiple lines.