bladexia 发表于 2008-01-03 17:55

Advanced Vi Usage


On this page...
[*]
Basic Movement

[*]
Searching

[*]
Line Jumping

[*]
Multiple Buffers

[*]
Split Windows

[*]
Folding

[*]
Ex Commands

[*]
Plugins

[*]
Miscellaneous

[*]
References/Links

Once you are familiar with the basic set of commands presented in
Using The vi Text Editor
, you can then try some of these more advanced vi commands.
Again, the whole purpose of vi is to alleviate the need to use the mouse. By keeping your hands on the keyboard you can do a lot of things faster than by doing them with a mouse.
Basic Movement
w - Move the cursor to the beginning of the next word.
b - Move the cursor to the beginning of the previous word.
e - Move the cursor to the end of the next word.
ge - Move the cursor to the end of the previous word.
} - Move the cursor to the beginning of the next paragraph.
{ - Move the cursor to the beginning of the previous paragraph.
) - Move the cursor to the beginning of the next sentence.
( - Move the cursor to the beginning of the previous sentence.
^ - Move the cursor to the first character on the line.
$ - Move the cursor to the last character on the line.
Searching
One thing that is used extensively when jumping around a file is searching. You can invoke searching by simply typing /foo in command mode where foo is the pattern of text you want to search for. vi will then go off and find it and put the cursor just above the first character of the first match. To go to the next match, hit the n key.
/foo - Search for pattern foo in a file.
n - Go to the next match of the current search pattern.
-n - Go to the previous match of the current search pattern.
Line Jumping
Jumping to a particular line in vi is useful when debugging code. When you compile your program and it breaks or when you are debugging output of your program, many times you will be given a line number with the associated error or debug message.
Once you have the file open, you can type : whereis the line number you want to jump to. For example, :16 jumps to line 16 of the current file.
Here are some useful line commands:
:x - Jump to line x in the current buffer.
-g - Show the line number position of the cursor.
:set number - Turn on line numbers along the left-hand side of the buffer.
vi+x - Openin vi and place the cursor at line number x.
Multiple Buffers
Opening multiple files in vi is a common occurrence and you need to know how to jump between them.
On the command line, you can open multiple files with vi by globbing or specifying each file as an argument to vi. For example:

vi foo.pl foo.java foo.txt
vi foo.*
Both of these commands will open the three files: foo.pl, foo.java, and foo.txt. The second example uses globbing and could possibly open more files that begin with foo. if they also exist in the current directory.
Once you have multiple files open, vi will drop you in the first one. To get to the second file opened, type :n. You can get to the previous file by hitting -^.
Split Windows
To split a window, simply type :split. This will split the current buffer in two. Jumping between the split windows is accomplished with -w w (or -w -w which can be a bit easier when jumping between a lot of splits). To close a split, type :close. If you have several splits open and you want to close all of them except your current buffer, type :only.
To create a split with the file foo.c, type :split foo.c. To create a split with a new buffer, type :new. Increasing the size of the split is done by #-W + or #-W - where # is the number of lines you want the split to move in the upward (+) or downward (-) direction.
There are also vertical splits. These are created with ':vsplit' and can do all the things horizontal splits can.
To make vim open several files each in there own split, use the -o option. For example:
vim -o one.sh two.sh three.sh
This will open a vim window with three horizontal split panes. The -O option creates vertical splits.
Folding
Type :set foldmethod=indent and your code will fold by indent level.
Fold commands:
zo - Open a fold.
zc - Close a fold.
zr - Reduce the fold level by one. Repeat as necessary.
zm - Increase the fold level (add More folds). Repeat as necessary.
zR - Open all folds beneath a level.
zM - Close all folds beneath a level.
zn - Disable folding.
zi - Toggle between folding on and folding off.
I use the indent fold method which folds your code by indent level.
Below is my .vimrc file. This file is used to customize vim's behaviour by default, on startup. Everything after a " is a comment.
set tw=78" set the textwidth to 78 characters
set ts=4   " set the tabstop to 4 characters
set autoindent " turn on autoindent
set expandtab " convert my tabs to spaces
set background=dark " my xterms are dark blue with a yellow font
set ruler " turn on the ruler
set foldmethod=indent " set the foldmethod to indent
set shiftwidth=4 "set the shiftwidth to 4 since my tabs are 4
Ex Commands:r- Readinto current buffer at cursor position.
Plugins
Most vim installations come with some widely used plugins. One such plugin is the file explorer plugin. Inside vim, to invoke it on your current directory, type :e ..
Miscellaneousgf - Open the filename which is under the cursor.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/58679/showart_456006.html
页: [1]
查看完整版本: Advanced Vi Usage