Freedom, Community & Sustainability

VIM Commands - Survival Kit

October 30, 2014 -- William
Last modified on August 2016
Duration: 5 minutes or less

VIM is a great text/code editor but not so user-friendly. Take some time to learn how to use it and it will seem much more friendlier then. On VIM you need to enter the ---INSERT-- mode to be able to edit your file like a regular text editor. To do this press the "i" key and you will see the 'insert' message at the bottom. Press Escape to exit the insert mode. Keep in mind that the commands below will not work if you are in the insert mode so you have to keep pressing "i" and "Esc" to get in and out of the insert mode. Now try some of the other commands:

i insert mode, it means you can start editing your file
Esc escape from the insert mode (literally)
/<text> search for <text> (press the slash key and start typing)
:set nu show line numbers
:syntax on turns on code highlighting (same thing for off)
:123 moves cursor to line 123
:w save the file
:w new-file.txt save as new-file.txt
:q quit VIM (add ! to quit without saving ":q!" )
Ctrl + V select text (move with arrows)
Shift + V select entire lines (move up or down)
y copy the selected text (press twice to copy the line)
p paste the copied text
d delete text (or entire line if pressed twice)
u undo last operation
Ctrl + R redo last operation
:! <command> executes and shows the output of the command without leaving vim
:help shows VIM help, you can optionally enter a particular command you want help for, like :help number

You can go through a 30 min tutorial in the terminal (Yeah!) by typing vimtutor on the command line (outside of vim).

Some VIM recipies

-Usually you want to save and exit the file at the same time. You can to that with ":wq"

-To open a file at a specific line you can do that with "vim +4 file.txt" to open the text file at line four.

-To add comments on code you can first select several line using "Ctrl+V" and moving the cursor down. Then type "I" (uppercase) and the comment according to your programing language (like //, # or ; ) and then Esc.

-To remove comments from several lines use "Ctrl+V" to select all the comments (just the comments, not the code you want to keep). Then press "d".

-If you use to always enable text highlighting or show line numbers etc. you can configure VIM to do this stuff by default by editing the file /etc/vim/vimrc and remove the comments or add the following lines (vimrc might be located on a different folder depending on your distribution, this is for Debian. If you only want to change stuff for your account, add the file vimrc.local and enter the code below):

if has ("syntax")
  syntax on

set number    " Shows row number by default

Check on vimrc that there are other options available to be commented out. They are commented because they cause VIM to behave differently than VI but they are "highly recommended".

Add new comment