Vi Helpful Commands

Figured I’d write down a quick set of commands in VI. Also, I keep on forgetting a few useful commands in vi so I’m gonna start archiving them here.

Show Hidden Characters

:set list

Reload a file from disk

:e

Hide Hidden Characters

:set nolist

Fix vi’s broken arrow keys (mostly ubuntu machines)

:set nocompatible

Or better yet put it in the .vimrc file.

 echo “set nocompatible” >> ~/.vimrc

Change blue syntax coloring to something you can read on black backgrounds

Add “hi comment ctermfg=blue” to the .vimrc file

 echo “hi comment ctermfg=blue” >> ~/.vimrc

Navigate to top and bottom

Go to top:

gg

Go to bottom:

shift+g

Searching

Search up:

?<SEARCHSTRING>

where <SEARCHSTRING> is what you’re looking for.

Search down:

/<SEARCHSTRING>

Ignore caps in search:

/\c<SEARCHSTRING>

?\c<SEARCHSTRING>

 

 Editing (Insert mode)

i

Press ‘esc’ to get out of insert mode. Use arrow keys and page up/page down to get around.

Delete current line

dd

Delete from one marker to the next

First make a marker

mk

Where k could be anything from the letter ‘a’ to ‘z’

Now move your cursor somewhere and type

d`k

This will delete everything between your current cursor and the marker at k.

Also note that typing `k will take you to that marker.

Exit and don’t save

:q!

! means force. If you changed something and didn’t put !, then it wouldn’t let you exit.

Exit and save

:wq

Open a file in read only mode

vi -R <FILENAME>

 

Find and Replace

:s/SEARCHSTRING/REPLACESTRING/

The above does one line on the cursor, but to do the whole page

:1,$s/SEARCHSTRING/REPLACESTRING/

or

:%s/SEARCHSTRING/REPLACESTRING/