Translate into your own language

Sunday, May 1, 2016

Working with Vi Editor

Vi Editor stands for Visual Interactive or Visual Interface. Vi editor is one of the most power full text editor around.

Invoking  Vi Editor

Vi is typically started with file name as an argument. If you are editing an existing file you would use either the relative of absolute path of the file name.

Example:

[oracle@host1 ~]$ vi test.sh




If you want to make a new file you can type vi with a new file name as an option. In this case you will see a blank screen in vi.

In both these cases we see that vi uses the tilde character(~) to represents lines past the end of the file you are editing. That is so you can tell blank lines from lines which are not part of your file.

You can type vi without giving a file name. That will start vi with a new file which you can then name when you save.

While there are several options for vi, you will probably find that you rarely need them. Most of the magic of vi happens by using commands within the editor.

Switching modes

As far as day to day usages goes, vi has two modes, insert and command modes. Insert mode allow you to enter text into your file while command mode lets you perform operations like searching, saving, moving, around the file, and quitting vi. When you initially start vi you will be in command mode.

There are several ways to go from command to insert mode depending on what you want to do and where you want to do it.

The escape key is the preferred way to go from insert mode to command mode. Below are the few common ways to switch modes.

Command
Action


i
Insert text to the left of the cursor position
a
Insert text to the right of the cursor position
I
insert text at the beginning of the current line
A
Insert text at the end of the current line
o
Start inserting on a new line below the current line
O
Start inserting on a new line above the current line
Escape
Return to the command line



It is important to note that these commands are case sensitive. Typically the upper case version command has a similar effect to the lower case version but it is never exactly the same.
When in doubt about your mode the safest thing to do is to press the escape key. That will always return you to command mode.

Moving around

While in command mode you can typically use the arrow keys to move around a file. If the arrow keys are not working, which is due to a problem with the terminal emulation, you can still get around using  h, j, k and l keys. While it may seem tedious to use theses keys, they offer a needed substitute for when the arrow keys are not working.

Beyond using these substitutes there are also several other ways to more efficiently move the cursor around in vi. Here are some of the most useful commands for moving around:

left arrow, right arrow
left or right one character
up and down arrow
up or down one line
h
left one character
l
right one character
j
down one line
k
up one line
$
to the end of the current line
^
to the begining of the current line
w
to the beginign of the next word
b
to the beginning of previous word
G
to the end of the file
25G
to line 25(any number can be used instead of 25)
enter
to the beginning of the first word of the next line


You might notice that adding a number before the G command changes its behavior. Many vi commands can be preceded with a number to effectively execute the command that number of times. For example, the command 8downarrow would move the cursor down eight lines, just as if the down arrow wre pressed eight times. Similarly 5w would move the cursor to the beginning of the word five words to the left.

Making and undoing changes

There are several ways to make changes in vi. Some will delete or change a single character while other work on entire work or row. Like the others, you will need to be in command mode to use these commands. Here are some common commands for making changes to text in vi:


x
delete once character
r
Replace once character with the next character typed
dw
delete from the curson postion to the end of the word
D
Delete from the current character to end of the line
cw
same as dw then enter insert mode
dd
delete the current line
cc
clear the text on this line and enter inset mode
C
delete from the current cursor position end of to the current line and enter insert mode
j
Join the next line to the current line
 u
undo the last change. Can be repeated to the past sereral changes


Many of these commands can be preceded with a number to repeat the command that number of times. Even the undo (u) command can be preceded by a number to undo several changes at once.

Coping and pasting

Vi offers many options for copying and pasting including the ability to copy and paste from multiple buffers, but we will just touch on the basis copy and paste commands.

yy
copy the current line into the default buffer
7yy
copy the current line plus the next six lines to the default buffer
p
paste the content of the default buffer below the current line
P
paste the content of the default buffer above the current line

If you have copied multiple lines with the yy command you do not have to specify the number of lines when pasting. The paste command will automatically paste the entire contents of the default buffer.

Searching and replacing

Searching for text and replacing text in vi cab be very handy but it is not as easy as you might think. Here are some of the more useful search and replace commands in vi.

/search text
find and move the cursor to the next occurrence of the search text
?search text
find and move the cursor to the previous occurrence of the search text
n
repeat the last search
N
repeat the last search but in the opposite direction
:%s/search text/replace text
find all occurrence of search text and replace it with the replace text

When these commands encounter the end of the file they will wrap around to the beginning and continue searching. If searching backwards they will wrap from the beginning to the end. You probably noticed the find and replace command is very complicated.

Saving and editing

you will want to save  and exit our work often and when you are done you will need to get out of vi.as with other things in vi, there are several ways to do this:

:w
save changes
:w file name.txt
save changes to filename.txt instead of the file we opend
:q
exit vi(changes should have already been saved)
:q!
exit vi without saving changes
:wq
save changes and exit vi
ZZ
save changes and exit vi (same as :wq)
:w!
write changes despite read only permission(must be file owner)






No comments:

Post a Comment