vi Text Editor Quickstart Guide

vi is the default text editor that comes with the Unix operating system.

There are two modes of operation for this editor:

  • Command mode
  • Insert mode

In command mode, every character typed is a command that does something to the text file being edited; typing a specific character in command mode will cause the editor to enter in insert mode. In insert mode, every character typed is added as text in the file. Pressing the ESC key turns off the insert mode.

Here are a handful of vi commands that should be sufficient for beginning vi users.

Starting vi

In the unix prompt

$ vi <filename>

Exiting

:x <ENTER> save file and quit
:wq<ENTER> save file and quit
:q!<ENTER> quit without saving latest changes

Moving the Cursor

j or <ENTER> or <DOWN-ARROW> move cursor down one line
k or <UP-ARROW> move cursor up one line
h or <BACKSPACE> or <LEFT-ARROW> movecursor left on character
l or <SPACE BAR> or <RIGHT-ARROW> move cursor right on character
0 move cursor to start of current line
$ move cursor to end of current line
w move cursor to beginning of next word
b move cursor to back to beginning of preceding word
:0<ENTER> or 1G move cursor to first line in file
:n<ENTER> or nG move cursor to line n
:$<ENTER> or G move cursor to last line in file

Screen Manipulation

<CTRL> f move forward one screen
<CTRL> b move backward one screen
<CTRL> d move down one half screen
<CTRL> u move up one half screen
<CTRL> l redraws the screen
<CTRL> r redraws the screen, removing deleted lines

Adding, Changing, and Deleting Text

u undo

Inserting or Adding Text

i insert text before cursor until <ESC> is pressed
I insert text at beginning of current line until <ESC> is pressed
a append text after cursor until <ESC> is pressed
A append text to end of line until <ESC> is pressed
o open and put text in a new line below current line until <ESC> is pressed
O open and put text in a new line above current line until <ESC> is pressed

Changing Text

r replace single character under cursor
R replace characters starting with current position of cursor until <ESC> is pressed
cw change the current word with new text starting with the character under the cursor until <ESC> is pressed
cNw change N words beginning with character under the cursor until <ESC> is pressed e.g. c5w changes 5 words
C change (replace) the characters in the current line until <ESC> is pressed
cc change (replace) the entire current line, stopping when <ESC> is pressed
Ncc or cNc change (replace) the next N lines, starting with the current line, stopping when <ESC> is pressed

Deleting Text

x delete single character under the cursor
Nx delete N characters starting with the character under the cursor
dw delete the single word beginning with character under the cursor
dNw delete N words beginning with character under the cursor e.g d5w deletes 5 words
D delete the remainder of the line, starting with the current cursor position
dd delete the entire current line
Ndd or dNd delete N lines beginning with the current line e.g. 5dd deletes 5 lines

Cutting and Pasting Text

yy copy (yank, cut) the current line into the buffer
Nyy or yNy copy (yank, cut) the next N lines including the current line into the buffer
p put (paste) the lines(s) in the buffer into the text after the current line

Searching Text

/string search forward for occurrence of string in text
?string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction

Determine Line Numbers

:.= shows line number of current line at bottom of screen
:= shows the total number of lines at bottom of screen
<CTRL> g shows current line number, and total number lines in the file at the bottom of the screen

Saving and Reading Files

:r filename<ENTER> read file named filename and insert after current line
:w<ENTER> write current contents to file named in original vi call
:w newfile<ENTER> write current contents to a new file named newfile
:M,Nw newfile<ENTER> write the contents of lines numbered M to N to a new file named newfile
:w! filename<ENTER> write the current contents over a existing file named filename

Related posts:

  1. Samba on Ubuntu Quickstart Guide
  2. WordPress Quickstart Guide
  3. Setting Up Log4j Quickstart Guide
  4. MySQL for Ubuntu Quickstart Guide
  5. Gallery2 Quickstart Guide
This entry was posted in Ubuntu and tagged . Bookmark the permalink.

Comments are closed.