Tabla de Contenidos

Vim shortcuts and commands

Summary of the most useful and common used Vim shortcuts and commands. In these examples, the <leader> key is comma ( , ), set with:

let mapleader=",".


Key / Command Action / Results
h, j, k, l Move cursor left, down, up and right in command and visual mode
gj, gk On long wrapped lines, these commands go down and up in the line "visually": (inside the line, not jumping to the next line), like in several other editors.
0 and ^ Move cursor to the beginning of the line or to the first char of the line
$ Move cursor to the end of the line
gg and G Go to first or last line of the document
<Number>G Move cursor to the line number <Number>
<Number>% Move cursor to the <Number>% of the file (ex: 50%)
Ctrl+f and Ctrl+b Scroll 1 page Forward or Backwards
Ctrl+d and Ctrl+u Scroll 1/2 page Up or Down
Ctrl+e and Ctrl+y Scroll 1 line Forward or Backwards
H and M and L Place the cursor on the High, Medium or Low line of the screen
zt and zz and zb Alter the "viewable window" so that the current line is in the top, zenter (center) or bottom of the screen
w and b Move cursor a word forward and backwards (W and B for works with hyphens, etc)
{ and } Move cursor a paragraph (or block of code) forward and backwards
% Move cursor to the matching parenthesis, curly braces, quotes, etc ( ( ), [ ], { }, ' ', " " ...
e and ge Move cursor at the end of the next or previous word (E and gE for works with hyphens, etc)
t<char> and T<char> Move cursor to first ocurrence of <char> forward (t) or backwards (T). Supports multipliers (2tX)
f<char> and F<char> Move cursor to first ocurrence of <char> forward (f) or backwards (F), included <char> itself. Supports multipliers (2f<char>)
; Repeat the execution of the last fFtT command
gi and g; and '. Move to the last edited line (or the exact cursor position)
m[a-z] Mark current cursor position locally (Lowercase marks = mark for this file).
m[A-Z] Mark current cursor position globally (Uppercase marks). You can return to this file and position later at any moment.
'[a-z] and `[a-z] Go to line marked. Change to the list (') or to the exact cursor position of the mark (`)
'' and `` Return to the position on which we were before a mark-jump (great!)
'^ Go back to the last line you were in Insert Mode (and actually inserted text)
'[ and '] Go back to the first ([) or last (]) line of the latest block modified in the document.

Most of the movement commands can work as destination for delete/copy actions, including also multipliers (d, y, etc), such as d{ (delete to next paragraph), d2{ (delete 2 paragraphs), y5w (copy 5 words), y$ (copy to end of line), dH (delete to top of screen), d% (delete to matching parenthesis or bracket), y'a (copy from cursor to mark a), dt( (delete from cursor to next open parenthesis), etc.

Alter text

Key / Command Action / Results
ESC Change to command mode (for not to alter text)
i and I Insert text at cursor or at Beginning of Line
a and A Insert text at right of the cursor or at End of Line
o and O Insert empty new line after or before the current one (and switch ot Insert mode)
S and C Substitute entire line (replace line with S) or from cursor to end (with C), and switch ot Insert mode. C is synonym of c$.
D or d$ Delete from cursor to end of line (copies text to buffer, keeps command mode). Also d0 or d^ to beginning of line
x and X Delete char under cursor, or char left to cursor (backspace). Synonyms of dl and dh.
r<char> and c<char> Replace character under cursor in command mode, with <char>. r remains in command mode and supports multipliers like 10rX, c replaces the char and switches to Insert mode.
J Join the current line with the next one
dt<char> and dT<char> Delete from cursor to <char> forward (t) or backwards (T). Also allows c and y like ytX. Supports multipliers like d2tX
df<char> and dF<char> Delete from cursor to <char> forward (f) or backwards (F), included <char> itself. Also allows c and y like yfX. Supports multipliers like d2fX
yy and dd Yank (copy) or delete current line (delete also copies to the default buffer).
. Repeat the last command that altered the document. Very useful for repetitive changes or repeating the last "complex" command quickly.
ci<char> and di<char> Change or delete the text between <char> and <char>. Where <char> can be ', ", (, {, [ and <. So we can use di( or di) to delete all the content inside ( and ) (with cursor anywhere inside the parenthesis). Also works for strings with the quotes/double-quotes char di" ⇒ quick way of delete the content of a string.
cit Like ciX but for tag entities (like <p> and </p>)
u and Ctrl+r Undo and Redo changes (multiple times)
:earlier <time> and :later <time> Rewind or advance changes in time: (ex: :earlier 1h and then later: 10m)

Special delete statements in the middle of a word or sentence:

Key / Command Action / Results
dw Delete from current cursor position to end of current word (dW includes hyphens, etc)
db Delete from cursor to beginning of the current word.
diw and daw Delete the word under the cursor, no matter where the cursor is located inside the word (daw includes spaces)
dis and das Delete the sentence (until a dot) under the cursor (das includes spaces)
dat Delete a tag (html, xml) (deletes everything between <TAG> and </TAG>, tag included).

Files, Windows and Buffers

Key / Command Action / Results
:wq! and :x and ZZ Save buffer to file and exit
:q! Exit without saving buffer to file
gf Open the filename under the cursor (great for opening include/require files)
:e filename Open a file in the current buffer
:enew Open a new, empty buffer
:bn and :bp Move to next or previous buffer (close with :bd or :bD!). Also :bf and :bl for first and last.
Ctrl+w v and Ctrl+w s Split window vertically or horizontally
Ctrl+w cursors and Ctrl+w h/j/k/l Move among windows (up, down, left, right…)
Ctrl+w q and Ctrl+w c Close current window (user :q! if not saved)
Ctrl+w + and Ctrl+w - Resize horizontal split in 1 unit (+ or -). Also :resize +-N
Ctrl+w < and Ctrl+w > Resize vertical split in 1 unit (+ or -)
Ctrl+w _ Maximize current window size (make all other windows of size=1)
Ctrl+w = Make all windows equal size
Ctrl+w o Make this window the only one (close all others)
Ctrl+w r and Ctrl+w x Rotate or swap window order
:sp filename or :split filename Open a file in a new horizontal split
:vsp filename or :vsplit filename Open a file in a new vertical split
vim -o *.txt or vim -O *.txt Open (from the command line) a set of files in horizontal or vertical splits

Copy, Paste and Delete

Key / Command Action / Results
dd and yy Delete (and copy) or copy current line (into the default buffer). Supports multipliers (10dd)
p and P Paste default buffer contents after and before the current line. Supports multipliers (2p).
]p Paste default buffer contents reindenting it to the appropiate level based on the place where we paste (auto indents code)
v and V Visual selection (using cursors or hjkl) for characters or full lines
Ctrl+v Block Visual selection (copy and paste blocks!)
ggVG Select all the content (gg = go line 1, V = visual mode, G = go EOF).
d and y and c (in visual mode) Delete, copy or replace the selection. d and y keep command mode while c switches to insert mode.
d`a and c`a and y`a Delete, cut or copy from the current cursor position to mark 'a or `a
"<register><vim-command> Use <register> a-z before the y, p, d commands to use one of Vim's 26 registers (instead of using the default -Unnamed- register). Ex: "zdd and then "zp
%!external-command Filter the entire file (or visual selection with :!command*) through a command (ex: %!sort
%r filename or %r !external-command Insert the contents of a file or the output of a command inside our file (ex: %r !date

The following ranges are available for some of the operations:

Appart from the default buffer ("the unnamed buffer") the following additional buffers (registers) are available for buffer operations:

Search and replace

Key / Command Action / Results
* and # Search the word under the cursor forward and backwards. Use it again to repeat. Highlights matches if using set hlsearch.
/<string> and ?<string> Search <string> forward and backwards. String can be a regular expression (see below)
/\c<string> and ?\c<string> Ignore case on a search
d/<string> and y/<string> Searches also admits actions: delete or copy from cursor to next ocurrence of string
/\V<string> and ?\V<string> Insert \V to treat strings as "raw strings" and not regexps
n and N Go to next or previous search ocurrence
%s/regexp1/string/modifiers Replace ocurrences of "regexp1" with "string".
%s/\Vstring/string/modifiers Insert \V to treat the search string as "raw string" and not regexp
%g/regexp1/action Apply action(s) to all lines matching regexp1 (example: :%g/DONE/d deletes all lines containing the string DONE). Lots of commands can be used (jklm, p, f, etc). Ex: g/BAR(/ normal nf(iT
%v/regexp1/action Apply action(s) to all lines NOT matching regexp1 (example: :%g/TODO/d

Replacement modifiers:

Searches (with / and ?) and the search of replacements follow Vim's regular expression format (different from Perl's):


/mystring                           # simple string search
/I have 1\$ in the bank             # Escape $ because we don't want end of line
/red\|green\|blue                   # OR (|) between terms
/\<completeword\>                   # <word> = search for complete word (not substrings)
/\<\(\d\+\)\>                       # integer number
:%s/cad1/cad2/gi                    # simple string replacement
:%s|cad1|cad2|g                     # Use a different separator to free / from escaping
:%s/red\|green\|blue/purple/g       # search regexp
:%s//purple/g                       # reuse last search with replacement
:'<,'>s//purple/g                   # apply replacement only to selection
:%s/style=".\{-}"//g.               # non-greedy operator (like .*? in Perl)
:%s/\s\+$//e                        # Remove trailing blanks at end of line
:%g/re/d                            # Delete all the lines matching regexp 're'
:%v/re/d     or    :!g/re/d         # Delete all the lines NOT matching regexp 're'
:%g/^\s\+#/d                        # Example: delete python comments.

Ctrl+R in insert mode

After pressing Ctrl+R while in insert mode:

Key / Command Action / Results
% Insert the name of the current file
* or + Insert the clipboard contents
=math-expression Insert the result of a math expression (ex: =5*5)
register (a-z or ") Insert the content of the specified register

Operating in the Visual Mode

While in visual mode, we can use not only cursors and ijkl to select text, but also the standard Vim verbs like 0, $, G, gg, and movements like fCHAR, /string, n (next search result), etc.

Also, there are some very useful movement verbs:

Key / Command Action / Results
iX Select the entire block based on X, where X can be:
w or W ⇒ word
s ⇒ sentence
p ⇒ paragraph
b or ( ⇒ block of parenthesis (content inside ( and ) ).
B or { ⇒ block of { braces }
t ⇒ a <tag> and </tag> block
< ⇒ a < and > block
[ ⇒ a [ and ] block
" or ' ⇒ string (block of quotes)
NOTE: Closing tags also work, like ), ], etc.
aX Select the entire block based on X. Like iX but includes the surrounding spaces if present.

Once the text is selected, we can apply our standard Vim actions like d (delete), c (change), y (yank/copy), ~ / U / u (change case), x (delete), ! (filter through external command), < and > (indent), J (join), p (paste and replace), = (autoindent / format)…

So, if we are programming, we can delete a complete string (the content between " and " in "string") just pressing vi"d with the cursor anywhere inside the string.

Also, with the cursor inside the parameter list of a function like myfunc( arg1, arg2, arg3 …, argn), we can delete all the content between ( and ) by pressing vi(d , which means change to visual selection mode (v), select all the content between ( and ) (with i() and delete it (with d). You could use also vi)d (any of the 2 matching characters will work) or the generic vibd.

Programming aid

Key / Command Action / Results
zf Create a fold with the selected lines (or movement like zf/string or zf}
zd and zE Delete one or all the folds in the file (just the fold itself, not the content)
za Open/Close current fold (Also <space> with mapping - see custom mappings section below)
zF and zR Open/Close all the existing folds in the file
== Indent current line (or selection) with the auto-indent (based of the language/filetype)
> and < Indent code 1 "column" (use . to repeat indentation)
Ctrl+a and Ctrl+x Increase or decrease the number under the cursor (command mode)
~, u and U Change caseof the char under the cursor, or the selection. (~ = toggle, u = make lowercase and U = make uppercase)
Ctrl+] Jump to the ctags definition of the word under the cursor
Ctrl+t o Return to the place of the last tag-jump
q[a-z], q and @[a-z] Start recording macro, stop recording and reproduce macro
@@ Repeat last macro executiong (also supports multipliers like 2@@
Ctrl+a and Ctrl+x Increase or decrease the number under the cursor (command mode)
Ctrl+x Ctrl+o Autocomplete word under cursor (Use tab with the supertab plugin)
Ctrl+v <char> Insert raw char (ex: tab, M, etc)
vi"d (or vi"c) Delete or change the content of a double quoted string. Also works vi', vi{, vi[, vi(, vi<, etc. for the content of {}, [], (), <> blocks. Check Visual Mode summary above.
* Search (and highlight all ocurrences) for the word under the cursor
gd and gD Move to the local or global declaration of the variable under the cursor
gf Open the file whose filename is under the cursor
:retab Replace tabs with blocks of spaces
zl and zl Scroll the screen left or right without moving the cursor (allows repeaters)
[{ Go to start of current code block (start of function, method, if(), etc)
]} Go to end of current code block (end of function, method, if(), etc)
[[ Go to previous code block (previous function, method, if(), etc)
]] Go to next code block (next function, method, if(), etc)
:retab Reformat an existing file to match your current tab/space settings


Key / Command Plugin Action / Results
,n NERDTree Toggle NERDTree window (calls :NERDTreeToggle with nmap <leader>n :NERDTreeToggle<CR>)
(In NERDTree window) t NERDTree Open the selected file in a new tab (Use T to keep focus on current tab)
(In NERDTree window) i NERDTree Open the selected file in a horizontal split window
(In NERDTree window) s NERDTree Open the selected file in a vertical split window
(In NERDTree window) I NERDTree Toggle hidden files
(In NERDTree window) m NERDTree Show the NERD Tree menu (add files/directories, delete, etc)
(In NERDTree window) R NERDTree Refresh the tree, useful if files/dir content change outside of Vim
(In NERDTree window) ? NERDTree Toggle NERD Tree's quick help
,ci NERDCommenter Toggles comments on current line or each selection line individually (not aligned)
,cl and ,cb NERDCommenter Toggles left or both comments on current line or each selection line (aligned)
,cc and ,cu NERDCommenter Comments out or uncomments the current line or selection
,c$ NERDCommenter Comments the line from the current cursor position to the end of line
,ca NERDCommenter Changes to the alternate set of comment / delimiters
cs'" Surround Inside a string delimited by single quotes, replace them with double quotes (also cs"')
ds' Surround Delete quotes (can be also used with tags like ds<p>)

Personal mappings and options

Key / Command Mapping Action / Results
<space> set foldmethod=manual
set foldtext=getline(v:foldstart)
vmap <space> zf
nmap <space> za
Fold/Unfold selection or line
,cl and ,cc nnoremap <leader>cl :set cursorline!<CR>
nnoremap <leader>cc :set cursorcolumn!<CR>
Toggle highlight current cursor column or line
,n1 and ,n0 nmap <leader>n1 <Esc>:set nu<CR>:set rnu<CR>
nmap <leader>n0 <Esc>:set nonu<CR>:set nornu<CR><CR>
Toggle highlight current cursor column or line
<F3> nnoremap <F3> :NumbersToggle<CR> Alternate between relative and absolute numbers (Plugin numbers.vim)
F7 or ,hs nmap <F7> <ESC>:sign unplace *<CR>:delm!<CR>:delm A-Z0-9<CR>
nmap <leader>hs <ESC>:sign unplace *<CR>:delm!<CR>:delm A-Z0-9<CR>
Remove all marks and hide the Sign Column
,/ nmap <silent> <leader>/ :nohlsearch<CR> Remove the highlights of the last search result
<F12> set pastetoggle=<F12> Toggle Paste Mode (on and off)
,td, ,todo, ,tasklist map <leader>td <Plug>TaskList
map <leader>todo <Plug>TaskList
map <leader>tasklist <Plug>TaskList
Tasklist plugin (highlight all TODO lines in a new window)
,ch map <leader>ch <ESC>:SyntasticToggleMode<CR> Toggle syntastic syntax analysis
Ctrl+w u let g:undoquit_mapping = '<C-W>u' Undo last Window close / quit / :q!
,t, ,tags, ,tagbar, <F8>, let g:tagbar_width = 20
nmap <leader>t :TagbarToggle<CR>
nmap <leader>tags :TagbarToggle<CR>
nmap <leader>tagbar :TagbarToggle<CR>
map <F8> <ESC>:TagbarToggle<CR>
Tasklist plugin (highlight all TODO lines in a new window)
Ctrl+p let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'
Fuzzy search with the ctrlp plugin
:w!! cmap w!! w !sudo tee % >/dev/null Save file as root using sudo
j and k nnoremap j gj
nnoremap k gk
On wrapped lines, go up and down in the same line (THANKS Vincent Driessen!)
,/string nmap <silent> <leader>/ :nohlsearch<CR> Hide search result highlight
,S and ,L nmap <leader>S <ESC>:mksession! ~/vim_session<CR>
nmap <leader>L <ESC>:source ~/vim_session<CR>
Save and Load current session (tabs, open files) on Windows and Mac
,ac and ,autoclose let b:AutoCloseOn = 0
nmap <leader>ac <ESC>:AutoCloseToggle<CR>
nmap <leader>autoclose <ESC>:AutoCloseToggle<CR>
Toggle Autoclose plugin (autoclose brackets, quotes, etc)
,h set nolist
nmap <leader>ac <ESC>:AutoCloseToggle<CR>
set listchars=tab:>.,trail:.,extends:#,nbsp:.
nmap <leader>h <ESC>:set list!<CR>
Toggle see hidden chars (trailing spaces, tabs, etc).
cw, dw set iskeyword-=_ Remove the underscore character ('_') from the list of chars considered part of a word (A-Z, a-z, 0-9 and _ by default).
/, ?, *, n & Ctrl+C (cancel searchhl) highlight SearchCurrent ctermbg=LightGreen ctermfg=black Configure fg/bg color for current search (plugin searchant)
,e let g:netrw_liststyle=3
nmap <leader>e :Explore<CR>
map <leader>e <ESC>:Explore<CR>
Replace NerdTree with Vim's Explorer. Keys:
d = create new directory.
% = creates and opens a new file.
D and R = Delete or Rename directory or file.
o and v = opens the file in a horizontal or vertical split.
c-w = + - resize splits to be the same (=), or alter size (+/-)

Gvim specifics

Key / Command Action / Results
:tabnew! Create new empty tab
:tabedit filename Open the specified file in a new tab
:tabclose Close current tab (also :q!)
tabnext [n] and tabprev [n] Go to next and previous tab
gt and gT Go to next or previous tab (also Ctrl+PgDn and Ctrl+PgUp)
Ngt Go tab N
:tabs Display list of open tabs
:set mousemodel=popup Make mouse right button behave like in gVim for Windwos (display contextual popup with Cut/Copy/paste)


<Volver a la sección de GNU/Linux>