暂无图片
暂无图片
暂无图片
暂无图片
暂无图片
vim.pdf
33
4页
4次
2024-02-17
免费下载
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.
:set is turns on incremental search.
3.4 Man page
 
 
K
shows you the man page for the keyword under cursor.
3.5 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 Joining line(s)
 
 
J
join the current line with the next, and
 
 
g
 
 
J
do the
same without inserting a space.
3.7 Executing commands
:!hcommandi executes hcommandi 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.
3.8 Matching parenthesis
 
 
%
is used to match parenthesis. It can match (), [], {},
comment (/* */) and #define.
3.9 Make C, C++ programs in Vim
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 flags. 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).
3.10 Visual mode
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 differenti-
ate itself from other editors. A triple click (yes!) and drag
invoke this.
3.11 Code folding
Instead of splitting your window to view source code from
different 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
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 hnumber of linesi
 
 
=
 
 
=
. To indent the whole file, type
 
 
g
 
 
g
 
 
=
 
 
G
(go to the top, then indent until the end of
file.
3.13 Using numbers
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 file, while hni
 
 
G
send
you to the hni’th line.
 
 
%
match parenthesis, while hni
 
 
%
send you to the
hni’th percentile of the file.
3.14 Macros
Macros are (one of the) most powerful feature in Vim. Be-
gin recording a macro by pressing
 
 
q
hci, where hci 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
 
 
@
hci. Using macros can increase pro-
ductivity a lot, saving your time in doing some repetitive
work.
3.15 Undo, Redo and Repeat
Undo by
 
 
u
, undo the whole line by
 
 
U
.
Redo by
 
 
Ctrl
+
 
 
R
.
 
 
.
repeats the last action. This can be a sequence
of actions you do in insert mode.
3.16 Splitting Window
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 first window.
Combining split and edit command, we have :new and
:vnew. Cycle through the list of files 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
{
 
 
+
,
 
 
-
,
 
 
>
,
 
 
<
}
3.17 Navigation and Marks
You jump to elsewhere in a file or another file 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
hci. This saves the loca-
tion in mark hci. To go back to that location press
 
 
hci.
3.18 Search and Replace
Perform a search using
 
 
/
hpatterni.
Perform a replace using
:hris/hpatterni/hstringi/hflagsi
where
hri defines the range
% : the whole file
a,b : from line a to line b
’<,’> : range equals to lines highlighted (in-
serted automatically in visual mode)
hpatterni 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.
hstringi 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.
hflagsi modifies the behavior of a search. You can
mix some of them.
g Replace all occurrence in the line. Without
this only the first matched string in each line
will be replaced.
c Confirm each substitution.
n Just report the number of matches, substitu-
tion is not done. (version 7.0 or above)
3.19 Constant manipulation
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 first 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.
3.20 Retabbing for printing
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
3.21 Jump to declaration
 
 
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 define
the function multiple times).
Display the definition of the current variable by
 
 
[
 
 
i
.
3.22 Using ctags
ctags is a powerful utility help to analyze the code and
help the programming process. Create a tag file using
ctags:
ctags *.c *.h
The tag file 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
of 4
免费下载
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文档的来源(墨天轮),文档链接,文档作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。