The "vi" editor is a screen-based editor used by many Unix users. The conventions used in here is Ctrl - Press control key.
Starting the VI Editor:
The "vi" editor lets a user create new files or edit existing files. The command to start the "vi" editor is vi, followed by an optional filename.
For example to edit a file called new.c, you would type vi new.c and then return. You can start "vi" without a filename, but when you want to save your work, you will have to tell "vi" which filename to save it into later.
When you start "vi" for the first time, you will see a screen filled with tildes (tilde : ~) on the left side of the screen. Any blank lines beyond the end of the file are shown this way. If you save the file , at the bottom of your screen, the filename should be shown, if you specified an existing file, and the size of the file will be shown as well, like this:
this is a test ~ ~ "filename" 1 line, 15 characters
If the file you specified does not exist, then it will tell you that it is a new file, like this:
~ ~"newfile" [New file]
If you started VI without a filename, the bottom line of the screen will just be blank when "vi" starts.
Getting Out of VI:
The VI editor has two modes; command mode and input mode. When "vi" first starts, it is in command mode. There are multiple commands that cause vi to enter input mode (a, A, i, I, o, R...). To reutrn to command mode, you press teh "Escape" key. (Usually the control-c key combination will also work.) Note that to exit "vi", you have to be in command mode. If you are unsure if you are in input mode or command mode, press thes "Esc" key to be sure. It does no harm to press it if you are in command mode.
The command to quit out of "vi" is :q. Once in command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of "vi" without saving is :q!. This lets you exit "vi" without saving any of the changes.
The command to save the contents of the editor is :w or :wq. The latter lets you save and quit. A faster version of the :wq command is the ZZ command. You can specify a different file name to save to by specifying the name after the :w (such as :w newfilename )
Simple VI Commands:
Here is a simple set of commands to get a beginning "vi" user started. There are many other convenient commands, which will be discussed in later sections.
a -> enter insert mode, the characters typed in will be inserted after the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.
h -> move the cursor to the left one character position.
i -> enter insert mode, the characters typed in will be inserted before the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.
j -> move the cursor down one line.
k -> move the cursor up one line.
l -> move the cursor to the right one character position.
Note that the arrow keys on the keyboard can be used to move cursor also.
r -> replace one character under the cursor. Specify count to replace a number of characters
u ->undo the last change to the file. Typing u again will re-do the change.
x -> delete character under the cursor. Count specifies how many characters to delete. The characters will be deleted after the cursor.
Cutting and Yanking:
The command commonly used command for cutting is d. This command deletes text from the file. The command is preceded by an optional count and followed by a movement specification. If you double the command by typing dd, it deletes the current line. Here are some combinations of these:
Ctrl-d ->deletes from current cursor position to the beginning of the line.
d$ ->deletes from current cursor position to the end of the line (or use the D command).
dw ->deletes from current cursor position to the end of the word.
3dd ->deletes three lines from current cursor position downwards.
There is also the y command which operates similarly to the d command which take text from the file without deleting the text.
The commands to paste are p and P. The only differ in the position relative to the cursor where they paste. p pastes the specified or general buffer after the cursor position, while P pastes the specified or general buffer before the cursor position. Specifying count before the paste command pastes text the specified number of times.
Word and Character Searching:
The "vi" editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. The / command searches forwards (downwards) in the file, while the ? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. Some characters have special meanings to "vi", so they must be preceded by a backslash (\) to be included as part of the search expression.
Input commands (end with ESC)
a -- Append after cursor
i -- Insert before cursor
o -- Open line below
O -- Open line above
:r file -- Insert file after current line
Changes during insert mode
<CTRL>h -- Back one character
<CTRL>w -- Back one word
<CRTL>u -- Back to beginning of insert
dd -- Delete line
ndd -- Delete n lines to general buffer
dw -- Delete word to general buffer
ndw -- Delete n words
d) -- Delete to end of sentence
db -- Delete previous word
D -- Delete to end of line
x -- Delete character
p -- Put general buffer after cursor
P -- Put general buffer before cursor
u -- Undo last change
U -- Undo all changes on line
File management commands
:w name -- Write edit buffer to file name
:wq -- Write to file and quit
:q! -- Quit without saving changes
ZZ -- Same as :wq
:sh -- Starts a sub-shell to execute shell commands (<CTRL>d)\
Other Editors :
1. Text Editor
3. Pico Tex t Editor
PREVIOUS HOME NEXT