Personally curated list of advanced Vim tricks inspired by http://vimcasts.org
Table of contents
- Search and Replace
- Registers
- Navigation
- NORMAL mode
- INSERT mode
- VISUAL mode
- Spelling
- History
- Commands
:set hlsearch
:set nohlsearch
to toggle search highlighting
- Use
*
to search for the word under the cursor - like/word
but without typing - Use
#
to search for the word under the cursor in reverse order - Use
/word
otherwise
:%s/\s\+$//e
- strip trailing white spaces- replace in the whole file
:%s
- the spaces till the end of the line
/\s\+$/
- with nothing
//
- and suppress any errors if no matches found
e
- replace in the whole file
:s/\%V#/ /g
- replace#
character with whitespace only within the VISUAL selection\%V
Vimgrep is a very powerful search tool that allows searching not only in the current file but in any provided targets.
Results will appear in the quickfix.
:vimgrep/{pattern}/{target}
- search for pattern where {target} can be:
%
current file*.rb
wildcards or file list`find . -type f`
backtick expression##
special symbol that represents the args (set by:args <expr>
)
Also :vimgrep
can be shorted as :vim
:vimgrep /{patter}/ {target}
search for a pattern:cdo s/{pattern}/{replacement}/ge
replace for each file in the quickfix -e
will ignore errors if not matches found:cdo w
or:cdo update
to write the changes
:g/monkey/d
- delete all lines containing "monkey":g/^monkey/d
- delete all lines starting with "monkey":v/monkey/d
- delete all lines that do not match "monkey" (like grep -v):g/^$/d
- delete blank lines
All commands above consist in the composition of 3 subcommands:
- grep the document globally
:g
- to find blank lines
/^$/
- and delete them
d
"ayy
yanks current line into register "a""ap
pastes from register "a""Ayy
appends yanked line to register "a":reg a
inspects register "a"qaq
resets register "a":g/monkey/yank A
yanks all lines containing "monkey" and append them into register "a"
-
Ctrl-w g f
while being with the cursor on a word (e.g. "file.txt") opens the file with that name on a new window -
from terminal:
vim -p file1 file2 file3
opens Vim and the 3 files on separate tabs (panels) -
Ctrl-^
alternates between 2 buffers- type
:ls
to see the open buffers - switch between active buffer (marked as
%a
) and the previous one (marked as#h
) where "h" stands for "hidden"
- type
-
:only
closes all windows leaving only the current one open -
Ctrl-w _
maximizes the current window vertically -
Ctrl-w |
maximizes the current window horizontally -
Ctrl-w =
equalizes/restores original sizes -
Ctrl-w r
rotates windows clockwise (R
for inverse) -
Ctrl-w HJKL
moves the window to the far side direction -
Ctrl-w T
extracts the current window in a new tab -
:tabclose
closes current tab -
:tabonly
closes all tabs leaving the current one open -
:tabmove <number>
moves the current tab to the N position -
<number>gt
goes to the N-th tab
:Explore
opens an interactive file explorer - same as:e.
%
creates a filed
creates a directory
:sp.
opens the file explorer in an horizontal split:vs.
opens the file explorer in a vertical split
:r! <system cmd>
>> runs a command on the system shell and reads the output into the current position of the current buffer
-
t
for "tag":cit
as "change inner html tag" -
/
for "search" term:c/term
as "change until 'term' is found" -
?
is like/
but searches backwards -
r
for "replace": select line +r#
will replace all characters with#
-
a
goes to INSERT mode "after" the current character -
A
goes to INSERT mode at the end of the line ("after ALL characters in line") -
Y
yanks entire line -
P
pastes above the cursor whilep
pastes after the cursor -
g;
goes to the last INSERT mode position - using it multiple times jumps backwards in the changelist history- use
g,
to move forward in the changelist
- use
-
%
jumps between parenthesis or line block delimiters (E.g. Ruby's begin-end) -
s
deletes "character" under cursor and goes into INSERT mode -
S
deletes "line" and goes into INSERT mode -
With lines that wrap on multiple "visible" lines, prefix commands with
g
to move through visible linesgj
moves to the next visible lineg$
moves at the end of the visible line
-
:-7t.
copy line from 7 lines above (with relative numbers) "to" (t
) "this" (.
) location- use
:+10t.
to copy the 10th line below. - without +/- sign it consider the "actual" line number
t
is the short forcopy
command
- use
Ctr-p
andCtr-n
opens a list of suggestions for auto-completionCtr-x Ctr-f
opens a list of "suggestions based on the files/directories" in the current directoryCtr-x Ctr-l
opens a list of "suggestions based on the lines" in the current fileCtr-x Ctr-p
will suggest similar patterns - also repeating the same commands will continuously add the next line
Ctr-r 0
pastes content of register"0
- Like"0p
in NORMAL modeCtr-r =
evaluates an expression and inserts the result inline
Ctrl-v
to enter in Visual select modeo
to control the "opposite" corner for selection
:set spell
enables the spell checker (:set nospell
to turn off) - alternatively:set spell!
to toggle on/off]s
goes to the next misspelled word - or previous one with[s
z=
opens a list of suggestions1z=
tries to autocorrect using the "first" (more likely) occurrencezg
marks word as good - whitelist wordzw
marks word as bad - blacklist wordzug
andzuw
to undo:set spelllang=en
sets the language for the dictionary
:earlier 1h
reverts to version of 1 hour ago:later 1h
goes forward to 1 hour ahead
:!%
runs current file in shell
:argdo <command>
runs the command for all files in the:args
list:argdo %s/monkey/gorilla/ge
replaces "monkey" with "gorilla" and ignores errors if no matches found:silent argdo %s/Bob/Alice/ge
hides details of the substitutions
:bufdo <command>
does the same for all files in open buffers:argdo update
saves the changes on all modified files