Vimrc
Regex
&
& Is replaced with the entire text matched by the search pattern when used in a replacement string. This is useful when you want to avoid retyping text:
:%s/Yazstremski/&, Carl/
The replacement will say Yazstremski, Carl. The & can also replace a variable pattern (as specified by a regular expression). For example, to surround each line from 1 to 10 with parentheses, type:
:1,10s/.*/(&)/
from Learning the vi and Vim Editors, Seventh Edition by Arnold Robbins, Elbert Hannah, and Linda Lamb.
Insert a space between # and the character that follows it
:%s/#\(\w\)/# \1/g
general
.  any character except new line
\s whitespace character
\S non-whitespace character
\d digit
\D non-digit
\x hex digit
\X non-hex digit
\o octal digit
\O non-octal digit
\h head of word character (a,b,c...z,A,B,C...Z and _)
\H non-head of word character
\p printable character
\P like \p, but excluding digits
\w word character
\W non-word character
\a alphabetic character
\A non-alphabetic character
\l lowercase character
\L non-lowercase character
\u uppercase character
\U non-uppercase character
Quickfix
C-w<Enter> Open file from list
Vim cool fonts
Linux
$ mkdir -p ~/.local/share/fonts
$ cd ~/.local/share/fonts && curl -fLo "Droid Sans Mono for Powerline Nerd Font Complete.otf" https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/patched-fonts/DroidSansMono/complete/Droid%20Sans%20Mono%20for%20Powerline%20Nerd%20Font%20Complete.otf
- In vimrc, set
set guifont=Droid\ Sans\ Mono\ for\ Powerline\ Plus\ Nerd\ File\ Types\ 11
MacOS
cd ~/Library/Fonts && curl -fLo "Droid Sans Mono for Powerline Nerd Font Complete.otf" https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/patched-fonts/DroidSansMono/complete/Droid%20Sans%20Mono%20for%20Powerline%20Nerd%20Font%20Complete.otf
- In .vimrc,
set guifont=Droid\ Sans\ Mono\ for\ Powerline\ Plus\ Nerd\ File\ Types:h11
- Install vim-devicons plugin, after NERDTree and others
- Restart terminal
- Change font in terminal
Make vim automatically read file as some filetype
Insert this line in the file
# vim: set filetype=sh
Misc
:echo expand('%:p') print filename with full path
Vim diff
turn on
:windo diffthis
turn off
:windo diffoff
move between changes
[c jump back
]c  and forward
Movements
- ggGo to beginning of file
- GGo to end of file
Windows et al
- ^wChange subwindow
- :qCommand history
Search and replace
- +?|& must be escaped for special function
- \r is new line (for replacing)
normal mode
- /wordtosearch+ Enter Search for word
- /\cwortosearch+ Enter Case insensitive search for word
- :%s/foo/bar/gciReplaces all ocurrences (g) in all the text (%) of the word foo by the word bar, ignoring cases (i) and asking confirmation (c) for each replacement
- :%s/patata//gncounts ocurrences of patata in all the text
Buffers
- :ls
- :b 2go to buffer# 2
- :vsp filenamevertical split
- :sp filenamehorizontal split
- :bnextmapped to (Ctrl + → ) next buffer
- :bprevmapped to (Ctrl + ← ) previous buffer
- :sbvertically split buffer on current file
External commands and writing (vimtutor# 5)
- :!commandexecutes an external command.
- :w FILENAMEwrites the current Vim file to disk with name FILENAME.
- v motion :w FILENAMEsaves the Visually selected lines in file FILENAME.
- :r FILENAMEretrieves disk file FILENAME and puts it below the cursor position.
- :r !dirreads the output of the dir command and puts it below the cursor position.
Editing
Normal mode
- :m .+1Move line down
- :m .-2Move line up
- `cw change word (deletes word and switches to insert mode)
- ci"change insde " (also, ([{, etc)
- 55,60d deleted lines 55-60 (can be applied to other actions)
- gg=Gformat all file
- # ngggo to line number # n
- :set listshow whitespaces
- uundo
- Uundo changes on line
- CTRL + Rredo
- Agoes to end of line and enters insert mode
- 
*goes to next appearance of the word pointed by the cursor
- 
zbgoes to bottom of screen
- zzgoes to middle of screen
- ztgoes to top of screen
Screen moving
- ^fmove one screen forward
- ^bmove one screen backwards
Insert mode
- ctrl+Space(was- Ctrl+N) Autocomplete
Visual Mode
- vvisual mode
- Vvisual line mode
- CTRL + vvisual block mode
Add text to multiple lines (at the same cursor position: column)
- Enter visual block mode
- Select lines
- Type I(capital i)
- Write text
- Press Escape
Plugins
Ultisnips
- From here.
- Configure custom snippets folder like this:
let g:UltiSnipsSnippetDirectories=["~/.vim/custom_snippets"]
- As noted in the docs, do NOT map tab to trigger:
let g:UltiSnipsExpandTrigger="<c-e>"
- Check Python interpolation
NERDTree
- RTFM
- or
- Read this:
From within NERDTree Window- mbring up the NERDTree Filesystem Menu
- acreate new file
- topen in tab
- iopen in h split
- sopen in v split
- Oexpand selected folder recursively_
- Ishow/hide hidden files
- Cchange root to highlighted folder
 
Block move by patterns
From Learning the vi and vim editors Given the file with contents:
Rh 0 "Get status of named file" "STAT"
.Rh "SYNTAX"
.nf
integer*4 stat, retval
integer*4 status(11)
character*123 filename
...
retval = stat (filename, status)
.fi
.Rh "DESCRIPTION"
Writes the fields of a system data structure into the
status array.
These fields contain (among other
things) information about the file's location, access
privileges, owner, and time of last modification.
.Rh "PARAMETERS"
.IP "\fBfilename\fR" 15n
A character string variable or constant containing
the Unix pathname for the file whose status you want
to retrieve.
You can give the ...
Suppose that you decide to move DESCRIPTION above the SYNTAX paragraph. With pattern matching, you can move blocks of text on all 150 pages with one command!
:g /SYNTAX/.,/DESCRIPTION/-1 move /PARAMETERS/-1
This command works as follows. First, ex finds and marks each line that matches the first pattern (i.e., that contains the word SYNTAX). Second, for each marked line, it sets . (dot, the current line) to that line, and executes the command. Using the move command, the command moves the block of lines from the current line (dot) to the line before the one containing the word DESCRIPTION (/DESCRIPTION/-1) to just before the line containing PARAMETERS (/PARAMETERS/-1).
Snippets
Reverse lines
- All
:g/^/m0
- Range (reverse only lines 100-150)
:100,150g/^/m99