There are many ways to edit files in Unix and for me one of the best ways is using screen-oriented text editor vi.
This editor enable you to edit lines in context with other lines in the file.
Now a days you would find an improved version of vi editor which is called VIM. Here VIM stands for Vi Improved.
Its usually available on all the flavors of Unix system.
Its implementations are very similar across the board.
It requires very few resources.
It is more user friendly than any other editors like ed or ex.
Moving within a File:
To move around within a file without affecting your text, you must be in command mode (press Esc twice). Here are some of the commands you can use to move around one character at a time:
Command
Description
k
Moves the cursor up one line.
j
Moves the cursor down one line.
h
Moves the cursor to the left one character position.
l
Moves the cursor to the right one character position.
There are following two important points to be noted:
The vi is case-sensitive, so you need to pay special attention to capitalization when using commands.
Most commands in vi can be prefaced by the number of times you want the action to occur. For example, 2j moves cursor two lines down the cursor location.
There are many other ways to move within a file in vi. Remember that you must be in command mode (press Esc twice). Here are some more commands you can use to move around the file:
Command
Description
0 or |
Positions cursor at beginning of line.
$
Positions cursor at end of line.
w
Positions cursor to the next word.
b
Positions cursor to previous word.
(
Positions cursor to beginning of current sentence.
)
Positions cursor to beginning of next sentence.
E
Move to the end of Blank delimited word
{
Move a paragraph back
}
Move a paragraph forward
[[
Move a section back
]]
Move a section forward
n|
Moves to the column n in the current line
1G
Move to the first line of the file
G
Move to the last line of the file
nG
Move to nth line of the file
:n
Move to nth line of the file
fc
Move forward to c
Fc
Move back to c
H
Move to top of screen
nH
Moves to nth line from the top of the screen
M
Move to middle of screen
L
Move to botton of screen
nL
Moves to nth line from the bottom of the screen
:x
Colon followed by a number would position the cursor on line number represented by x
Control Commands:
There are following useful command which you can use along with Control Key:
Command
Description
CTRL+d
Move forward 1/2 screen
CTRL+d
Move forward 1/2 screen
CTRL+f
Move forward one full screen
CTRL+u
Move backward 1/2 screen
CTRL+b
Move backward one full screen
CTRL+e
Moves screen up one line
CTRL+y
Moves screen down one line
CTRL+u
Moves screen up 1/2 page
CTRL+d
Moves screen down 1/2 page
CTRL+b
Moves screen up one page
CTRL+f
Moves screen down one page
CTRL+I
Redraws screen
Editing Files:
To edit the file, you need to be in the insert mode. There are many ways to enter insert mode from the command mode:
Command
Description
i
Inserts text before current cursor location.
I
Inserts text at beginning of current line.
a
Inserts text after current cursor location.
A
Inserts text at end of current line.
o
Creates a new line for text entry below cursor location.
O
Creates a new line for text entry above cursor location.
Deleting Characters:
Here is the list of important commands which can be used to delete characters and lines in an opened file:
Command
Description
x
Deletes the character under the cursor location.
X
Deletes the character before the cursor location.
dw
Deletes from the current cursor location to the next word.
d^
Deletes from current cursor position to the beginning of the line.
d$
Deletes from current cursor position to the end of the line.
D
Deletes from the cursor position to the end of the current line.
dd
Deletes the line the cursor is on.
As mentioned above, most commands in vi can be prefaced by the number of times you want the action to occur. For example, 2x deletes two character under the cursor location and 2dd deletes two lines the cursor is on.
I would highly recommend to exercise all the above commands properly before proceeding further.
Change Commands:
You also have the capability to change characters, words, or lines in vi without deleting them. Here are the relevant commands:
Command
Description
cc
Removes contents of the line, leaving you in insert mode.
cw
Changes the word the cursor is on from the cursor to the lowercase w end of the word.
r
Replaces the character under the cursor. vi returns to command mode after the replacement is entered.
R
Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
s
Replaces the current character with the character you type. Afterward, you are left in insert mode.
S
Deletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode.
Copy and Past Commands:
You can copy lines or words from one place and then you can past them at another place using following commands:
Command
Description
yy
Copies the current line.
yw
Copies the current word from the character the lowercase w cursor is on until the end of the word.
p
Puts the copied text after the cursor.
P
Puts the yanked text before the cursor.
Advanced Commands:
There are some advanced commands that simplify day-to-day editing and allow for more efficient use of vi:
Command
Description
J
Join the current line with the next one. A count joins that many lines.
<<
Shifts the current line to the left by one shift width.
>>
Shifts the current line to the right by one shift width.
~
Switch the case of the character under the cursor.
^G
Press CNTRL and G keys at the same time to show the current filename and the status.
U
Restore the current line to the state it was in before the cursor entered the line.
u
Undo the last change to the file. Typing 'u' again will re-do the change.
J
Join the current line with the next one. A count joins that many lines.
:f
Displays current position in the file in % and file name, total number of file.
:f filename
Renames current file to filename.
:w filename
Write to file filename.
:e filename
Opens another file with filename.
:cd dirname
Changes current working directory to dirname.
:e #
Use to toggle between two opened files.
:n
In case you open multiple files using vi, use :n to go to next file in the series.
:p
In case you open multiple files using vi, use :p to go to previous file in the series.
:N
In case you open multiple files using vi, use :N to go to previous file in the series.
:r file
Reads file and inserts it after current line
:nr file
Reads file and inserts it after line n.
You can use vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read
a text file
Ex mode commands:
q Quit set <x> Enable option
q! Quit, discard changes set no<v> Disable option
r <f> Read in file <f> set all Show all options
sh Invoke shell
vi Vi mode
wq Write and quit
w <f> Write file <f>
w! <f> Overwrite file <f>