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/wordbut without typing - Use
#to search for the word under the cursor in reverse order - Use
/wordotherwise
:%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*.rbwildcards 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}/gereplace for each file in the quickfix -ewill ignore errors if not matches found:cdo wor:cdo updateto 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
"ayyyanks current line into register "a""appastes from register "a""Ayyappends yanked line to register "a":reg ainspects register "a"qaqresets register "a":g/monkey/yank Ayanks all lines containing "monkey" and append them into register "a"
-
Ctrl-w g fwhile 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 file3opens Vim and the 3 files on separate tabs (panels) -
Ctrl-^alternates between 2 buffers- type
:lsto see the open buffers - switch between active buffer (marked as
%a) and the previous one (marked as#h) where "h" stands for "hidden"
- type
-
:onlycloses 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 rrotates windows clockwise (Rfor inverse) -
Ctrl-w HJKLmoves the window to the far side direction -
Ctrl-w Textracts the current window in a new tab -
:tabclosecloses current tab -
:tabonlycloses all tabs leaving the current one open -
:tabmove <number>moves the current tab to the N position -
<number>gtgoes to the N-th tab
:Exploreopens an interactive file explorer - same as:e.%creates a filedcreates 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
-
tfor "tag":citas "change inner html tag" -
/for "search" term:c/termas "change until 'term' is found" -
?is like/but searches backwards -
rfor "replace": select line +r#will replace all characters with# -
agoes to INSERT mode "after" the current character -
Agoes to INSERT mode at the end of the line ("after ALL characters in line") -
Yyanks entire line -
Ppastes above the cursor whileppastes 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) -
sdeletes "character" under cursor and goes into INSERT mode -
Sdeletes "line" and goes into INSERT mode -
With lines that wrap on multiple "visible" lines, prefix commands with
gto move through visible linesgjmoves 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
tis the short forcopycommand
- use
Ctr-pandCtr-nopens a list of suggestions for auto-completionCtr-x Ctr-fopens a list of "suggestions based on the files/directories" in the current directoryCtr-x Ctr-lopens a list of "suggestions based on the lines" in the current fileCtr-x Ctr-pwill suggest similar patterns - also repeating the same commands will continuously add the next line
Ctr-r 0pastes content of register"0- Like"0pin NORMAL modeCtr-r =evaluates an expression and inserts the result inline
Ctrl-vto enter in Visual select modeoto control the "opposite" corner for selection
:set spellenables the spell checker (:set nospellto turn off) - alternatively:set spell!to toggle on/off]sgoes to the next misspelled word - or previous one with[sz=opens a list of suggestions1z=tries to autocorrect using the "first" (more likely) occurrencezgmarks word as good - whitelist wordzwmarks word as bad - blacklist wordzugandzuwto undo:set spelllang=ensets the language for the dictionary
:earlier 1hreverts to version of 1 hour ago:later 1hgoes forward to 1 hour ahead
:!%runs current file in shell
:argdo <command>runs the command for all files in the:argslist:argdo %s/monkey/gorilla/gereplaces "monkey" with "gorilla" and ignores errors if no matches found:silent argdo %s/Bob/Alice/gehides details of the substitutions
:bufdo <command>does the same for all files in open buffers:argdo updatesaves the changes on all modified files