LinuxMoz

Linux Stuff && Coffee

Vi Commands Cheat Sheet

| Comments

The ultimate Vi cheat sheet for anyone learning Vi Commands or the Vi editor in general. Keep this guide close by when using the editor and you will learn how to use Vi in no time. Vim commands will be the same as Vi for the most part – Windows however has Gvim which allows some GUI interaction.

I – Inserts text at the beginning of the text line, not the beginning column of the vi screen

a – Appends to the end of the right of the cursor

A – Appends at the end of the current line

o – Begins a new line below the current line

O – Drops the current line and begins a new one in its place

Vi Replace

cw – Vi replace a single word from the current cursor position. To replace a whole word, you put the cursor on the the first character of the word.

c$ – replace the current line but doesn’t extend to change the rest of a wrapped sentence on the screen

r – Vi Replace the character under the cursor

R – Replaced the text on the same line until Esc is pressed, but it doesn’t change text on the next line. Instead, it pushes to ahead of the current changes.

Vi Delete

x – Deletes a single character under the cursor

X – Deletes a single character before the cursor

dw – Deletes a single word that’s currently under the cursor, from the cursor position onward.

Vi Delete Line

dd – Vi delete line, regardless of the cursors position on the line

D – Deletes all text from the cursor position to the end of the line

dL – Deletes all text from the cursor position to the end of the screen

dG – Deletes all text from the cursor to the EOF

d^ – Deletes all text from the beginning of the line to the cursor

Vi Copy & Paste

Commands for Vi copy & paste:

yy – Vi copy line – copies a line of text to the unnamed buffer

3yy – Copies 3 lines of text to the unnamed buffer

yw – Copies a word (under the cursor) to the unnamed buffer

3yw – Copies 3 words to the unnamed buffer

P – Pastes the contents 0f the unnamed buffer to the right of the cursor

p – Pastes the contents of the unnamed buffer to the left of the cursor

Navigation Within a File

This may confuse you to start with,

H – This is the left arrow; it’s easy to remember because it’s the leftmost key in the four key set

J – Use this for the down arrow; I remember this by thinking of jown instead of down.

K – This is the up arrow; I remember this by thinking of kup for up.

L – Use this for the right arrow; I remember this as L is right, which I always thought sounded dumb, it’s alright on the right side of the keyboard…

Vi Page Down

Ctrl+F – Vi page down – Moves forward a page Ctrl+D – Moves forward half a page

Vi Page Up

Ctrl+B – Vi page up – Moves back a page Ctrl+U – Moves backward a half-page

Named and Unnamed Buffers

“ayy – Pulls a line the named buffer (a), overwriting the current contents

“Ayy – Appends the current line to the buffer

“A3yy – Pulls three lines from the current cursor position and appends the lines to the A buffer

“ap – Pastes the a buffer to the right of the cursor (the case of the buffer letter is meaningless)

Vi Search

How to perform a Vi Search.

N – Vi Search forward

Shift+N – Search Backward

Vi Search and Replace

:s/bob/BOB/ – Replaces the first instance of bob with BOB

:s/bob/BOB/g – Replaces all instances of bob with BOB in that line (note g stands for global)

:%s/bob/BOB/g – Replaces all instances of bob with BOB in that file no matter how many exist or how many changes made to each line

Vi Search for Part of a Word

A fuzzy search allows you to find something that you only know part of, for example if you wanted to find all instances of lines starting with the word “Uber” you would use the following:

1
/^Uber

To find all instances of the word “ninja” at the end of a line you would use:

1
/ninja$

In some instances you’ll need to find what’s called a metacharacter. For example, say you wanted to find the instances in a file for the asterisk character (*), because it stands for many characters. You could use something like this:

1
/The \* character

Another example might be finding the text ninja, with the period being treated only as a period. Otherwise, you’d find ninjas, ninja?, ninja! and so on. To find JUST ninja you would use the following:

1
/ninja\.

Finally, matching a range of characters is handy, such as trying to find all instances of the version number string v2.9. You either have to perform several searches of use something like this:

1
/v2.[1-9]

The square brackets denote a single character, stretching from the first character to the one after the dash. If you wanted instead to find all versions of the word the, including THE, THe and tHE, you would use the following:

1
/ [tT] [hH [eE]

Options in Vi

set number

set tabstop=5

set noh1search

The above code should be placed in the .exrc file which is located in the users home dir.

There are more than 60 options available in vi, to view them all type

1
:set all

To find out about an options status type

1
:set optionname?

:set number – turns on line numbers

:set nonumber – turns the number option off

Advanced Vi commands

How to run external commands in vi:

Say for example you want to run “ls -l” inside of vi as you can’t remember a file name, you would enter:

1
:! ls -l

Pressing enter or command will return you to the vi session. If the output is more than one screen it is piped to the more command.

Joining lines in vi

Back space only works on current lines, so to join lines in vi you need to position the curser in either line and press Shift+J

Split windows in vi

When you are editing a file and want to see a different section of the file or even a different file altogether, you can use the following:

:split – This splits the window horizontally

:vsplit – this splits the file vertically, with the same file on each side

To switch between the windows, hit Ctrl+W

To edit two files in vi at the same time, open the first file and then type:

1
:split file2

To set the hight of the split window:

1
:15split /blah/file

The above will split the top 15 lines of the screen and display the contents of the /blah/file.

To close the split window, take focus by hitting Ctrl+W and then enter :close

Or to close all the other split windows, take focus of the main window and enter:

1
: only

This will close all other windows apart from your window :p

Vi Save

:w – Vi Save, write the file out to disk

Vi Save & Exit

:q – Vi exit – this will close Vi

:wq – Vi save & exit

: x – Vi exit, and prompts it you want to save on exit.

Shift+ZZ – Alternative way to save and exit Vi

:q! – Exits vi and discards and changes you made

:wq! – Vi Save and exit if you are root and do not have the write bit set for the file you are attempting to write.

Misc / Additional

U – Vi Undo, easy to remember, enter U in command mode to undo the last command.

:+X+! – In command mode this will undo everything you have done since the last disk write.

Ctrl+G – Shows the file name, total number of lines and the current position expressed as a percentage of the total number of lines in the file.

Multipliers

Just about any keystroke or action can be done multiple times by prefixing it with a number.

For example to move the curser to line 5 you would press 5g. To move 12 words to the right you would enter 12W.

Comments