Skip to main content

VIM

Vim tutorial

 ** To move the cursor, press the h,j,k,l keys as indicated or directional keys **
            ^
            k              Hint:  The h key is at the left and moves left.
      < h       l >               The l key is at the right and moves right.
            j                     The j key looks like a down arrow.
            v

Exiting VIM

to Exit VIM without saving press <ESC> then :q! <Enter>

Useful commands in Normal mode (press <ESC>)

i
Insert mode
x
Delete the character under the cursor
A
To append text
wq
To save a file and exit
ESC
Will place you in normal mode or will cancel an unwanted and partially completed command
dw
To delete until the start of the next word, Exluding its first character
d$
To delete to the end of the line (depends on where the cursor is at) Including the last character
de
To the end of the current word Including the last character.
2w
To move two words forward (you can use any combination, 3w will move you 3 words forward.
3e To move the cursor to the end of the third word forward (you can use any combination 2e will move you 2 words forward.
0
Zero - to move to the start of the line
u
to undo the last command
U
to fix a whole line
CTRL-R to redo the commands


Operations and Motions

Multiple combinations can be achieved

d2w will begin the next 2 words beginning at the cursor, d4w will delete the next 4 words.

in the example below we will place our cursor at A (first capital A) and type d2w

--->  this ABC DE line FGHI JK LMN OP of words is Q RS TUV cleaned up.

after typing d2w cursor at A

--->  this line FGHI JK LMN OP of words is Q RS TUV cleaned up.

now place cursor at F and type d4w

--->  this line of words is Q RS TUV cleaned up.

now with our cursor at of type 3w to move 3 words forwward with our cursor at Q type d3w

--->  this line of words is cleaned up.

/ to search after you hit enter press e to move/search to the next line

G G to go to the beginning of the file

Shift G to go to the end of the file

DD to delete a whole line you can also use 2dd to delete 2 lines \

Shift Z Z to exit VIM (this saves changes)

Shift Z Q to exit vim without saving

 

vim Command

Explanation

Esc

Switches from input mode to command mode. Press this key before typing any command.

i, a

Switches from command mode to input mode at (i) or after (a) the current cursor position.

o

Opens a new line below the current cursor position and goes to input mode.

:wq

Writes the current file and quits.

:q!

Quits the file without applying any changes. The ! forces the command to do its work. Add the ! only if you really know what you are doing.

:w filename

Writes the current file with a new filename.

dd

Deletes the current line and places the contents of the deleted line into memory.

yy

Copies the current line.

p

Pastes the contents that have been cut or copied into memory.

v

Enters visual mode, which allows you to select a block of text using the arrow keys. Use d to cut the selection or y to copy it.

u

Undoes the last command. Repeat as often as necessary.

Ctrl-r

Redoes the last undo. (Cannot be repeated more than once.)

gg

Goes to the first line in the document.

G

Goes to the last line in the document.

/text

Searches for text from the current cursor position forward.

?text

Searches for text from the current cursor position backward.

^

Goes to the first position in the current line.

$

Goes to the last position in the current line.

!ls

Adds the output of ls (or any other command) in the current file.

:%s/old/new/g

Replaces all occurrences of old with new.