Overview of vi and vim
Ø vi: the “Visual Editor”, standard Linux and Unix editor.
Ø vim: the “vi improved” editor, standard Red Hat editor.
Ø On Red Hat operating system, the vi command invokes vim.
Ø Derived from earlier Unix editors.
Ed -> ex -> vi -> vim
Starting vi and vim
Ø To start vim: # vi
If the file exists, the file is opened and contents are displayed.
If the file doesn’t exist, vi creates it when the edit are saved for the first time.
Ø To use vi instead:
Unalias vi or
\vi
Ø Same useful operation that you can use with vim include:
Vi command with different options | Performance |
# vi -m | File is non-modifiable. |
# vi -R | File is only modifiable by using the exclamation point: w! |
# vi -n | Do not use a swap file for backup (useful for floppies) |
# vi -r | Recover data from a swap file after a crash |
# vi -x | Encrypt file when saving decrypt file when editing |
Three modes of vi and vim
1) Command mode: Cursor movement, change, delete, yank, put, search.
2) Insert mode: Type in new text and return to command mode with
3) ex mode: Configuring, exiting, saving, searching and replacing.
Cursor Movement (Arrow keys also work)
Key | Performance | Key | Performance |
h | Left | b | Word back |
j | Down | ( | Sentence back |
k | Up | ) | Sentence |
l | Right | { | Paragraph above |
w | Word ahead | } | Paragraph below |
Entering Insert Mode
Key | Performance | Key | Performance |
a | Append after the cursor. | A | Append to end of file. |
i | Insert before the cursor. | I | Insert at beginning of line. |
o | Open a line below. | O | Open a line above. |
Leaving Insert Mode -
Ø
Ø Hint: When in trouble, press
Change, Delete and Yank (copy)
Performance Type | Change | Delete | Yank |
Line | cc | dd | yy |
Letter | c1 | d1 | y1 |
Word | cw | dw | yw |
Sentence ahead | c( | d( | y( |
Sentence behind | c) | d) | y) |
Paragraph above | c{ | d{ | y{ |
Paragraph below | c} | d} | y} |
Put (Paste)
Use P or p to put (pasts) copied or deleted or changed data.
For line oriented data: | |
Key | Performance |
p | Puts the data below the current line. |
P | Puts the data above the current line. |
For character oriented data: | |
P | Puts the data after the cursor. |
p | Puts the data above the cursor. |
Undoing Changes
Key | Performance |
u | Undo most recent change. |
U | Undo all change to the current line since the cursor landed on the line. |
| Redo last “undone” change. |
Search for text
Key | Performance |
/text | Search downwards for “text”. |
?text | Search upwards for “text”. |
n | Continue search in the opposite direction. |
Saving and Exiting - ex mode
Normally Changes | ||
| Save Changes | Abandon Changes |
Exit | :wq | :q or :q! |
Do not exit | :w | :e! |
| | |
Forcing Changes | ||
Exit | :wq! | |
Do not exit | :w! | |
A few tricks
Command | Performance |
dtc | Delete from cursor to the letter “c” (does not span lines). |
5dd | Delete five lines (a number can precede any of the two character change, delete, yank or put commands). |
x | Delete a character. |
rc | Replace a character. With c. |
R | Replace character for character until press |
Filtering
The output of a command can be placed in the file and the data in the file can be used as input.
Ø !!date It will show current date and time.
Ø :! ls It will show the files and directories.
Ø !}sort
Ø !}fmt -66
ex mode – (search and replace)
Different default addressing rule -
no address | Current line only. |
1,$ | For changes to entire file. |
.,.+10 | From current line (“.”) to current line plus 10 lines (“+10”). |
8.12 | Change line 8 through 12. |
Example,
:1,$s /hio/lowa/g
Advanced
Ø :r newfile
Ø :r !date
Ø :1,zow xfile
Ø :.,$w yfile
Ø :1,zow >> zfile
Ø :n
Ø :n!
Ø :n#
Configuring vi and vim
Configuring permanently –
· ~/.vimrc or ~/.exrc
A few common configuration items –
· :set showmatch
· :set autoindent
· :set textwidth=65 (vim only)
· :set wrapmargin=15
· :set ignorecase
· :set number - It will set line numbers.
· :set no number - To remove the line number.
· :set all or :set - Configuring on the fly. It will display all the configuration commands of vi and vim editors.