Академический Документы
Профессиональный Документы
Культура Документы
VIM TRICKS
You don’t have to use the vim text editor as a latter day vi. These simple in conjunction with ignorecase, sets vim
to search for any case unless uppercase
tricks will save you time and keystrokes. BY JON KENT is used in the search pattern. If you use
an uppercase character, vim assumes
V
im has become the de facto re- searching as soon as you start typing the you want to perform a case-sensitive
placement for vi in most Linux word you want to find. This feature is in- search. This gives you options when you
distributions. If you have been credibly useful but is not enabled by de- perform a search. For example,
using Unix systems for a while, you fault. Luckily, all you have to do is enter
probably have not even noticed that you /example
are now using vim instead of vi because :set incsearch
all the usual vi commands work as you would match Example, exAMple, and
would expect in vim. However, vim is an in the command window to enable in- example, whereas
extremely powerful editor – think emacs cremental searching. Now when you
without the need to grow additional fin- enter search mode, vim will start match- /Example
gers and attend yoga classes – and there- ing straightaway and highlight each
fore is much more than just a vi clone. match until you find what you are look- would only match Example.
These features are ready for you to use, ing for. Once you have found what you If you find these options useful and
and once you start using them, you want, you can simply hit Return or Esc want to enable them permanently, you
might wonder how you survived without to exit the search. need to edit or create a .vimrc file in
them. This article puts you on the road One issue is outstanding, even with your home directory and add:
to vim enlightenment by highlighting incremental search enabled: Matching is
some useful features you might not still case sensitive. As luck would have set incsearch
know about if you’re accustomed to it, you can also set up vim to be non- set ignorecase
using vim as another form of vi. case sensitive by enabling the following set smartcase
options in the command window:
Smarter Searching On most Linux systems, you should find
Searching is easy in vim, but what hap- :set ignorecase the default vimrc file located in /etc or
pens if you misspell a word or if you are :set smartcase /etc/vim, so if you are creating your own
not completely sure how the word is version, it is usually best to start by
spelled? With the standard search, you Users coming from vi will recognize the copying the default version into your
are stuck; however, vim has an incre- ignorecase option, but smartcase might home directory, renaming it .vimrc, and
mental search search mode that starts be new. The smartcase option, when set then making your changes.
:e .
Figure 1: Vim’s tabbing feature helps you organize open files.
Vim now will display the contents of the
current directory, which you can trans- or within vim itself. To see all the possi- can use the :n (next) and :N (previous)
verse by arrow keys or navigation keys. ble settings, enter: commands, but vim also lets you use
To open a file within vim, navigate to the split screen mode to edit files:
file you want to open and press Enter. In :help wildmode
the same manner, you can traverse direc- :split
tories by simply navigating to the direc- By default, this is set to full mode and
tory you want to open and pressing acts as outlined previously. Now the screen is split in half with a
Enter, at which time you will be pre- The previous examples all work on file horizontal rule, and you can use further
sented with the contents of the directory. names, but what if you want to find the split commands to split the screen into
If the directory is large, you can also per- files that contain a common string? From additional segments. To move among the
form the usual vim searches. the shell command line, you would use windows, use Ctrl+W and arrow keys.
Within vim, you can open any files grep to show all the files that contain a This feature makes editing multiple
without using the file browser by simply common string. To do the same with files easy, especially if you want to view
entering the name of the file you want to vim, you simply start vim and then run: both files at the same time. However, if
view. But what if you are not completely splitting the screen is not to your liking,
sure of the name? Vim has another trick :grep <string> * or if you have multiple files you want to
up its sleeve: file completion. This works edit, version 7 or later of vim has a new
in much the same way as shell file com- Vim will find files that contain string and feature that could help: tabbing. Tabbing
pletion, in which you simply enter a few open them, then you can navigate be- works by adding a tab to, by default, the
characters of the file in question and tween the files with vim commands: top of the screen with the file you are ed-
press the Tab key for vim to complete iting. To use tabbing within vim, enter:
the file name. If multiple files match the :cn - go to next file
characters you have entered, you simply :cp - go to previous :tabnew <file name>
press Tab until you come to the file you :cc - show current match
want to edit and then press Enter. Don’t forget that you can also use the
File completion is fully configurable – When you view each file, vim starts at auto-completion feature of vim with the
this is vim after all. Modify the wildmode the location of the match in that file. If tabnew command. Vim will open this
parameter either within your vimrc file you have moved away from the match file in a new tabbed window ready for
within a file and want to go back, use :cc you to edit. Another, possibly quicker,
Table 1: Navigating to go back to the match. To move be- method is to supply all the files you
Vim Tabs tween files, use the :cn and :cp com- want to open at the shell command line
mands instead of :n and :N because they with the -p flag:
Command Description
use vim error handling; therefore, vim
:gt Next tag
stores the matching files differently. vim -p file1 file2 U
:tabn Next tag
file3 file4
:tabp Previous tag
:tabfirst First tag
Editing Multiple Files
:tablast Last tag
Vim, like vi, can edit multiple files at the To navigate between tabs, use the com-
same time. To transverse these files, you mands shown in Table 1.
:y