|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
windows下很多人都使用source insight 编写和查看代码。linux下可以使用VIM,刚开始会觉得VIM像windows下的记事本,而如果使用得当,它并不比source insight 逊色。: I2 d, }1 G, O% s* ?7 _( t9 ~
在这里,我会尽我所能细致地讲清楚如何把vim变成source insight, 然而你仍然需要积极地思考,并且必须自己去摸索一些东西。8 O) P w* i# G; o+ ^
为了避免过于罗嗦,我把基础的部分放在后面,如果你越看越觉得太简单了,那么本文并不适合你;如果看完前面的仍有疑问或者看不懂前面说的是什么东西,不用担心,后面会有一些必备的知识介绍。
r' u/ h" Q5 F, P: k) D5 ~* \
3 E8 ^3 p( ^7 J/ ~' z2 e一、用好系统自带软件ctags
i: Z0 q( F8 c1 J$ o大部分的unix系统都有ctags软件,它能跟vim很好地合作。, V# m0 ~1 b9 h9 N7 J% _3 y, Z
用途:2 Y1 x, D% V& l7 d8 w, V
生成c语言的标签文件,实现相关c文件之间的跳转。8 W4 l( d4 b/ A; U( s& ?4 N
用法:+ h2 X4 j: Y( ~1 h9 f5 f& E% _/ I1 i7 i M
1.生成标签文件
- }- w7 t& X9 x( L6 w( l+ f 在当前目录下(运行$提示符后面的命令): 3 I/ e+ G* q8 G( [8 P$ @
$ctags -R .8 D& R6 g9 t Y& n, J: O. _
-R表示recursive,递归,为当前目录及其子目录中的c文件生成标签文件。最后一个.表示在当前目录。
: a! V! C+ ^( O: T: U 运行完当前目录会多一个文件tags,就是c标签的索引文件。
0 ?! G% @& C, I$ O" ^6 t
, E8 t* u+ W' T. p# u# M 2.跳转6 [/ C8 Q$ M: \) K
1)用vim打开一个已经建过标签的c文件
, L, t9 v0 I& A 2)ctrl+] 找到光标所在位置的标签定义的地方1 ^9 q; W+ E4 V C
3)ctrl+t 回到跳转之前的标签处
% g3 O2 b: l2 ` 注意:此时运行vim,必须在"tags"文件所在的目录下运行。否则,运行它会找不到"tags"文件,而需要在vim中用":set tags="命令设定"tags"文件的路径。对于一个稍微大点的项目,你可能在任何一个目录下打开vim,然而在每个目录下都生成一个tags文件并不 是个好主意,那么如何解决呢?方法是在.vimrc中增加一行:
1 Z; s4 h1 T) T+ M# c set tags=tags;/
- j9 S4 e: Q- { 这是告诉vim在当前目录找不到tags文件时请到上层目录查找。
6 k" L( G: \0 x. K/ F, w; C1 r6 T+ j- W! |+ D8 M k; Q
二、需要额外安装的脚本:$ R9 k3 W# _. B7 X6 Z
! T# B, y/ @1 B! u! \" V; |& C& I1、taglist. {7 }5 p& X3 \
下载地址http://www.vim.org/scripts/script.php?script_id=273/ t# O$ W4 W9 k/ `/ s5 g
若你下载时地址已改变,请到 www.vim.org 找到正确的地址,这很简单。
, L( A/ r$ o( F, g( E. X: T$ r0 h9 L0 }* |7 u. J, y5 h
用途:* G1 S0 F; X' Q- w
打开后,可以显示源码的整体架构,方便地进行跳转。(用惯source insight的人一定勾起某些回忆了^_^)
+ v1 e& W. Q2 [6 p' Z m& |用法:
1 D! e' W. s8 n7 h0 s e6 h8 v 下载插件并安装,使用时在vim中输入命令9 t G3 m- ^$ h
:Tlist! F; p3 \$ p0 Z. Y* z4 f; |
即可打开/关闭taglist窗口。0 W, v4 @' a9 o+ g
一个简单的方法是设定快捷键,在.vimrc中增加一行:
% o8 p4 C8 K' N0 s1 c nnoremap <silent> <F8> :TlistToggle<CR>; b) K% w8 {6 @: ]
这样在vim中按F8就可以打开/关闭taglist了。
( ]6 |9 M- V% j2 b7 U8 | 更多相关配置请看后面关于.vimrc的介绍。
2 l# G& H, M' `3 B
6 {9 T Q# D3 [ F+ L1 u# J6 S三、基础知识探讨
8 }$ O4 _# c3 ?" l! P- S, G4 q! \3 W! T; n, Y# O
约定:为了方便和准确,我们约定本文中"$"标示符后的命令为在终端下运行,而":"后的命令为在vim中运行。" ^* i' q9 b! [
5 K3 p# S1 N( g6 I8 p. @VIM的配置文件一般放在用户主文件夹下,也就是非root状态时在终端运行
' H. R7 o: S) l- `: ~ $cd ~/8 a1 [ g' E4 e5 y' e$ \) U
会到的那个目录,文件名为.vimrc。0 ^; V9 c: ~& c
看不到?有两个可能:
/ `# N$ n7 g2 v 1、文件名前面有一个点,表示是隐藏文件,ls查看时需加-a选项。1 p* \8 U; j( Q0 i( E9 ]
$ls -a9 \6 }! X- C$ S# S- B+ w% V
2、你还没有建.vimrc文件,自己创建一个就行,先建个空的吧,以后可以不断往里面填东西。+ V/ R# A/ N6 Y7 d0 j/ `
$touch .vimrc J8 G* l8 D( B
7 H: k9 o6 R- g6 h
主文件夹下还有一个.vim文件夹,没有请自己mkdir
E( }3 H- n( Q+ q2 Z. x $mkdir ~/.vim; ?' m1 S' z+ b! S
在.vim文件夹下,再建两个子文件夹:plugin和doc; A( D& I) ?) s2 q6 B( a/ z2 U
$mkdir ~/.vim/plugin: x) M( E3 y9 m+ Y) {3 i9 u1 L
$mkdir ~/.vim/doc
/ P* |' J" R7 P* X) |* H plugin文件夹下放插件,doc文件夹下放相应的help文档。6 A. ]0 m3 B6 @7 F% X; W
去下一个taglist吧(我们应该把它叫作脚本还是插件呢?它是放在plugin文件夹下的,那么应该是插件;而在vim.org,它是作为scripts存在,那么应当是脚本。),我们当作例子来请解。
. c9 ^8 e4 Y5 B0 b3 A2 S 下载的是一个zip包,把它放在 ~/.vim 目录下,然后 J f- {" m$ k8 l
$unzip filename.zip* Q: \! N+ n1 O2 v7 I, x
它已经自动把taglist.vim和taglist.txt分别放到plugin、doc文件夹下了。) @& l9 }( x8 T( _) A
这时重新启动vim4 W. A: u+ n& ~3 _
$vim8 d) ^% j- N$ Q7 K8 h- [. ^; {
运行4 P _ o$ S w
:Tlist
. R3 ?/ G b+ d# [7 h 发现旁边多了一栏没有?如果你打开的是c文件,并且已经生成了tags文件,那么里面应当会显示一些有用的信息。
/ A4 L8 S" W- L" f 这个时候,taglist的help文档已经在 ~/.vim/doc 目录下了,但是你在vim下敲% d' |, i: x3 m$ s( ]2 u& c
:help Tlist/ O8 r: ^0 Z, Z- K7 m
却没有任何反应,那是因为vim还没有获取帮助文档里面的tag,解决方法是在vim下
( g( }, H% s3 Y) m- S4 J3 L :helptags ~/.vim/doc/ L( E* M: y6 c. {* j3 a0 h8 j; j
现在,你再
- Z, H+ v7 v8 ^. U' v: T% _1 G :help Tlist2 e' R6 p( X; x0 H9 F! L* G
看看有没有反应?
! H1 O' X" t# a0 }
2 a' k' l/ |6 P2 m H+ \8 [ 关于.vimrc
% I; O" A4 ^" T; Y* p2 h. G8 X 我自己的.vimrc也在不断地完善中,完善的过程中得益于从网络上获取的很多知识,感谢提供信息的朋友,也是他们促使我写下这篇东西。我把自己.vimrc的一部分贴在下面,你可以把这些根据需要加到你的.vimrc里面去。" a) {$ H ?; X L- x# D
/ j9 o+ `/ o: c) ]6 G/ E0 S% V".vimrc1 s9 A4 K! k, S( Q' K. b
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""5 l. d6 h; s8 p* K7 }$ h1 c
" General
0 ^' C J4 _; g( h"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
/ q/ Y) c4 V+ C* @, d. z"For ctags, then it can find the 'tags' file even not in current directory6 Q% e, ~/ P1 V6 U% R; n4 I
set tags=tags;/9 ^6 O5 }. R# w1 m
3 P% H0 ~9 I3 ~ D, @"Get out of VI's compatible mode..4 ]- c# m/ W6 l2 g1 x2 P1 `: z: Y
set nocompatible7 ^$ n/ P! c& i( l d
1 {: T; g# w1 d& h$ g1 g' P3 q"Sets how many lines of history VIM har to remember5 F# _* N: B) s! E( k6 @: O4 D
set history=400. P* d$ n6 `' h) H. [
- ?4 e' N* i! m+ k7 M"Set to auto read when a file is changed from the outside) I0 I9 K! p( }1 R0 P* u* }
set autoread
$ K/ k* P$ a' F: H, \0 a. \: l9 H( J2 }5 W
"Have the mouse enabled all the time:& {! R- p+ d* S1 k) `5 r' }
"when you need to copy from vim, maybe you have to ':set mouse=' first _, V, `' F) V. L: ]- m2 D8 X
set mouse=a
& e, X; [* Y7 `
: ]3 j9 B) m! \% a1 a& K+ }"""""""""""""""""""""""""""""""""""""4 l2 e0 @) T: t6 J
" Colors and Fonts
+ w; Z/ B( b) z) W"""""""""""""""""""""""""""""""""""""" g+ g" m5 g+ f/ c% [
"Enable syntax highlight2 W4 S8 k0 v- N8 d! w
syntax enable
" g: u3 @' \7 H) z6 b( {2 h$ o( c6 o' I3 _2 {
"set colorscheme( d2 X9 `6 }. |" ~! t8 w
colorscheme elflord7 H1 h" o# R6 o7 a: u8 D0 q$ T
"endif' v$ O' Z' _" C U1 h$ K# m
& p" h1 {: y# k7 B
"""""""""""""""""""""""""""""""""""""
( d% @2 ~- i% e9 Q7 j7 }+ ?& I" VIM userinteRFace
' N5 _" @2 }5 K"""""""""""""""""""""""""""""""""""""
3 O }: c, p- I$ K w"Set 7 lines to the curors away from the border- when moving vertical..
* d6 z% x+ ~! N( O1 r5 Y. jset so=7
, u& Y) e8 `6 J# q9 e0 d1 D& w: i0 M
"Turn on WiLd menu
" T/ U. }$ b' ? y# m& k7 h0 M: Aset wildmenu) r( o- y5 ^" U' }: Z' r- u* t1 M
4 l$ c1 {# m5 m z G8 w"Always show current position& f8 P8 L( m' U8 B! v- V
set ruler2 G5 V8 |; N* ~* [1 h& f
# k2 e7 d. C" A8 A8 F
"The commandbar is 2 high" Q" u9 i+ j! P% [' F
set cmdheight=2
0 {4 j% {8 Q q F0 g2 n C
3 j- R7 o* R6 x0 r: J0 b/ E8 E ?"Show line number( O+ @. z# N2 D9 C; |. Z% \7 i3 n
set nu
# A+ ~( H- E# n9 u) t2 r, F$ X- S, u2 H! N4 b) H
"Set backspace c' ]7 H2 {& v# ]1 }0 l
set backspace=eol,start,indent
$ `; `# E) n( u' W5 u! a( \0 x5 t
" y; P" H& l) ]& T- @7 |, m"Bbackspace and cursor keys wrap to( t2 n8 j! J) r7 c: y+ r. a! `
set whichwrap+=<,>,h,l7 |3 B2 A& {9 x
4 m9 _- v& V* I) O7 L
"show matching bracets/ X) j( e- X0 u9 q, e0 ~* g+ X( r0 W
set showmatch
V! E# y: O7 _7 L7 K
! y5 s- ~' @' k6 z"How many tenths of a second to blink! J5 Q+ b. |2 [7 W( u
set mat=2. F* M- l5 p( Z5 [- v8 h$ P
' ~+ C+ G) f9 G) t- w" w" G7 y
"Highlight search things; L& I- |3 @& h3 F2 K+ c
set hlsearch- o3 [+ S! E4 L& D
"imediately show the search result# @7 y2 J: Y' ^; h, i& [# [
set is
+ l; I8 [* Z4 g7 o6 ~& E6 {# A1 f& b
"""""""""""""""""""""""""""""""""""""; r7 |' U# }! r
" Folding
$ m$ i: J( z9 T# r) n6 X"""""""""""""""""""""""""""""""""""""
! H* [1 B' \8 X w+ j" A; q# j"Enable folding, I find it very useful2 J$ f; r6 s1 K: j* ?
set nofen: W7 c: R4 {+ c7 G# m {& i2 `5 s1 ]
set fdl=0
8 `5 h$ h0 Z: Z( Q) h7 z" P5 {$ y5 F- n) m* f9 k T
0 l" u+ r5 U ]; j
"""""""""""""""""""""""""""""""""""""6 m+ Y' _& O2 B* m: @& _, w
" Text options4 ]1 g$ {6 N# g) m( v( }5 B
"""""""""""""""""""""""""""""""""""""
) p/ s% O& b& L' n7 ~8 [set expandtab2 B6 Q3 P5 K( ^4 b- I4 k0 Q
set shiftwidth=2
' C# h6 W% X. K* b# fset ambiwidth=double) b1 ]! v$ U" W1 K B; \+ J
set smarttab) H3 u5 E. p, ]7 c
"Set Tab=4 spaces
o- a8 \4 [8 t! p2 M I6 C' T* c* g+ W; Sset ts=4
& ^; \" q( N u9 W5 t* _2 vset lbr) X4 T1 Y3 d$ E- b. j) [) i a: n( k) d
set tw=500: _ q6 a( ?# {8 s8 t" x( u
set selection=inclusive# q8 x, F3 a( R& L" v. Y% @" n
""""""""""""""""""""""""""""""5 W+ I" [+ }+ f+ G; i
" Indent
1 q3 N+ j5 ]$ o& v* g """"""""""""""""""""""""""""""1 v! p- h6 Y1 p9 |$ K
"Auto indent& p K3 x# S, C; k* y# @
set ai# J9 [$ }7 j$ p7 N; X
"Set auto indent width = 4 spaces% u. ]. v! s/ r* h A
set sw=4
1 o/ v( t3 A: q; f3 x( u$ Y" x d$ Y7 ^
"Smart indet
0 }9 z1 O' @. H. k set si0 J% p# _) Y( T9 o
0 ^9 U! u2 }% y" d8 j "C-style indenting( j" z( h- u% D0 A9 g" ]: M) K
set cindent "usage: select codes, press '=' key, the codes will autoindenting
7 \0 }: I4 h, T0 [( e/ S; v5 w+ ^. ^/ I* T5 r
"Wrap lines& X( h" P- c1 q$ g+ |
set wrap
9 Z8 ~# o1 `. i! t1 b, D# Z0 p9 O, K5 n% x: W$ O: s
"Encoding settings
) s4 Y9 r4 d x7 a, Z% ~if has("multi_byte")
# ^+ t1 j! d, k0 G H% E " Set fileencoding priority8 [; ~2 H, T2 E9 F. M. L
if getfsize(expand("%")) > 0
% |+ F, a) P. O% H set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin13 m3 p+ [, K8 e, C
else
9 Y5 Q$ G i% S* Q9 q. n set fileencodings=cp936,big5,euc-jp,euc-kr,latin1
5 X/ x' R" U" k8 Y3 x5 T endif
/ C9 y0 f' G* f& I C* E
# A# J9 c, `* | M8 V " CJK environment detection and corresponding setting) k' S- F3 X# X* c: e
if v:lang =~ "^zh_CN"1 g0 ]8 e" Y# u: b( C
" Use cp936 to support GBK, euc-cn == gb2312
4 e6 ]% q B" | set encoding=cp936
0 C( i" @( q7 H) M set termencoding=cp936
4 b% U, e' Q( @" S: ? g0 T. Q set fileencoding=cp936
; T( K# T( P2 {- D: t elseif v:lang =~ "^zh_TW"; B4 t2 L B2 V" o# d) x1 t
" cp950, big5 or euc-tw
( B9 a: T$ b+ ^- v8 q1 h) H " Are they equal to each other?
9 i2 R6 S6 _% V; X; L# V set encoding=big5
, `& t" y2 J2 R set termencoding=big5% x! ?: n" @; W. P; b) z# x
set fileencoding=big5( O, t9 c9 ]& Y* y9 h, B7 r! I0 [1 {
elseif v:lang =~ "^ko"( Q2 E" ?, U& _6 o# v! H
" Copied from someone's dotfile, untested
, G% L6 ?3 @ A, ~8 G; Q/ `$ j set encoding=euc-kr# M" ^6 L+ |& ^/ e- Y
set termencoding=euc-kr' D% s) D( P0 K5 L2 ]' ^
set fileencoding=euc-kr
& I" ~: k0 b% R5 w elseif v:lang =~ "^ja_JP"7 `" @4 v a" U, d
" Copied from someone's dotfile, unteste
& N& S: f& P! H! K9 H! o set encoding=euc-jp8 n1 W( Y- P" ~: E* \
set termencoding=euc-jp
1 v E, z1 @9 r" a! L- J4 P set fileencoding=euc-jp3 @, s- I& V# R
endif' G& l5 O) v3 ]6 b
" Detect UTF-8 locale, and replace CJK setting if needed2 k5 D, }6 t- X- Z6 u* \& D8 ~
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
9 h! q3 d! [6 C; [% O+ @' ~) Y set encoding=utf-8
) z$ p, A/ P: c, P/ g1 Q8 w2 f set termencoding=utf-8
& v; g" R9 \+ h3 v7 N set fileencoding=utf-81 Z! B$ A& L/ W# s8 }
endif
2 a! [) X& r: _/ w) U# L: m3 Oelse
2 I; l7 p0 d$ E echoerr "Sorry, this version of (g)vim was not compiled with multi_byte": A6 V, {+ g5 T% Q
endif) a+ n" q0 k& _6 ^. w
' K" y& @$ Y' h
$ l9 {1 ]6 J6 h* Q/ e""""""""""""""""""""""""""""""""""""": g6 l# A1 Z3 q
"plugins. }5 b. E5 a q& e; l Z$ \$ |
' C& K% |6 N) D. H5 I2 u
"""""""""""""""""""""""""""""""""""""* T4 V3 T) E% r2 Y* F
" Tlist8 Z$ M. i5 H$ y* C9 p
if &diff
. ~6 k* z& |8 C5 B5 c, l) Flet Tlist_Auto_Open=0 "don't auto pen when compare two files
3 `, M. Q; \7 R3 H6 Gelse
% W0 E4 G( O1 h1 {: ?let Tlist_Auto_Open=1 "auto pen Tlist when open a file9 p9 E d" k2 o2 I5 M7 i4 U& s
endif+ ]4 d; `7 V; @3 C1 o
5 J/ K1 F' a$ W. u
"set taglist window in right, delete the following line if you don't like
/ k0 k' ~0 p+ ^. g5 @& q8 glet Tlist_Use_Right_Window=1
+ [& A1 {# V( k4 ^let Tlist_Auto_Update=1
0 q0 I) j2 M# {! Slet Tlist_File_Fold_Auto_Close=12 a9 ?" s" I M" Z
"auto close Tlist when exiting file.
: M* l' G% T2 |& q! W qlet Tlist_Exit_OnlyWindow = 1
$ ^' I6 w& Q' D
* l# s. R- e' F2 r' tnmap <F7> :copen<CR>/ m) V$ |& ^% }" B. c0 K/ q- g7 \
nmap <F6> :cclose<CR>4 s% A" F0 [8 G0 V- _
3 x" K3 U9 G. O( n1 w( n K |
|