Simple Vim Cheat Sheet

Simple Vim Cheat Sheet

Simple Vim Cheat Sheet

Recently I was editing a lot of .conf files inside of  a CentOS server. I couldn’t remember some commands and needles to say re-typing out certain sections of code over and over again wasn’t something I was wanting to do. So I did some Google searching and gathered some helpful commands you can use when working with vi/Vim to speed up your process.

Common Commands

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

Vim 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.

Vim 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.

Vim 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

Vim 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

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…

Vim Page Down

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

Vim 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)

Vim Search

N – Vi Search forward

Shift+N – Search Backward

Vim 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

Vim Save

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

Vim 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.

 

 



Leave a Reply