Using VI Text Editor

Text editors are programs used to create or edit files. One of the most popular editors on Linux/Unix systems (also available for other platforms) is vi. Though it does take some time getting use to, this easy to follow HowTo will walk you through the essentials of using vi.


Starting the vi text editor

The command to start the vi editor is simply vi, followed by the filename. For example to edit a file named linuxlookup, you would type vi shineservers and then return.

New files will be empty, where as existing files will be loaded into the text editor. When creating a new file you will see a screen filled with tilde characters (~) down the left side of the screen, these represent blank lines beyond the end of file. At the bottom of your screen, the filename will be shown, along with a line and character count. These come in handy for referencing.

Modes of Operation

Vi has two modes of operation, "command mode" and "insert-text mode". You're always in command mode when vi first starts. This command mode allows you to issue a desired action. While insert-text mode simply enters anything you type into the file. Through-out this document we're going to provided lists of frequently used commands, be sure to pay attention to case sensitivity as it does matter and can have different results.

KeystrokeDescription
EscHit the "Esc" (Escape) key at any point in time to enter command mode. If you were already in the command mode when you hit "Esc", don't worry. It might beep, but you will still be in the command mode.
Note: When in insert-text mode, you should see -- INSERT -- at the bottom of the window.

Properly exiting vi

In order to exit vi, you must first be in command mode. Type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, you may force quit command without saving. This allows you to view a file without actually saving any changes.

KeystrokeDescription
:qQuits the session.
:q!Forces a quit the session and does not save changes.
:wWrites (saves) changes to current file.
:wqWrites changes to current file and then quits.
:wq!Forces a write to current file and then quits.
Note: Files are not saved unless you issue the command to do so, thus closing the terminal to exit will result in the lost of any changes made to the file.

Inserting

KeystrokeDescription
iInserts text to the left of the cursor.
IInserts text at the beginning of the line, no matter where the cursor is positioned on the current line.
aBegins inserting after the character (append) on which the cursor is positioned.
ABegins inserting at the end of the current line, no matter where the cursor is positioned on that line.
oBegins inserting text on a new, empty line, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.
OBegins inserting text on a new, empty line, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.
rReplace single character under cursor position.
RReplace all characters, starting under cursor position, until Esc is pressed.

Deleting, Copying and Pasting

KeystrokeDescription
DDelete to end of the line.
ddDelete line at cursor position.
xDelete character right of the cursor position.
XDelete the character before the cursor.
d$Delete to the end of the line, including the current character.
ndwDelete to the beginning of the line, excluding the current character. (n is a number)
ndWDelete a word(s), stopping at punctuation.
ndeDelete to the end of next word.
nddDelete a line(s).
yYank (copy) the text at cursor position.
yyYank (copy) the line at cursor position.
pPaste after the current cursor position.
PPaste before the current cursor position.

Navigating

KeystrokeDescription
hMove cursor to the left one character.
lMove cursor to the right one character.
jMove cursor down one line.
kMove cursor up one line.
^Move cursor to the beginning of the line.
$Move cursor to the end of the current line.
GMove cursor to the last line of your file.
1GMove cursor to the first line of your file.
wMove cursor forward to the next word, stopping at punctuation.
WMove cursor forward to the next word, ignoring punctuation.
eMove cursor forward to the end of the word, stopping at punctuation.
EMove cursor forward to the end of the word, ignoring punctuation.
:nn is the line number you'd like to jump to.
:set numberShow line numbers down the left side of the window.
Note: The arrow keys can be used for basic navigation.

Search and Replace

KeystrokeDescription
fSearch the current line for the character specified after the 'f' command. If found, move the cursor to the position.
FSearch the current line backwards for the character specified after the 'F' command. If found, move the cursor to the position.
;Repeat the last f or F command (see above).
,Repeat the last f, F, t or T command in the reverse direction.
tSearch the current line for the character specified after the 't' command, and move to the column before the character if found.
TSearch the current line backward for the character specified after the 't' command, and move to the column before the character if found.
/Search the file downwards for the string specified after the /.
?Search the file upwards for the string specified after the ?.
nRepeat last search given by '/' or '?'.
:s/old/newThis will find the first occurrence of "old" and replace it with "new".
:s/old/new/gThis will find globally (all) occurrences of "old" on the current line and replace them with "new".
%s/old/new/gThis will find every occurrence of "old" in the file and replace them with "new".
%s/old/new/gcSame as above but asks for confirmation before changing.

  • 10 Users Found This Useful
Was this answer helpful?

Related Articles

Setting up a Linux Streaming Server

Linux, streaming server Linux, allows you to stream video, audio or other media on your website....

How to Install LiteSpeed on a WHM/cPanel Server

Here’s how you install LiteSpeed on a WHM/cPanel Server – it should take about 25minutes from...

Change the default SSH port

The Secure Shell (SSH) Protocol by default uses port 22. Accepting this value does not make your...

Reset a MySQL root password

So you've managed to misplace your root login for a MySQL database? Not to worry, the following...

Internal Server Error while accessing cPanel, WHM & webmail

When accessing cPanel, WHM & webmail at times you get a error as below. Performing upcp OR...

Powered by WHMCompleteSolution