Вы находитесь на странице: 1из 3

LINUXUSER Vim Tricks

Mathias Schweighofer, photocase.com

Getting beyond vi with the vim text editor

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.

82 ISSUE 85 DECEMBER 2007


Vim Tricks LINUXUSER

The following requires that you have the


full featured vim package and not a cut-
down version, which is usually obvious
when you run vim --version: Usually you
will see Small version without GUI in the
version summary. If your Linux uses the
alternatives system, another approach is
to look at the symbolic link vim has
within /etc/alternatives and see if it
points to the vim.tiny binary instead of
vim or vim.full. Ubuntu is guilty of in-
stalling a stripped-down package of vim
called vim-tiny. Ubuntu users can simply
install a vim package called vim-full to
get the fully featured version.
To make vim act as a file browser, sim-
ply enter the following:

: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.

DECEMBER 2007 ISSUE 85 83


LINUXUSER Vim Tricks

sult. In visual mode, you highlight the


text you want to edit and then type an
editing command. Three modes are
available: line-by-line, character, and
block mode. Line-by-line and character
modes are probably the most useful, but
block mode could be handy if you need
to edit text tables. These modes are
available via the key combinations
shown in Table 3.
With vim’s visual mode, to copy a sec-
tion of text, simply enter visual mode,
highlight the text you want, and type

:y

to yank the highlighted text into an un-


named buffer (Figure 2). Move the cur-
sor to where you want to locate the text
and type
Figure 2: Vim’s visual mode is useful for selecting and moving text.
:p
If you don’t like the tabs at the top :setlocal spell U
of the screen, turn them off by running spelllang=en_gb to place a copy of the text there.
It is also possible to use your mouse to
:set showtabline=0 Replace en_gb with the language region highlight text by typing
code you require. Vim will highlight
and then switch them back on with: words that are incorrectly spelled in red :set mouse=a
and rare or uncapitalized words in blue.
:set showtabline=2 To navigate the words vim thinks are in- or placing this command within your
correct, use the key combinations shown .vimrc file. Now when you highlight a
If you prefer not to see the tabs, simply in Table 2. The vim spell checker comes section of text with your mouse, vim will
add the following to your .vimrc file: with other key combinations. Enter go into visual mode automatically.

set showtabline=0 : help spell Conclusion


If you migrate from the vi world, some
With the tabbing display switched off, at to see the extended documentation. of the recent improvements to vim could
times, you’ll want to remind yourself of Vi users know that if you want to edit be hard to find. In this article, I summa-
the tabs you have running. The follow- a section of code or text within a file, rized some of the tricks you can play
ing command provides such a summary: you can place the cursor on the last line with the vim text editor. ■
you want to edit and run the following
:tabs command to place a marker: Table 2: Spell Checker
Key Combinations
Vim will display a list of tabs and the file :ma Sequence Description
associated with each tab. ]s Go to the next misspelled word
Finally, one of the most useful features To copy this section, place the cursor at [s Go the previous misspelled
of tabbing is the tabdo command, which the start of the section and enter word
lets you perform an action on all of the z= Display suggestions for correct
open tabs (Figure 1). As a simple exam- :y'a spelling
ple, the following command replaces the zg Add word as a correctly spelled
word
text fred with joe in all open tabs: which yanks all the code or text from the
cursor to the marker a you just placed.
:tabdo %s/fred/joe/g Then you can move the cursor to where Table 3: Visual Mode
you want to place the section and enter: Options
Purists look away, but yes, vim now
Sequence Description
comes with a built-in spell checker. The :p
Shift+v Line-by-line mode
spell checker is often disabled by de-
v Character mode
fault. To enable spell checking, enter the With vim, this also works, but it has a
Ctrl+v Block mode
following command: slightly quicker way of achieving this re-

84 ISSUE 85 DECEMBER 2007

Вам также может понравиться