<-- home

Vim tips

Some important Vim tips I collected over time. For me they serve as a reference and as a reminder.

Naming conventions over configuration files

This are how some important chars are described on configuration files like vimrc.

  • <Bar> : also known as pipe
  • <Esc> : escape char
  • <CR> carrier return or enter
  • <Leader>: the meta character leader

Normal commands

  • ZZ quit, saving if the buffer was altered saves it too
  • ]p indented paste
  • P paste above.
  • p paste below.
  • S substitute the line
  • gUU all the line uppercase
  • guu all the line lowercase
  • G=gg indent all the buffer
  • \`\` puts the cursor on the line of the last modificaton
  • =% reindent inside a paired block
  • p puts text from a register after the cursor
  • P puts text from a register before the cursor
  • gv go visual back again
  • s delete a single character and remains in insert mode
  • X delete a character before cursor

Movements

  • # search for the current word backward (the opposite of *)
  • % go to the closing match of the under cursor character. Some applied chars are([{}])
  • e the end of the word
  • ge the end of the previous word
  • ( the beginning of a sentence
  • ) the end of a sentence
  • { the beginning of a block
  • } the end of a block
  • H the highest point of the screen
  • M the middle of the screen
  • L the lowest point of the screen
  • zt puts the curso on the top of the screen
  • zz puts the curso on the middle of the screen
  • zt puts the curso on the bottom of the screen

Registers

If we wanted to yank the current word into register a , we could run “ayiw . Or if we wanted to cut the current line into register b , we could run “bdd . Then we could paste the word from register a by typing “ap , or we could paste the line from register b by typinng “ap , or we could paste the line from register b by typing “bp. (Pratical Vim)

Some important registers to keep in mind.

  • "+ the clipboard
  • "\* the primary
  • ": Last ex command
  • "/ Last search pattern
  • ". Last inserted text
  • "0 yank

Copy to a register:

"ay$ Yank to register a till the end of the word

"ap Paste register a

\"uyiw Yank the current word to register u

/<C-r>u Paste the register u on search. implements ServiceLocatorAwareLoca

Cleaning registers

qaq to register a.

Check content of register

:reg a to check the content of register a.

Appending to register

A appends to register a.


Search and substitute

\c to search all previously with case insensitive. \C to search all previously with case sensitive.

Very magic search is a search that consider all (applicable) chars its finds as regex special ones, so no escaping to get regex here.

/\vVeryMagicRegexSearch

Very no magic search is a search that consider all (applicable) chars its finds as literals special, so you need to escape get regex here.

/\VVeryNoMagicSearch

End of the match

/foo/e put the cursor on the end of the match

Substitute the first parenthesis matched on the last search

Obs: The parenthesis matches on searches goes inside numeric registers so it could be handled ways beyond what is exemplified above.

Repeat the last substitution

:%s//~/

Searching on history

q: ex commands history q/ search commands history


Global command

:g/\v^\#{1,}/normal gUU To capitalize all markdown titles

:g/TODO/t$ The t command here append to the end of the file

Invert selection of global command

:v/TODO/yank A

Run commands inside blocks on a global context

:g/{/ .+1,/}/-1 >

Other side of visual selection

When on visual mode you could adjust the other side of the visual selection by pressing o.


Tags

g<C-]> List all matching tags <C-t> Back on the tag list

:tag ServiceLocatorAware<tab>

To go to a tag from ex mode


Autocomplete

part<C-x><C-l><ESC> To auto complete a line.

part<C-x><C-f> To auto complete a file name.


Spell

  • [s back
  • ]s forward
  • zg add current word to spell file
  • zw remove current word from spell file
  • zug undo dictionary crud
  • 1z= to get the first suggestion as correction
  • <C-x>sto fix a spelling problem on the current line when on insert mode.

Macros

:'<,'>normal @a to run a macro over the selection 5@a to run in series


Surround

Checkout this awesome plugin: surround.vim.

  • S" to surround the selection
  • S) to surround inside parenthesis
  • cs]) change surrounding ] for )

Repeat some character to undescore a phrase

Vr-

g; navigate back on changes history g, navigate forward in changes history

Going to variable definition

gd above a variable moves to it’s definition on the file

Seing a function definition

[I with the cursor over the function call

  1. http://stevelosh.com/blog/2010/09/coming-home-to-vim/
  2. https://wiki.archlinux.org/index.php/Vim
  3. http://vim.wikia.com/wiki/Best_Vim_Tips
  4. http://vim.wikia.com/wiki/Search_and_replace
  5. http://benmccormick.org/2014/11/10/vim-workflows-file-switching-strategies/
  6. http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text
  7. http://statico.github.io/vim.html
  8. http://www.viemu.com/a-why-vi-vim.html
  9. http://www.atmos.albany.edu/deas/atmclasses/atm350/vi_cheat_sheet.pdf
  10. http://zzapper.co.uk/vimtips.html

Other tips? Please share!