Comment on page
My Vim Cheatsheet
_______ _______ _______
|\ /|\ /|\ /|
| +---+ | +---+ | +---+ |
| | | | | | | | | |
| |v | | |i | | |m | |
| +---+ | +---+ | +---+ |
I have started transitioning slowly to lightweight editors, because of my low system configuration. And what can be better than
vim. I will keep a log of things I learn in the process.
Update: I started using vim "actively" from Nov 12, 2020 and it has now been 1 month complete in Vim & I don't think I am moving to another text editor in future.
For starters I use neovim (v0.4.4).
Some must know stuff filtered from the vast array of vim things.
iinsert text before the cursor position
aappend text after the cursor position (my advice, always use this instead of
Aappend text at end of line
oopen a new line after current line
Oopen a new line before current line
xdelete character under cursor
Ddelete until the end of line
rreplace the character under cursor
Rreplace stuff until we want
ddDelete current line.
ccdelete current line and switch to insert mode.
Cdelete everything from the cursor position to the EOL.
- u : Undo latest changes in vim.
- Ctrl + r : to redo
- . : repeat last change in vim.
I felt like a rookie when I used to search this, anyways here is how you do it:
- 1.Enable visual mode by pressing
- 2.Use arrow keys to select text.
- 3.Use d to Cut. OR
- 4.Use y to yank (copy) text (only inside vim)
:"+y: for yanking(copying) text to system's clipboard.
- 5.Use p to paste after the cursor position or P to paste before the cursor.
:"+p: to paste from system's clipboard
- 1.Move your cursor to the desired word
*to select all its occurrences.
- 3.Hit Esc and use
:%s//<replace-word>/to replace all the selected words.
:nohlsearch: for clearing search highlighting. Also read (:h usr_12.txt), section 12.2 for a nice overview on search.When in search mode instead of hitting Enter use
Ctrl + gand
Ctrl + tto traverse matches while still being in search mode.
:earlier N: Time travel in past N seconds.
:later N: Time travel in future N seconds.
:echo $MYVIMRC: to view location of your default
==in Visual Mode to fix line indent.
- 5.When in command mode (:), use Ctrl + f to browse through your command history, live edit any command and hit enter to run it (the quick fix window).
:resize 60to resize windows horizontally or
:vertical resize 60for vertical resizing. Also signed values can be used like +5, -2.
:centerto align text. Assuming width of document is
textwidth(default is 80). You can also specify arguments for e.g
:center 100will move the start of line to 100th column.
- 8.To list all your active/inactive buffers, use
:buffersin command mode. You can switch to a buffer by providing the buffer name,
:buffer <TAB>to see all buffers.
:verb map <key>to check which key is mapped to what operation. Useful when debugging your mappings and differentiating them from that of a plugin.Read help for checking key notations
- 10.Use vim's
wildignoresetting to exclude searching for files and directories according to your project. For e.g for python projects this could look likeset wildignore+=*/.git/*,*/site-packages/*,*/lib/*,*/bin/*,*.pycThis should exclude searching through your virtual environments [Read manual
:h 'wildignore']. Another handy trick is to exclude media files from appearing in search by excluding them as well.set wildignore+=*.jpg,*.bmp,*.gif,*.png,*.jpeg,*.avi,*.mp4,*.mkv,*.pdf,*.odt
:syntaxwill output all highlight groups for syntax highlighting of the current open file. It can come handy when you are writing your own colorscheme.
- 12.Scrolling 2 or more windows together. When in multiple windows (or splits), you can use
scrollbind. Pick one window then
:set scb, pick another window
:set scbfor disabling use
- 13.To search for pattern in vim help text use
- 14.If you have spell-checking (
:set spell) enabled use
zgto exclude certain words from being reported as misspelled. This adds the words to your own list of words called a spellfile. On NeoVim this fill is created automatically, although you can do it manually.mkdir -p ~/.vim/spell/then in
q:to open command line history or
Ctrl + fwhen already in command mode
q/to open search history, this will list all the things you searched using search mode
ito change anything and <CR> to execute again.
- 17.To quickly jump to function definition or variable assignments under cursor use
gd(local declaration) or
- 18.To reselect the last visual selection use
- 19.When in visual mode use
gUto make text uppercase &
It helps you view only a selected range of text. (Read
:h usr_28.txtfor a quick overview)
Quick settings to put in vimrc/init.vimrc
- za: Toggle code folding.
- zR: Open all folds.
- zM: Close all folds.
- zo: Open current fold.
- zc: Close current fold.
- w jump through beginning of words in a line
- e jump to end of words in a line
- b to move backward
- H jump to top of text under screen (not to be confused with top of file).
- M jump to middle
- L jump to bottom
- gg go to top of file
- GG go to end of file
- 0 go to beginning of line
- $ go to end of current line
- ^ go to first character in a line
- g_ go to last character of the line
- zb put current line at bottom of screen
- zt put current line at top of screen
- Ctrl+f scroll down 1 page
- Ctrl+b scroll up 1 page
- f : find next
- F : find backward
- t : find next char & place cursor before
- T : find next char & place cursor before backward
- ; : go to the next occurrence of f/t
- , : go to previous occurrence of f/t
Use Ctrl + x +
- 1.f = File name completion
- 2.l = Whole line completion (context aware, handy if you are copy pasting a previously typed line)
- 3.i = Keywords in current & included file ("include" means when you import or #include)
- 4.s = Spelling suggestions
- 5.k = Keywords from dictionary. For this to work add
set dictionary+=/usr/share/dict/wordsto your vimrc
:help ins-completionto see more such completions
Take registers as "special vim storage locations". There are exactly 21 + 26 registers which store different kind of stuff
In command mode use
:regto display contents of all these registers. Do
h registersto read manual
10 flavors of registers
Vim has a default file browser called netrw, below are some handy tips that will help:
- 1.R : rename a file/directory.
- 2.qf : Show file info.
- 3.x : open file in associated program, use it open media files like images.
- 4.Ctrl + l : refresh netrw, Opens a new buffer. Use
- 5.d : Make a new directory.
- 6.gh : toggle display of hidden files.
- 7.D : Delete a file/directory (Doesn't work on non-empty directories).
It let's you run commands repetitively without using
Qto enter into Ex mode,
visualto go back.
The Ex mode in Vim is quite underrated in 2020 since we have a
:termbut learning about it can be quite helpful sometimes.
I will only add stuff here when I start using it or use it for the first time.