vim.pdf

(111 KB) Pobierz
Vim: Tips and tricks
1
1.1
Issues related to new
Vim
7.0
Parenthesis matching
2
2.1
Regarding
GVim
A ‘clean’
GVim
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 eect.
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
1.2
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 nd this annoying then turn it o by adding
this line to the vimrc:
set completeopt=
See more about completion by
:help completion.
Keyword completion — disable the
menu
2.2
Fonts,
fonts,
fonts, ...
Choose a font in the
GVim
using
:set guifont=*
Once you’re satised 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
1.3
Tab editing
2.3
Instead of typing
:edit
new.file
(:e in short) to edit
another le, 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 buer
list is that you switch between les using shortcut keys
(instead of
:bn<ENTER>
and
:bN<ENTER>).
Also, name of
the le 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 exibility.
3
In
GVim,
you can do copy and paste to the clipboard by
ØÙ
"
ØÙ
+
ØÙ
y
and
ØÙ
"
ØÙ
+
ØÙ
p
, respectively.
Clipboard
Other bits
3.1
Paste Mode
1.4
Spell checking
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 stu verbatim.
After you have nished 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)
To enable spell checking, type the following:
:setl spell spl=en
All regions (default)
GB, USA
:setl spell spl=en gb,en us
Move around misspelled words using
ØÙ
]
ØÙ
s
and
ØÙ
[
ØÙ
s
.
To turn it o:
: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.
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
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).
Searching
Use
:noh
to cancel highlights after searching.
:set is
turns on incremental search.
3.11
Code folding
3.4
3.5
ØÙ
K
shows you the man page for the keyword under cursor.
Man page
Dictionary Lookup
Add this line to your vimrc:
nmap <F2> :!webster <C-R><C-W><CR>
Dictionary shows up when you press
ØÙ
F2
in normal mode.
3.6
ØÙ
J
join the current line with the next, and
ØÙ
g
ØÙ
J
do the
same without inserting a space.
Joining line(s)
3.7
Executing commands
:!
command
executes
command
as if it is typed under
the shell. Combining this with
:make
you don’t need to
exit
Vim
every time you compile and debug your program.
ØÙ
%
is used to match parenthesis. It can match
(), [],
{},
comment (/*
*/)
and
#define.
Instead of splitting your window to view source code from
dierent part of the program, you can fold up codes that
you are not interested. To fold some part of your code,
just create a selection using the visual mode, then type
:fo.
Note that when you type
ØÙ
:
it will insert
’<,’>
automatically and you don’t need to remove them. A fold
will be represented by
/*{{{*/
and
/*}}}*/.
You can
open and close a fold by
ØÙ
z
ØÙ
o
and
ØÙ
z
ØÙ
c
, respectively.
You can also ask
Vimto
fold the code based on the
indentation of your code. To achieve this, type
:set
fdm=indent
(or add it to vimrc). Here
fdm
means “fold
method”. Then you can fold/unfold the code at that in-
dent level using
ØÙ
z
ØÙ
c
and
ØÙ
z
ØÙ
o
. Moreover, you can
fold/unfold codes of the same indent level by
ØÙ
z
ØÙ
m
(fold
more) and
ØÙ
z
ØÙ
r
(fold less). In this case you can fold your
code without adding the fold mark.
Why
ØÙ
z
? According to the help,
z
is like a folded piece
of paper.
3.12
Indenting code
3.8
Matching parenthesis
3.9
Make C, C++ programs in
Vim
There are few options related to indentation, namely
autoindent, cindent
and
smartindent.
An example to
this is located in
$VIMRUNTIME/vimrc example.vim.
To adjust indentation (e.g. in case of copy & paste),
use
number of lines
ØÙ
=
ØÙ
=
. To indent the whole le, type
ØÙ
g
ØÙ
g
ØÙ
=
ØÙ
G
(go to the top, then indent until the end of
le.
Add this line to vimrc
au FileType cpp,c setl mp=make\ %:t:r
then you can make your C or C++ programs in
Vim
using
:make.
Note that it uses environment variables
CFLAGS
and
CXXFLAGS
as default ags. So add
set CFLAGS="-Wall -g"
set CXXFLAGS=($CFLAGS)
to your
.cshrc
(for csh), or
export CFLAGS="-Wall -g"
export CXXFLAGS=${CFLAGS}
to your
.bashrc
(for bash), to make sure you don’t miss
any warnings and have your program ready for debugging.
In case of any warnings or errors it will take you there.
You can display subsequent warnings and errors after a
make by typing
:cn
(previous by
:cN).
Three visual modes are available.
ØÙ
v
is for normal visual
mode, you can invoke it using mouse drag.
ØÙ
V
is for line
visual mode, the unit becomes line instead of character,
using a mouse you do double click and drag.
ØÙ
Ctrl
+
ØÙ
V
is for block visual mode, one of the features that dierenti-
ate itself from other editors. A triple click (yes!) and drag
invoke this.
3.13
You can precede most of the commands with a number
indicating the number of times to repeat. E.g.
ØÙ
8
ØÙ
p
paste 8 times. But for some commands, preceding with a
number changes its meaning:
ØÙ
G
send you to the bottom of le, while
n
ØÙ
G
send
you to the
n
’th line.
ØÙ
%
match parenthesis, while
n
ØÙ
%
send you to the
n
’th percentile of the le.
Using numbers
3.14
3.10
Visual mode
Macros are (one of the) most powerful feature in
Vim.
Be-
gin recording a macro by pressing
ØÙ
q
c
, where
c
can
be any alphabets. This will be the name of the macro.
Any keys pressed after that will be recorded. End record-
ing a macro by pressing
ØÙ
q
again. Execute the macro
you recorded by
ØÙ
@
c
. Using macros can increase pro-
ductivity a lot, saving your time in doing some repetitive
work.
Macros
3.15
Undo by
ØÙ
u
, undo the whole line by
ØÙ
U
.
Undo, Redo and Repeat
Redo by
ØÙ
Ctrl
+
ØÙ
R
.
ØÙ
.
repeats the last action. This can be a sequence
of actions you do in insert mode.
g
Replace all occurrence in the line. Without
this only the rst matched string in each line
will be replaced.
c
Conrm each substitution.
n
Just report the number of matches, substitu-
tion is not done. (version 7.0 or above)
3.16
Splitting Window
3.19
Split a window horizontally using
:sp
(split). Split a win-
dow vertically using
:vs
(vertical split). Precede the com-
mand with a number (e.g.
:10sp)
to control the height (or
width) of the rst window.
Combining split and edit command, we have
:new
and
:vnew.
Cycle through the list of les using
:bn
and
:bN.
Move to the adjacent windows using
ØÙ
Ctrl
+
ØÙ
W
and
then cursor key. Remove a window using
:q.
Adjust the
size of the window using
ØÙ
Ctrl
+
ØÙ
W
{
ØÙ
+
,
ØÙ
-
,
ØÙ
>
,
ØÙ
<
}
Sometimes you have constants in your program that you
want to modify. The shortcut
ØÙ
Ctrl
+
ØÙ
A
add one to the
number and
ØÙ
Ctrl
+
ØÙ
X
subtract one from the number.
The advantage of using this is your cursor don’t have to be
right on top of the number, it just search rst number after
the cursor on the same line and do the job. This function,
by default, recognize decimal numbers,
0
for octal numbers
and
0x
for hexadecimal numbers.
Constant manipulation
3.17
Navigation and Marks
3.18
You jump to elsewhere in a le or another le for various
reasons. Searching, or simply press the wrong key, brings
you away from where you are working. To go back, press
ØÙ
Ctrl
+
ØÙ
O
(‘oh’). To go forward, press
ØÙ
Ctrl
+
ØÙ
I
.
To mark a position, press
ØÙ
m
c
. This saves the loca-
tion in mark
c
. To go back to that location press
ØÙ
c
.
Perform a search using
ØÙ
/
pattern
.
3.20
Retabbing for printing
Search and Replace
Perform a replace using
:
r
s/
pattern
/
string
/
ags
where
r
denes the range
%
: the whole le
By default, tab is equal to 8 spaces when printed. This
causes printouts hard to read when you have more than a
few level of indentation. Retab is the solution. Perform
the following:
’ Specify how much spaces one tab is equivalent
’ to (Not required if you are happy with the
’ current settings)
:set ts=4
’ With expandtab, tabs are converted to spaces
:set expandtab
’ Replace tabs with white spaces
:ret
’ Save and do your printing, e.g.
’ lpr -Popenlab xx.c
’ With noexpandtab, spaces are converted to tabs
:set noexpandtab
’ Undo changes to your code using ’u’, or
:ret
ØÙ
g
ØÙ
d
and
ØÙ
g
ØÙ
D
go to local and global declaration re-
spectively. Place the cursor on a variable or a function and
these commands will bring you to the declaration. (Go
back using
ØÙ
Ctrl
+
ØÙ
O
)
Note that these function is not guaranteed to work as it
may bring you to the wrong place (e.g. when you dene
the function multiple times).
Display the denition of the current variable by
ØÙ
[
ØÙ
i
.
a,b
: from line
a
to line
b
’<,’>
: range equals to lines highlighted (in-
serted automatically in visual mode)
pattern
is the pattern to search for.
Vim
accepts
regular expression. Include
\c
in search pattern to
ignore case in search.
gr[ae]y
matches both gray and grey.
red\|blue
matches both red and blue.
^
matches beginning of line,
$
matches end of
line.
string
is the string to replace with. You can use
backreference (\1,
\2,
. . . ) to refer back to the string
matched (enclosed in
\(
. . .
\)).
:%s/ \([4-9]\)/\1th/g
replaces 4 with 4th, 5
with 5th, etc.
ags
modies the behavior of a search. You can
mix some of them.
3.21
Jump to declaration
3.22
Using
ctags
ctags
is a powerful utility help to analyze the code and
help the programming process. Create a tag le using
ctags:
ctags *.c *.h
The tag le defaults to
tags.
After that you can
use
ØÙ
Ctrl
+
ØÙ
]
to ask for declaration. Go back by
Ctrl
+
ØÙ
T
.
ctags
is more powerful than
ØÙ
g
ØÙ
d
and
ØÙ
ØÙ
g
ØÙ
D
, but requires update if code has changed. You can
incorporate the update in your
Makefile
to simplify the
work. A good tool for analysing code.
ØÙ
g
ØÙ
Ctrl
+
ØÙ
]
shows a list of matching tags. This is use-
ful to identify multiple denition. If you like you can map
ØÙ
Ctrl
+
ØÙ
]
to this in your vimrc:
nmap ^] g^]
A nice tutorial on
ctags
can be found at:
http://www.linuxjournal.com/article/8289
Contents
1 Issues related to new
Vim
7.0
1.1 Parenthesis matching . . . . . . . .
1.2 Keyword completion — disable the
1.3 Tab editing . . . . . . . . . . . . .
1.4 Spell checking . . . . . . . . . . . .
. . . .
menu
. . . .
. . . .
.
.
.
.
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
4
4
2 Regarding
GVim
2.1 A ‘clean’
GVim
. . . . . . . . . . . . . . . .
2.2 Fonts,
fonts,
fonts,
...
. . . . . . . . . . . . .
2.3 Clipboard . . . . . . . . . . . . . . . . . . .
3 Other bits
3.1 Paste Mode . . . . . . .
3.2 Copying from Terminal
3.3 Searching . . . . . . . .
3.4 Man page . . . . . . . .
3.5 Dictionary Lookup . . .
3.6 Joining line(s) . . . . . .
3.7 Executing commands . .
3.8 Matching parenthesis . .
3.9 Make C, C++ programs
3.10 Visual mode . . . . . . .
3.11 Code folding . . . . . .
3.12 Indenting code . . . . .
3.13 Using numbers . . . . .
3.14 Macros . . . . . . . . . .
3.15 Undo, Redo and Repeat
3.16 Splitting Window . . . .
3.17 Navigation and Marks .
3.18 Search and Replace . . .
3.19 Constant manipulation .
3.20 Retabbing for printing .
3.21 Jump to declaration . .
3.22 Using
ctags
. . . . . . .
4
4.1
Finally...
Help! How do I...
What have been covered is just a very small portion of tips
and tricks. If you have other problems concerning
Vim,
try
the following:
Ask
vimtutor
if you are new to
Vim.
A quick reference card might help get you started.
Search, on the internet, using the term
"vim
quick reference card".
This should return you
some html formatted reference as well as prettily-
formatted pdf which is nice for printouts. A graph-
ical cheat sheet is also available ("vim
graphical
cheat sheet").
Use
:help.
For example,
:help ts
should tell you
what is mean by
ts
(in your vimrc). Navigate your-
self by using the cursor keys (
ØÙ
h
ØÙ
j
ØÙ
k
ØÙ
l
), follow
a link (enclosed in pipes,
|link|
for example) us-
ing
ØÙ
Ctrl
+
ØÙ
]
, go back by
ØÙ
Ctrl
+
ØÙ
T
. Wander
around the help should increase your skill in using
Vim.
(But remember, learn through practice)
As mentioned, internet is a good place to look for
help. In particular, on
www.vim.org
there are lots
of tips and scripts. Scripts for eye-catching colour
schemes, syntax highlighting for other languages are
few examples.
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
in
Vim
.
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4 Finally...
4.1 Help! How do I... . . . . . . . . . . . . . . .
First edited: October 6, 2006.
Last modied: February 11, 2009.
Zgłoś jeśli naruszono regulamin