VIM Text Editor


Using VIM Text Editor

Vim is an editor to create or edit a text file. There are two modes in vim. One is the command mode and another is the insert mode. In the command mode, user can move around the file, delete text, etc. In the insert mode, user can insert text.

Changing mode from one to another

From command mode to insert mode type a/A/i/I/o/O ( see details below)

From insert mode to command mode type Esc (escape key)

Some useful commands for VIM

Text Entry Commands (Used to start text entry)

KeystrokeDescription
aAppend text following current cursor position.
AAppend text to the end of current line.
iInsert text before the current cursor position.
IInsert text at the beginning of the cursor line.
gIInsert text in column 1 [count] times.
oOpen up a new line following the current line and add text there.
OOpen up a new line in front of the current line and add text there.

Selecting Text (Visual Mode)

To select text, enter visual mode with one of the commands below, and use motion commands to highlight the text you are interested in. Then, use some command on the text.

The operators that can be used are:

KeystrokeDescription
~switch case
ddelete
cchange
yyank
>shift right
<shift left
!filter through external command.
=filter through equalprg option command.
gqformat lines to textwidth length.
vstart Visual mode per character.
Vstart Visual mode linewise.
Escexit Visual mode without making any changes.

The following commands are used only in the commands mode

Cursor Movement Commands

KeystrokeDescription
hMoves the cursor one character to the left.
iMoves the cursor one character to the right.
kMoves the cursor up one line.
jMoves the cursor down one line.
nG or :nCursor goes to the specified (n) line.

Example:- 10G goes to line 10

KeystrokeDescription
^F (CTRl F)Forward screenful.
^BBackward screenful.
^fOne page forward.
^bOne page backward.
^UUp half screenfu.
^DDown half screenful.
$Move cursor to the end of current line.
0 (zero)Move cursor to the beginning of current line.
wForward one word.
bBackward one word.

Exit Commands

KeystrokeDescription
:wqWrite file to disk and quit the editor.
:q!Quit (no warning).
:qQuit (a warning is printed if a modified file has not been saved).
ZZSave workspace and quit the editor (same as :wq).
: 10,25 w temp

write lines 10 through 25 into file named temp. Of course, other line.

numbers can be used. (Use :f to find out the line numbers you want.

Text Deletion Commands

KeystrokeDescription
xDelete character.
dwDelete word from cursor on.
dbDelete word backward.
ddDelete line.
d$Delete to end of line.
d^ (d caret, not CTRL d)Delete to beginning of line.

Yank (has most of the options of delete)-- VI's copy commmand

KeystrokeDescription
yyyank current line.
y$yank to end of current line from cursor.
ywyank from cursor to end of current word.

5yy yank, for example, 5 lines Paste (used after delete or yank to recover lines.)

KeystrokeDescription
ppaste below cursor.
Ppaste above cursor.
"2ppaste from buffer 2 (there are 9).
uUndo last change.
URestore line.
JJoin next line down to the end of the current line.

File Manipulation Commands

KeystrokeDescription
:wWrite workspace to original file.
:wfile Write workspace to named file.
:efile Start editing a new file.
:rfile Read contents of a file to the workspace.
owhile in the insert mode in a new line.
iwhile in the insert mode.
^Lwill appear in your text and will cause the printer to start.

Other Useful Commands

Most commands can be repeated n times by typing a number, n, before the command. For example 10dd means delete 10 lines.

KeystrokeDescription
.Repeat last command.
cwChange current word to a new word.
rReplace one character at the cursor position.
RBegin overstrike or replace mode – use ESC key to exit.
:/pattern Search forward for the pattern.
:?pattern Search backward for the pattern.
nused after either of the 2 search commands above to continue to find next occurrence of the pattern.
:g/pat1/s//pat2/greplace every occurrence of pattern1 (pat1) with pat2.

Example replace every occurrence of pattern1 (pat1) with pat2

KeystrokeDescription
g/tIO/s//Ada.Text_IO/gThis will find and replace tIO by Ada.text_IO everywhere in the file.
:g/a/s// /greplace the letter a, by blank.
:g/a/s///greplace a by nothing.
Note: Even this command be undone by u.

Examples

Step 1

type vim filename (create a file named filename).

Step 2

type i ( switch to insert mode).

Step 3

enter text (enter your Ada program)

Step 4

hit Esc key (switch back to command mode).

Step 5

type :wq (write file and exit vim).


Editing the Existing File

Step 1

type vim filename (edit the existing file named filename).

Step 2

move around the file using h/j/k/l key or any appropriate command.

KeystrokeDescription
hMoves the cursor one character to the left.
lMoves the cursor one character to the right.
kMoves the cursor up one line.
jMoves the cursor down one line.
nGCursor goes to the specified (n) line (ex. 10G goes to line 10).
Step 3

edit required text (replace or delete or insert).

Step 4

hit Esc key (exit from insert mode if you insert or replace text).

Step 5

type :wq