A Look at the VI Editor

VI is the widely used text editor in UNIX operating systems. I have had the chance to use the VI editor in the Solaris and Linux operating systems. In this article we are going to learn how to use the VI editor.

VI in Linux and Solaris is almost the same. The main difference I encountered is the arrow keys. In UNIX-based versions you have to use h, j, k and l for left, down, up and right, moving one character/line at a time. But in Linux, we can use the standard arrow keys for editing.

In the Solaris OS, if you use the standard arrow keys during editing you will see some unwanted outputs if the arrow keys are not mapped correctly. Sometimes it may display the error "Terminal too wide" if your terminal is wider than the typical 1024 x 768.

Open a new file/open for editing: To open a new file just use the vi command with a filename like the example below.

vi newfile.txt

This command will open the VI editor with a new file named newfile.txt in the current directory. If a file named newfile.txt is present in the current directory, then the above command will open that file for editing.

{mospagebreak title=Saving and Closing a file}

Use :w for saving the content and :q for quitting. Here are some examples.

:w

Saves current file but doesn’t exit

:w file

Saves current content as file but doesn’t exit

:n,mw file

Saves lines n through m to file

:n,mw >>file

Saves lines n through m to the end of file

:q

Quits VI and may prompt if you need to save

:q!

Quits VI without saving

ZZ or :wq

Saves and exits VI

VI has two modes; one is the command mode and the other is the edit mode. To change to the edit mode you have to use i (insert) or a (append). Use the ESC character to change back to command mode.

Inserting texts: Use i, I, a, A, o or O for insertion as described in the following table.

i

Insert before cursor

I

Insert before line

a

Append after cursor

A

Append after line

o

Open a new line after current line

O

Open a new line before current line

{mospagebreak title=Cursor motion/movements}

To move your cursors in VI use the following commands (a long list!).

h

Move left

j

Move down

k

Move up

l

Move right

w

Move to next word

W

Move to next blank delimited word

b

Move to the beginning of the word

B

Move to the beginning of blank delimited word

^

Moves to the first non-blank character in the current line

+

Moves to the first character in the next line

-

Moves to the first non-blank character in the previous line

e

Move to the end of the word

E

Move to the end of blank delimited word

(

Move a sentence back

)

Move a sentence forward

{

Move a paragraph back

}

Move a paragraph forward

0 or |

Move to the beginning of the line

$

Move to the end of the line

:n

Move to nth line of the file

For jumping to the beginning of the document use [[, and use ]] to jump to the end of the document.

Deleting texts: To delete content use d and x. Use x for deleting character to the right of the cursor. Use d with the above operands (motion/movement operands) for deleting. To delete a word use dw (next word). Delete is basically cut, i.e. you can paste the deleted contents. The following are some other delete commands.

x

Delete character to the right of cursor

nx

Deletes n characters starting with the current one; omitting n deletes current character only

X

Deletes the character to the left of cursor

nX

Deletes the previous n characters; omitting n deletes the previous character only

D

Delete to the end of the line

D$

Deletes from the cursor to the end of the line

dd or :d

Deletes the current line

ndw

Deletes the next n words starting with the current word

ndb

Deletes the previous n words starting with the current word

ndd

Deletes n lines beginning with the current line

:n,md

Deletes lines n through m

Tips: You can edit multiple lines better if you know the line numbers. To show line numbers use the following command.

:set nu

For example, to go to line number 5 use :5

{mospagebreak title=Undo editing}

Use u for undoing the last command. For undoing the last edit on the current line only, unless you move off the line you were editing. use U.

Tips: To perform the last change, use . (period once).

Copying and pasting: You can paste the content you deleted, since the deleted contents are kept in a buffer. Use p to paste. Use yy to copy the current line or use :y for copying (or yanking) the current line, and use yw to copy the buffer contents ("yw" for "yank work"). You can use yyp together to copy and paste the current line. Use p to put the buffer contents after the position or after the line. Use P to put the buffer contents before the position or before the line.

You can copy the content in different buffers from a to z, and you can also paste from these buffers by specifying the buffer before the paste command like (a-z)p or (a-z)P. Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. For example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.

Searching: Use / for searching in the forward direction and, use ? for searching in the backward direction. The following are some examples.

/str

search down for "str" in forward direction

?str

search up for "str" in backward direction

You can ignore cases during searching by using the set command.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye