In my work I often need to edit files in a terminal environment. My choice of editor in these environments is VIM. Here are a bunch of commands in command mode that I use.
Start vim
vim <filename>
Start vim with the output of another command/program
<command/program> | vim -
Show vim help
:help
Save file
:write #or 'w' for short
Quit vim
:quit #or 'q' for short
Save AND quit
:wq
Force save (you can also force edit and quit by appending a exclamation ‘!’ mark)
:w!
Show line numbers
:set number
Remove line numbers
:set nonumber
Enable syntax highlighting
:syntax on
Disable syntax highlighting
:syntax off
Change colorscheme
:colorsheme <colorscheme> #E.g. desert
Enable auto indent
:set autoindent
Remove auto indent
:set noautoindent
Set tab spaces
:set ts=4 #Sets tab to 4 spaces
Always show status bar (with filename)
:set ls=2
Show special characters
:set list #Use :set nolist to turn off
Delete all lines between X and Y
:X,Yd #X and Y are linenumbers
Load new file into vim. Can also be used to refresh current file – just use ‘e’ without path or filename
:edit /tmp/myfile #or 'e' for short
Run external command. Vim let you run the command, shows the result and switches back to your Vim window again when you press Enter
:! command #E.g. ':! ls' will show the files of your current location
Go to a specific line numer
:<linenumber>
Substitute something with something else in the whole file
:%s/old/new/g
Search for a string in file (next search result = n)
/<search string>
Use regexp
/<regexp>
Undo last action
u
Split window vertically
ctrl + wv
Split window horizontally
ctrl + ws
Create new vertical window
:vnew
Move between windows
ctrl + ww
Rotate windows
ctrl + wr
Diff two outputs from standard input (from command line)
vim -d <(command1) <(command2)
Diff two files inside vim
:e file1 #Open first file :diffthis #Add window to 'diff pool' ctrl + wv #Open new window ctrl + ww #Move to new window :e file2 #Open second file :diffthis #Add window to 'diff pool' :diff #Execute diff # Extras :set diffopt+=iwhite #To ignore whitespaces
If you want to diff two windows (and not open files) just add them to 'diff pool' and run the :diff command
COMMAND MODE OPTIONS
Delete 5 lines
Stand on the first line you want to delete and press 5dd
Delete a whole line
Stand on the line and press: dd
Direct search of word
Stand in the word and press: *
Find matching bracket
Stand on a bracket and press: % (press % again to go back to previous bracket)
Expand all nodes in diff mode
Press: zR
Fold all nodes in diff mode
Press: za
0 Comments.