Monday, January 14, 2013

Tutorial:Simplifying vi (editor) in Linux

The aim of this tutorial is to simplify the use of vi editor in unix/linux and to unleash its power with a combination of few keystrokes and commands.

The vi editor(or visual editor) is a screen editor which is available on almost all the unix systems.Its a fast and a powerful editor. vi has no menus but instead uses a combination of keystrokes in order to accomplish commands.A beginner might find it difficult to edit files in vi editor,but at the same time it is fun to learn and use.One needs to remember the keystrokes.

Getting into vi

To begin with,on the unix/linux console type vi followed by a space and the filename.If you want to edit an existing file,type in its name or if you are creating a new file,type in the name you wish to give to the new file.Next,hit enter

vi modes

vi has two modes: the command mode and the insert mode.When in the command mode,letters of the keyboard will be interpreted as commands.Wen you are in insert mode the same letters of the keyboard will type or edit texts.The vi editor always starts in the command mode.Switching between the two modes is easy.Once you enter the vi editor,hit the letter i on the keyboard.You enter the insert mode.If you wish to leave the insert mode and return to the command mode,hit the ESC key on the keyboard.If you are not sure where you are,hit ESC a couple of times and you will be back to the command mode.

Please note that the vi editor is case sensitive.Thus, a i and I will be interpreted as two different commands.

Also note that the keystrokes in the command mode are not displayed on the screen.


Entering text

Suppose you created a new file using the vi command.Initially, you are in command mode.To enter some text in the file you need to first switch to the insert mode.Hit i.This brings you to the insert mode.When you start typing now,anything that you type will be displayed on screen and will be entered into the file.At the end of each line,vi will break the line and texts will move to the next line.

Cursor movement in a file

You must be in the command mode if you want to move the cursor to different positions in the file.If you are finished entering text into the file,its time to return back to the command mode.Hit ESC.This will bring you back to the command mode once again.

Moving one character at a time
The arrow keys can help you move one character at a time in the file.Alternatively,use these keys
h: one space to the left (equivalent of left arrow key)
l : one space to the right (equivalent of right arrow key)
j : one space down the current position (equivalent of down arrow key)
k : one space upward (equivalent to up arrow key)

Moving among words and lines
To make navigation quicker there are several 'key' commands which allow you to move word by word in a file.
w: moves the cursor forward one word.

b: moves the cursor backward one word.

W: moves the cursor forward one word(but ignores space or punctuations between words.To make out the difference use w and W seperately for navigating in your file.

B: moves the cursor backward one word(ignores punctuations)

0(zero): moves the cursor to the beginning of the current line.

$: moves the cursor to the end of the current line.

Screen movement
To move the cursor to a line within your current screen use these keys

H: moves the cursor to the top line of the current screen
M: moves the cursor to the middle line of the current screen
L: moves the cursor to the last line of the current screen

In case the file is a large one (say 1000 lines),it is not possible to display the whole file on a single screen.In this case we have to scroll through multiple screen to view the whole file.In such a situation use these keystrokes

ctrl+f : scrolls down one screen

ctrl+b : scrolls up one screen  

ctrl+u : scrolls up half a screen

ctrl+d : scrolls down half a screen

For moving to the beginning or end of the file, use the G command

G : to move the cursor at the end of the file.

1G : to move the cursor at the first line of the file.In general, if G is preceded by a number,it moves the cursor to the specified line in the file.For instance, 5G moves the cursor to the fifth line in the file, 17G moves the cursor to the 17th line in the file and so on.
Search as you move
A very efficient method to get to a specific position in a file is by searching for a particular text.In command mode,type a / followed by the text you want to search.When you hit enter, the cursor is placed at the first match in the file.Hit n to point the cursor to the next instance(if any,present in the file).

Editing a file

The file editing commands work in the command name.As mentioned earlier,the commands are case-sensitive.Also, some editing commands can be preceded by a number to indicate the number of repetitions of the command.

Deleting(or cutting) letters/words/lines 
To delete a character in the file,position the cursor to that particular character and use these commands

x : deletes the character under the cursor.
X : deletes the character to the left of the cursor.
dd : deletes the current line.
D : deletes from the current position of cursor to the end of that particular line where the cursor is placed.

Preceding the command with a number will repeat the command that number of times.For instance, 4x will delete the character on which the cursor has been placed followed by 3 more characters immediately succeeding it.Similarly, 4dd will delete four lines.

Pasting text using Put 
In case you want to paste or insert the deleted text to a specific location in file,you can use the Put command to do so.To use the command,place the cursor where you wish the deleted text to be inserted.Then use the p command to reinsert the text.If you are inserting a line or a paragraph use the lower case p to insert on the line below the cursor and use P to insert on the line above the cursor.

Copying or 'Yanking' text
If you want to copy an existing text in the file,you can use the yank command to do so.Yank copies the selected text into a buffer and holds it until another yank or deletion occurs.As usual,we can use it in command mode.The commands below will help you yank word(s) or line(s) in a file.

yw : copies the word(based on the current location of the cursor) to the buffer.
yy : copies the entire line to the buffer.

When a number precedes these commands say, 7yy ,it will copy 7 lines into the buffer(the current cursor position line and 6 lines following it).

The yanked text can be easily placed at some other position using the p(or P) command.

Replacing characters,words or lines
The following commands can help you replace characters,words or lines in your file.

r : replaces the current character(highlighted by the cursor) with the next character you type in.After the character has been typed,the control returns to the command mode.

R : puts you in the overwrite mode where things are overwritten as you type.Hit  ESC to exit this mode.

cw : changes and replaces the current word with the text you type in.The $(dollar) sign at the end of the typed text marks the end of the replacement text.

Appending text 
The append command can help you add text or content to your file.Append works much like insert except that it enters text after the cursor rather than before it.To use this command,place the cursor to the position where you want to add text and press a.Then continue typing in the needed content.Once done,hit the ESC key to return back to the command mode.

The Undo command
As the name suggests,the undo command can undo things for you. DO NOT move the cursor from the line where you made a mistake.(Before that hit ESC if you are in the insert mode).Then try these commands

u : undoes the last change you made anywhere in the file.Using u again will "undo the undo"

U : undoes all recent changes to the current line.

Some file handling

Saving and quitting
The command ZZ (uppercase) will allow you to quit the vi editor and save changes made by you to the file.Below are some of the commands which allow file handling in vi editor.Remember that you should be in command mode while executing these commands.Also,use the key combination shift+: before typing in any of these commands.

 wq : alternative to ZZ

e! : return to the last saved version of the current file.
q or q! : quit vi without saving the file.

w : write to the current file without exiting the vi editor.(use w! to force write)


Some Cool stuff!!  


Use these commands in the command mode to see how awesome is this vi stuff.Remember to use the key combination shift+: before using any of these commands.

sh : temporarily escape to the shell

^d : return back to vi from shell(read as ctrl+d)

![command] : execute a linux command without exiting the vi editor.Use ctrl+d to return back to the vi editor after executing the command.

r![command] : read output of the command to vi.

r[filename] : read a file to vi

n : open next file

^g : lists the current line number.

set number : show line numbers

set showinsert : show the flag "I" at bottom of screen when in insert mode.Can help you to identify in which mode you are working at any instant.

set tabstop=n : sets default tab space to number n.

1,15w filename : write lines 1 to 15 to any filename mentioned in the command.

15,$w >>file : writes the content from line 15 to the end of the file and appends it to the file mentioned.

That completes the tutorial folks.I might have missed out some things.However,i have embedded a 'cheat-sheet' of vi for quick reference.Check it out below...

No comments: