VIM editor

Visit Vim Homepage

articles/vim/vim-demo.png

Modes

  • Insert
  • Command
  • Visual
  • Visual block

By default Command mode is active. Insert mode used for typing. Visual modes used for copy paste. Visual block mode used to select and copy colums.

'i' enter Insert mode
'o' - enter Insert mode and add new line
'v' - enter Visual mode
'ctrl+v' - enter Visual block mode
'esc' - exit Insert or Visual mode and back to Cammand mode

Open Close file

$ vim file.txt
':q' - close file
':q!' - close without saving
':wq' - save and close file
':e other-file.txt' - open other file without closing vim 
':new' - new unsaved unnamed file in new tab
':w file-name.txt' - save unnamed file
':e' - reload file
':ls' - list open files
':bp' - previous
':bn' - next
':bn *filename-part*' - with tab key providing auto-completion

Copy Paste Cut Delete

'yy' - copy line
'p' - paste
'u' - undo
'ctrl+r' - redo
'd' - delete selected
'dd' - delete line
'd5↑' - delete 5 lines up
'y3↓' - copy 3 lines down 
*delete works as cut

Search in file

'/pattern' - search for pattern in file
'/regexp' - search using regual expresion
'n' - step to the next find item
'noh' - clear search highlight

Browse directory

':E' - browse directory

Split View

':vsp' - split vertical
':sp' - split horizontal
'ctrl+w arrow' - move cursor to other tab
':q' - close tab
':cd' - change directory
':pwd'- current directory

Running any linux command

':! git status'

Set syntax highlight

':set syntax=c'

Auto-Completion

'ctrl+p' - show drop down list for autocomplete

Configuration file

Global config, make shure, file permissions is set to 644

/etc/vim/vimrc

Config for single user

/home/(username)/.vimrc

Misc

Running multiple jobs in single terminal window

'ctrl+z' - send to backgound
'jobs' - list all jobs
'fg' - bring from backgound
'fg (number)' - bring from background by list number
'kill %(number)' - kill job	
'cd -' - go to previous path

Search recursive

$ grep -r --include="*.c" "main" ./

Git grep works faster, so to search inside git repo use:

$ git grep "main"
'-n' - to see line numbers

Autoformat

'=30↓' - autoformat 30 lines down

Jumps

'gg' - to top
'shift+g' - bottom
'g10' - to 10th line
'%' - between { }

Indent multiple lines

'5 > ↓' - 5 line down
'>' - selected lines in Visual mode