Win Unix Mac

Articles,How Tos,Tips n more

  • Increase font size
  • Default font size
  • Decrease font size

Unix Vi Command Cheat Sheet

E-mail Print PDF

Unix Vi Cheat Sheet -  Please add your favorites that I missed or made a mistake with, or if there is a command I do not know, please add it as a comment !

These are the most useful commands in Vi I personally use, made into a cheat sheet for you. Although Vi is normally the standard editor on unix some people find it a little intimidating or user unfriendly, let me assure you that this is just growing pains. Vi is a mans editor, made by men for men - Its worth mastering !

nano, pico and other simpler editors will help you do it quickly, but they also do not work well with large files and other issues, so step up to the game and learn more about Vi with this easy to use cheat sheet. (Sekas that is for you, you know who you are !)

There are 3 modes of operation for Vi, I will mention them quickly for clarity:

command mode - Navigation of the document being edited and able to use commands in the mode.

insert mode - Able to input text into the document via the keyboard, still available to move around the document.

last-line mode -  ( I dont think i have ever used it ! )

So with just a few of these commands mastered you will look and feel like a seasoned Unix pro !

File commands


:w -    Write the contents of the file out to disk under the current file name (save)
:wq -    Write the contents of the file out to disk under the current file name and quit (save and quit)
:q -    Quit - You may or not be asked if you want to save in this case, depends on your distribution and how Vi is configured
:q! -     Quit forcefully, without asking the save question, this ! can be used with other commands for the same purpose, e.g. :w! or :wq!
ZZ -    Write the contents of the file and quit ( 2x capital Z, same as :wq, notice you do not need to enter command mode first )

Execute commands from within Vi

!command        -      Fire or 'bang' a command inside Vi, useful for looking at or working with another file as well in Vi,  examples below

!ls -l ~/public/*txt -     List the content of your ~/public/*txt from within vi
!cat  ~/.ssh/rsa_id.pub -     cat your ssh pub key
!ifconfig -a|grep -i "address" -    Displays current machines ip interface addresses listed and filtered with grep

Search commands


/this -    finds the next "this", case sensitive, in a forwards (to the right) direction
?that -    finds the next "that", case sensitive, in a backwards direction
n -    Goto the next search result

Search and replace commands

%s/this/that/g
-       Replaces "this" with "that" globally, throughout the document
%s/this/that/ -       Replaces "this" with "that" on the first occurence

Editor commands

i -    Insert
o -    Open a new line below
O -    Open a new line above
a -    Append
A -    Append at the end of the line
u -    Undo
. -    Repeat the last command issued
d$ -    Delete to the end of the current line
d0 -    Delete to the beginning of the current line
x -    Delete a character
5x -    Delete 5 characters
dd    -    Delete the current line
9dd    -    Delete 9 lines

Cut and Paste equivelant commands
( called yank + paste here )

yy -    Yank (copy) a line
2yy -    Yank 2 lines
dd -    Delete a line
2dd -    Delete 2 lines
p -    Paste on the line above ( lower case p )
P -    Paste on the line below ( upper case P )

Navigation through the document

Up -    Cursor up key, goes up
2Up -    5 Then Cursor up, goes 2 lines up
Down -    Cursor down key, goes down
4Down -    4 Then Cursor down, goes 4 lines down
w -    Goto the next word in the document
2w -    Go forward 2 words in the document
b -    Go backwards one word
0 -    Goto the beginning of the file (zero)
$ -    Goto the end of the current line
1G -    Goto line #1
10G -    Goto line #10
G -   Goto the end of the document
L -    Goto the lower part of the visible screen
M -    Goto the middle part of the visible screen
H -    Goto the higher or top part of the visible screen

Last Updated on Monday, 07 September 2009 17:29  

Add your comment

Your name:
Subject:
Comment:

yvcomment, category: "Unix How To's"