• No results found

Lab No. 05: Command line Text Editors (Vim)

Objective:

To enable students to use command line editors for editing text files. Completing this lab, students will give sufficient knowledge about vim / vi and its features for their daily tasks.

Scope:

On the completion of this lab, you will be able to:

• Start the editor, moving cursor around and exiting the editor.

• Work in command mode.

• Copy, Cut and past the text

• format program and check source code

• Search Character/string.

• Recover Your Work.

Useful Concepts:

vi

• A screen-based editor used by many UNIX users.

• Has a powerful feature to aid programmers.

Editing modes

There are two editing modes that the editor can be executed in.

• Insert mode

User can type text in this mode

• Command mode

User can use commands for operation on the file. (e.g. saving, exiting, searching, etc) Text Buffers in VI

• There are 36 special buffers and also a general purpose buffer for temporary storage of words or. When a block of text is cut from the file it is placed into the general purpose buffer but, if specified, the block of text can also be stored in another buffer. Buffers are numbered from a-to-z and from 0-to-9.

Exercise-1:- This exercise illustrates how to start the editor, move cursor around and exit form the editor.

To start the editor from command line, type vi

An empty file will be opened in the editor. If you want to open an existing file in the editor, type vi filename

To switch to 'Insert' mode, press:

i key

-- INSERT -- key word at the bottom will indicate that you are in an inserting mode and your cursor will start blinking, to go to the start of the line. Press

a

at command mode will take you to the current cursor position and you can enter any text.

To move cursor around while you are in command mode, press h key move left,

l key move right, k key move up, j key = move down

you can also use arrow keys while in insert mode to do the same tasks.

To switch back to command mode, press Escape key

While in command mode, each command is preceded by colon ":"

To exit the editor without saving changes made to the file, switch to command mode and type, :q!

To save changes and exit the editor, switch to command mode and type the command, :wq

Where "w" is for writing changes to the file and "q" is to quit or exit the editor

Exercise-2:- This exercise illustrates how to Work in command mode in vi edtor.

Press Escape key to switch to command mode and practice the following commands and observe the re-sults.

To replace one character under the cursor, type:

r

to undo the last change to the file. Typing u again will re-do the change.

u

To delete character after the current cursor position x

Exercise-3:- This exercise illustrates how to Copy, Cut and paste the text To cut text from the file, type,

d

to cut multiple characters, type:

NUMd

wher NUM is the numper of characters to cut to cut the whole line type:

dd

to cut from current cursor position to the beginning of the line,type.

d^

to cut from current cursor position to the end of the line.

d$

to cut from current cursor position to the end of the word.

Dw

To cut three lines from current cursor position downwards.

3dd

You can use the y command which similarly to the d command which is used just to copy the text.

To paste the buffer after the cursor position, type:

p

to paste the buffer before the cursor position type:

P

To paste the text for the specified number of times, say 3, before the cursor position, type:

3P

Exercise-4:- This exercise illustrates how to format a program and check source code for for correctness.

You can use variable to set up the indentation for each level of nesting in code.

To set the shift width to 4 characters, type:

:set sw=4

To Shift the current line one shift width to the left, type:

:<<

To Shift the current line three shift width to the right, type:

:3>>

To check for matching parenthesis or brace in your code, place the cursor onto a parenthesis or brace and type:

:%

Exercise -5:- This exercise illustrates how to Search Character/string in a file using vi.

To search a string forwards in the file, say "comsats", type:

/comsats

To search a string backwards in the file, say "comsats", type:

? comsats

To repeat the previous search in the same direction, type:

n

To repeat the previous search in the same direction, type:

N

To search special characters in the file, type a backslash (\) before the character.

Some special characters that are used in search expression and their meaning:

^ Beginning of the line. (At the beginning of a search expression) . Matches a single character

* Matches zero or more of the previous character.

$ End of the line, (At the end of the search expression)

[ ] Starts a set of matching, or non-matching expressions... For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad

< > Put in an expression escaped with the backslash to find the ending or beginning of a word. For example: /\<the\> should find only word the, but not words like these: there and other.

There are two sets of commands used for character search. [f & F] and [t & T]

To search for a character forwards within one line, say 'j', type:

/fj Or /tj

To search for a character backwards within one line, say 'j', type:

/Fj Or /Tj

; or , command can be used to repeat these two sets of commands.

; repeats the last character search command in the same direction, , repeats the command in the reverse direction

Exercise -6:- This exercise illustrates how to recover Your Work:

The VI editor keeps a temporary copy of your edited file, and when you save your work, it puts the con-tents of the temporary copy into the original file. If file is closed accidentally during editing, the VI editor will attempt to save your work for you to recover later.

To recover the file named myfile, start the editor with –r option for recovery vi -r myfile

and save the recovered file then

Note: The -r option only works once per failed VI session.

Exercises for lab:

Exercise-1: Open a file in vi editor and practice all the commands given below.

^B ^D ^F ^H ^J ^M ^N ^P ^U $ % ^

( ) { } | + - _ 0 B E

G H L M W b e h j k l

w

Note the effect of the options on the output.

Home Work:

1) Practice the following cursor movement commands and write a note on them:

^E ^Y z C

2) Practice the following text replacement commands and write a note on them:

R S c r s

3) Practice the following search commands and write a note on them:

, / ; ? F N T f

n t

4) Practice the following formatting commands and write a note on them:

~ < > J