| Living with Vim |
| Living with Vim |
I started to write this document years ago, to illustrate Vim for some basic usage. The purpose is just trying to domenstrate how Vim is usful and could make your life easier. Recently, I decided to revise this document and to introduce for more wonderful features. Some of them are new for Vim, and some are inherent but new for me.
Vim is a very powerful editor to fulfill your goal efficiently. Keep using Vim is the best way to learn it. Did you learn to ride a bicycle by reading the user manual or some tutorial? So, do not expect that you can become a Vim expert by just sitting and reading any kind of documentation. Therefore this is not a exhaustive tutorial here. Only tips and command reference are provided to help your Vim skill.
| Some Basics |
| Vim is not Vi |
In FreeBSD, Vim is described as: ``A vi ''workalike``, with many additional
features''. In Linux, ``vim - Vi IMproved, a programmers text editor''. Vim
is an improvement over the editor,
vi, one of the standard UNIX text editors. The creator is
Bram Moolenaar. VIM adds many of the features that you would expect in an editor:
Unlimited undo, syntax coloring, split windows, visual selection, graphical
user interface (read: menus, mouse control, scrollbars, text selection),
and much much more. VIM runs on many operating systems, including most
UNIX/Linux systems, MacOS, DOS and Windows (95/98/NT4/2000), etc. And the
best of all: VIM is FREE! :-)
So why should I use a vi clone like Vim? Well, I realize the answer after starting to use Vim. Perhaps you should learn the answer by youself.
| Convention |
The notation C- denotes a Ctrl key combination.
C-w represents the combination of Ctrl and w.
| Getting the help |
Perhaps the fastest way to get a help is asking some Vim senior for your
diffuculties. However, Vim does provide user manual. When using GUI,
getting the manual is straightforward by mouse clicks. When in console
mode, F1 or :help will bring you the help. Additional references among the Internet are
listed at the end of this article.
| Start up VIM |
To start up Vim, simply type
vim pentium.v
to enter the file pentium.v or
gvim pentium.v
to use the GUI interface of Vim. You can edit multiple files and open multiple sub-windows initially
vim -o *.v
and Vim will open all of the files you want and display and split them horizontally. Or you can use
vim -O *.v
to get similar result with vertical sub-windows.
| Text Editing |
Basically there are two operation modes in Vim: command mode and insert mode. Initially Vim starts at the command mode and any input will be treated as a part of the command. To append/insert the characters into the file you must enter the insert mode first.
One of the most commonly used command to enter the insert mode is i, which allows you to insert words at the position of the cursor. Other
related commands are
i: insert at the position of the cursor a: insert next to the position of the cursor I: insert from the beginning of the line A: insert at the end of the line
In insert mode, almost every input will be considered as the content of
your file (this is not exactly true, because Vim provide many powerful
command which can be used in the insert mode). You can leave the insert
mode by using ESC and then execute your commands.
Instead of insertion, we can delete a character at the cursor by using x. Two more commands are useful
dw: delete a word from the cursor
cw: change a word from the cursor
(delete it then go into the insert mode)
You can image that dw means deleting a word and cw means changing a word. The concept can be expanded
dd: delete a line at the cursor
cc: change a line at the cursor
(delete it then go into the insert mode)
Furthermore, you can assign a number to indicate how many lines/words you
wanna delete/change, e.g., 10dd means delete 10 lines.
We have discussed about the function of ESC above, ESC can also be used to cancel a command, for example, if you wanna change a
word but after typing c, you find out that the cursor is at the wrong position, you can use ESC to cancel the command. =item * Replacement
In addition to insertion and deletion, there are replacement commands. You
can use r you replace the character at the cursor, just type
r and then whatever you want for the replacement. As you may notice after you
use i command Vim prompts a -- INSERT -- string to remind you at the insert mode. You can use R and see the difference, the prompt message changes to -- REPLACE --, and every input will be treated as a replacement with previous one at the
cursor. Similarly, a ESC can terminate the replacement.
| Moving the Cursor |
To moving the cursor, you can use the keys h, j, k and l.
h: left j: down k: up l: right
These four keys is very convenient to use for your right hand, but in Vim you can forget them because many people doesn't like them. You can simply use the arrow key to move the cursor. When the file size is getting bigger, you can use the hot key to scroll the page
C-f: move forward a page C-b: move backward a page
Usually we edit a code in C, Perl, Verilog or some kind of language. The
compilers often report that what number of the code is wrong, e.g., line 25
has a syntax error. You can jump to line 25 by using
25G at the command mode. If only command G is used, the cursor will go to the end of the file (last line).
1G: go to the first line nG: go to the n-th line G: go to the last line
Similarly you can jump within a line
0: jump to the beginning of the line $: jump to the end of the line
Additionally, you can jump between the words by using w and e
to go forward and backward, respectively.
| Exiting VIM |
To exit Vim, just use
:q
or you can save then quit
:wq
If you modified the file, you have to force Vim to leaving
:q!
without saving the changes. The command :wq is identical to
:x. You can also use :w! to force a write. Usually !
can use to force an action.
| Undo/Redo Your Changes |
To undo a change simply use u. Vim allows multiple undoes, therefore you can go back as many times as
you want. Use C-r can redo an undo. Also you can do multiple redoes.
| Copy and Paste |
In Vim, the copy and paste commands is called as Yank and Put. The usage of yanking is very similar with deletion.
yw: yank a word yy: yank a line
also 12yy will copy 12 lines into the buffer. The next step is to put the content to
where you like. Move the cursor and use p to put the content of the buffer. Moving a line or a word or multiple lines
is simply deleting them then putting at the new location.
A powerful function of Vim is the visual blocking (or Visual mode). Using V, you will see the current line is highlighting. Moving the cursor you can
mark more lines. A d or y
can delete or copy these highlighting lines. There are three different
visual mode.
V: linewise visual mode v: characterwise visual mode C-v: blockwise visual mode
Try them can you will find them very useful. The visual can be broken by ESC.
In visual mode there are many available operations, among them here is some common and useful ones:
d delete y yank U uppercase all characters u lowercase all characters ~ toggle case < unindent > indent
| Search and Replace |
To search a pattern, use
/pattern
You can use n to find the next match, or N for previous match.
?pattern
can do the same thing, with opposite searching direction.
The method of replacement is discussed above. However sometimes we want to
change a keyword to another. For example, if you want to replace every Intel
with AMD, use
:%s/Intel/AMD/g
The syntax format is
:%s/oldpattern/newpattern/gc
%: the range of whole file s: substitution oldpattern: old pattern to match newpattern: new pattern for replacement g: global c: confirm for every replacement
The first part indicates the range, % means the whole file, you can define the range of part of the file, as
:2,46s/oldpattern/newpattern/gc
to apply the substitution from line 2 to line 46. The second part indicates
the command, s means substitution. The characters / separates two patterns for matching and replacement. The last field of the
syntax is the option. Global option g is meant to match every pattern in a line, instead of the first matched
pattern of a line.
Visual mode can be used for substitution, e.g., you can mark multiple lines
using V, then type :, a line will appear as
:'<,'>
then append the substitution command s/oldpattern/newpattern/g.
| Location and File Status |
Type C-g and Vim will report the filename, the status (modified or readonly), total
number of lines and the cursor position.
| More Tips |
| Recovering When Panic |
When you edit an important file and suddenly there is a power outage. All
system are shutdown before you can save your source. Don't be sad because
Vim provide a recovering mechanism. Checking a file called .pentium.v.swp if you are editing a file called pentium.v. Recover with Vim
vim -r .pentium.v.swp
and see if it contains the source you deal with. Save it if everything is
OK, then remove the swap file .filename.swp.
| Executing Shell Commands |
You can execute a command with the shell, for example, you can compile you
C code when editing it, :!gcc kernel.c, or :!gcc % to compile the current editing file. Executing other commands is the same.
| Keyword Matching |
If you are lazy as me, you will find this function wonderful :) In insert
mode, you can type a few characters of a word, e.g., if there is a variable
called alu_pipeline_register, you may just type alu then C-p. Vim will find out the last word in the file started with characters alu, if it is not you want, you can re-type C-p to match the further previous ones. Similarly, C-n can do that for finding the next matches. Therefore you do not worry about
the mistyping of the long variables, or uncommon-used words.
| Jumping Between Multiple Windows |
To split the window you can use C-w s, or start Vim initially
vim -o *.c
Jumping between the sub-windows, use C-w plus arrow key to switch the cursor.
| Syntax Highlighting |
In our environment, the option of syntax highlighting is set by default. Therefore you can use it without any change. The function of Vim is very convenient for coding. You can switch on and off the function by
:syn on :syn off
| Getting On-Line Help |
To access Vim online help,
:help
you will learn the usage of the documentation. The online help contains almost everything you need so be patient to read the instructions to use it efficiently.
| Editing Multiple Files |
Editing two or more files at once is our daily work. You can copy or move a block from one file to the other as you wish. There are many differnet ways to achieve the goal.
You can edit a file and process with others by typing the command
:e newfile to edit the file called newfile. This leaves the original file in an invisible editing buffer which can be
called back later. You can then use C-6 command to switch between different editing buffer. Also you can use C-w C-6 to split the editing window of Vim and view all the file at the same time.
Press C-w o bring you back to the single editing window.
You can start up Vim with multiple edits windows directly. Just type the command at the startup.
vim -o *.txt
will edit all the file ended with .txt at multiple windows. Add a split window using the command C-w C-s, or open a new file using :sp newfile. For Vim version 6 or later, you can use
:vs filename to split the editing window vertically. Inside the multiple editing
windows, you can switch from one window to the other with the command C-w w
C-w j go to the next window below (or C-w up-arrow) C-w k go to the next window above (or C-w down-arrow)
| Braces Matching |
If the cursor is on an opening parenthesis { [ (, the command
% will move the cursor to the matching closing } ] ) in the normal mode, and vice versa.
| Insert/Delete an indent |
C-T/C-D insert/delete an indent in the current line, no matter what column the
cursor is in. There are handy hot keys when you are editing language with
ambiguous block boundary such as Python, which you have to delete the
indent yourself when necessary.
| Settings for .vimrc |
Some useful setting and mapping for .vimrc are listed as follows:
set nocp " nocompatible
set sw=4 " shiftwidth
set et " expandtab
set wm=8 " wrapmargin
set bs=2 " backspace
set ru " ruler
set ic " ignorecase
set is " incsearch
set scs " smartcase: override the 'ic' when searching
" if search pattern contains uppercase char
set vb t_vb= " set visual bell and disable screen flash
set backup
Use :help option-list for detail description of these options.
map Q gq
map ,, <C-p> " Map the auto-complete command to
" a handy ,, sequence
| References |
There is an official website for Vim (http://www.vim.org ). Many other people are also generous in showing their experience of Vim. In fact, many parts of this document is composited from borrowing someone else's ideas. Please visit my website for a detailed list of reference:
http://larc.ee.nthu.edu.tw/~cthuang/vim/
|