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
gg
Go to beginning of fileG
Go to end of file
Windows et al
^w
Change subwindow:q
Command 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/gci
Replaces 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//gn
counts ocurrences of patata in all the text
Buffers
:ls
:b 2
go to buffer# 2:vsp filename
vertical split:sp filename
horizontal split:bnext
mapped to (Ctrl + → ) next buffer:bprev
mapped to (Ctrl + ← ) previous buffer:sb
vertically split buffer on current file
External commands and writing (vimtutor# 5)
:!command
executes an external command.:w FILENAME
writes the current Vim file to disk with name FILENAME.v motion :w FILENAME
saves the Visually selected lines in file FILENAME.:r FILENAME
retrieves disk file FILENAME and puts it below the cursor position.:r !dir
reads the output of the dir command and puts it below the cursor position.
Editing
Normal mode
:m .+1
Move line down:m .-2
Move 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=G
format all file# ngg
go to line number # n:set list
show whitespacesu
undoU
undo changes on lineCTRL + R
redoA
goes to end of line and enters insert mode-
*
goes to next appearance of the word pointed by the cursor -
zb
goes to bottom of screen zz
goes to middle of screenzt
goes to top of screen
Screen moving
^f
move one screen forward^b
move one screen backwards
Insert mode
ctrl+Space
(wasCtrl+N
) Autocomplete
Visual Mode
v
visual modeV
visual line modeCTRL + v
visual 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
m
bring up the NERDTree Filesystem Menua
create new filet
open in tabi
open in h splits
open in v splitO
expand selected folder recursively_I
show/hide hidden filesC
change 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