
Vim: Tips and tricks
1 Issues related to new Vim 7.0
1.1 Parenthesis matching
When your cursor in on a parenthesis, the matching paren-
thesis (“the other side”) will be highlighted. This may an-
noy some of you. In order to disable this feature, open
your vimrc (located in your home directory, ~/.vimrc in
Unix and N:\ vimrc in Windows) and add this line:
let loaded matchparen = 1
Restart your Vim (or GVim) to see the effect.
1.2 Keyword completion — disable the
menu
Starting from 7.0, you will see a menu showing a list of
candidates when you press
Ctrl
+
N
or
Ctrl
+
P
by
default. If you find this annoying then turn it off by adding
this line to the vimrc:
set completeopt=
See more about completion by :help completion.
1.3 Tab editing
Instead of typing :edit new.file (:e in short) to edit
another file, a new option :tabedit new.file (:tabe) is
available. This creates tabs, like those used in browser.
Navigate though the tabs using
Ctrl
+
PgUp
and
Ctrl
+
PgDn
(If your terminal processed these key bind-
ings, you can still use
g
t
and
g
T
— though that’s
not as convenient). Advantage of using tabs over buffer
list is that you switch between files using shortcut keys
(instead of :bn<ENTER> and :bN<ENTER>). Also, name of
the file is shown clearly on the tab, so you know what you
are doing. (though you lose one line)
Combining this command and window splitting (:new,
:vnew, :split, :vsplit, ...) gives you the most flexibility.
1.4 Spell checking
To enable spell checking, type the following:
:setl spell spl=en All regions (default)
:setl spell spl=en gb,en us GB, USA
Move around misspelled words using
]
s
and
[
s
.
To turn it off:
:setl spell spl=
Of course, putting these as a keyboard macro will be more
convenient. Add this to your vimrc:
set spell spl=en us " Select language
set nospell " Turn it off at start
nmap <F7> :set spell! "toggle spell check
In insert mode,
Ctrl
+
X
S
gives you spell sugges-
tion(s), and you can press
Ctrl
+
N
for next suggestion.
For more information, :help spell.
2 Regarding GVim
2.1 A ‘clean’ GVim
One of the good things about GVim is that the most com-
mon tasks are listed on the menu so you can learn quickly.
After that you may want to claim back the area, eliminate
the menu and toolbar. (Why not Vim? GVim has more
colours to choose from, that’s why — at least to me) Just
add this line to your vimrc:
set guioptions=g
2.2 Fonts, fonts, fonts, ...
Choose a font in the GVim using
:set guifont=*
Once you’re satisfied with the font, show the font name
with :set guifont, which will return something like
guifont=Monospace 8
You have to escape spaces and commas that might occur
in the string using backslash. In this case you should add
this line to your vimrc:
set guifont=Monospace\ 8
2.3 Clipboard
In GVim, you can do copy and paste to the clipboard by
"
+
y
and
"
+
p
, respectively.
3 Other bits
3.1 Paste Mode
Have you ever tried pasting something into Vim and end
up with badly aligned result? Here’s the trick: type :set
paste to put Vim in paste mode before you paste, so Vim
will not do anything fancy and paste all the stuff verbatim.
After you have finished pasting, type :set nopaste to go
back to normal, where indentation will take place again.
(You only need this option when pasting in terminal, but
not GUI)
3.2 Copying from Terminal
Use :set nonu to disable line numbering before you copy
lines of code to the clipboard. Restore by :set nu.
3.3 Searching
• You might know
/
and
?
for searching. How
about
*
and
#
? They search for word nearest to
the cursor forward and backward, respectively. They
save you time typing the word to search.
• Use
n
instead of
/
ENTER
to repeat search (
N
do it backwards).
• Use :noh to cancel highlights after searching.
评论