vim——编辑利器

vim——编辑利器

关于vim,其实是一个永远聊不完的话题。

首先是编辑器之争,很可能又是vim和emacs之争,如果来到这里,那相比你已经认可vim了。
其实就编辑来说,我觉得用那个编辑器不是问题,重点在于用着顺手吧。
vim也是一样,有的人会说,嗯,你应该加上这个插件,有的人又说配这个插件功能很强大,关键还是自己用着顺手啊,没有任何一个配置是适合所有人的。所以说,授人以鱼不如授人以渔

那么vim的配置方法又如何呢,可以说相当的不简单
不同的人在不同的时间所钟情的配置方法各不相同,因为不同的人有不同的喜好,而相同的人又随着使用vim的深入,对各种功能的探索也愈盛,对vim的需求也会变化。

关于“渔”我就不多说了,后面的那个文档可以说是圣经级别的了。
而我是一名程序员,提下和程序相关的一点内容呗。

  • 移动用的熟练了,hjkl绝对是王道,使用vim的vimer们恐怕没有任何一个让不认可这一点吧,我觉得这是vim最吸引我的一个特性,以至于我浏览网页及看pdf文档时都想要使用着几个按键。还好有firefox和chromium的vim插件,而debian发行版带的pdf阅读器也是支持这四个键跳转的,神奇啊。看文档时基本可以摆脱鼠标之束缚了。。。
  • 跳转%在括号间跳转啊,[[及][在函数首尾跳转啊,gg及G在文件首尾跳转啊,再有m..加标记与~..跳到标记,~n打开上几次的位置啊这些,熟练运用绝对可以大大提高效率的。
  • 列编辑呃,也许会用到吧,ctrl+v进入block visual模式,选择多列后使用I进入多列的插入模式,插入的字符就会自动在多列了。
  • 中文编码
    与编码相关的几个变量
    1.encoding屏幕显示的编码,如使用utf-8做locale的系统,encoding就应是utf-8以方便显示。
    2.fileencodings供vi尝试的编码列表,vi会逐个尝试每一项,如果没有发生错误,就设置当前的fileencoding为与该项相同的值。如果均失败,fileencoding将为空。
    3.fileencoding正在被编辑的文件的编码,它也决定新文件的编码。如果为空,表示与encoding相同。如果与encoding不同,vi将会在保存和读取时做二者之间的转换。因此可做如下设置:

    " 设置新文件的编码为 UTF-8
    set fileencoding=utf8 
    " 自动判断编码时,依次尝试以下编码:
    set fileencodings=ucs-bom,gb18030,utf-8,default 
    " gb18030 最好在 UTF-8 前面,否则其它编码的文件极可能被误识为 UTF-8
    
  • 空白的处理
    与缩进相关的几个设置如下:

    变量名             缩写          含义
    (no)autoindent     ai    自动缩进,即为新行自动添加与当前行同等的缩进。
    (no)cindent       ci     类似C语言程序的缩进
    (no)smartindent    si    基于autoindent的一些改进
    

    与tab相关的几个设置项如下:

    变量名          缩写      含义
    tabstop=X        ts      编辑时一个TAB字符占多少个空格的位置。
    shiftwidth=X     sw      使用每层缩进的空格数。
    (no)expandtab    (no)et      是否将输入的TAB自动展开成空格。开启后要输入TAB,需要Ctrl-V<TAB>
    softtabstop=X    sts         方便在开启了et后使用退格(backspace)键,每次退格将删除X个空格
    (no)smarttab     (no)sta         开启时,在行首按TAB将加入sw个空格,否则加入ts个空格。
    

    这样只要结合自己的需要作相应的设置就可以了。* 帮助
    vim的绝大多数的设置,在你不清楚时可以使用:help xxx来获得帮助吧。

其他的如跳转啊,搜索替换啊,复制粘贴啊等等的就不多说了,反正文档里也有:
vim用户手册中文版
另附上我的vimrc脚本以供参考哈,莫笑~~
(另需安装tagbar插件,请自行去vim官网下载,安装方法:so % )
[My vimrc]

  1 
  2 " When started as "evim", evim.vim will already have done these settings.
  3 if v:progname =~? "evim"
  4     finish
  5 endif
  6 
  7 " Use Vim settings, rather than Vi settings (much better!).
  8 " This must be first, because it changes other options as a side effect.
  9 set nocompatible
 10 
 11 " allow backspacing over everything in insert mode
 12 set backspace=indent,eol,start
 13 
 14 set history=50      " keep 50 lines of command line history
 15 set ruler       " show the cursor position all the time
 16 set showcmd     " display incomplete commands
 17 set incsearch       " do incremental searching
 18 
 19 " For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
 20 " let &guioptions = substitute(&guioptions, "t", "", "g")
 21 
 22 " Don't use Ex mode, use Q for formatting
 23 map Q gq
 24 
 25 " CTRL-U in insert mode deletes a lot.  Use CTRL-G u to first break undo,
 26 " so that you can undo CTRL-U after inserting a line break.
 27 inoremap <C-U> <C-G>u<C-U>
 28 
 29 " Switch syntax highlighting on, when the terminal has colors
 30 " Also switch on highlighting the last used search pattern.
 31 if &t_Co > 2 || has("gui_running")
 32     syntax on
 33     set hlsearch
 34 endif
 35 
 36 " Only do this part when compiled with support for autocommands.
 37 if has("autocmd")
 38 
 39     " Enable file type detection.
 40     " Use the default filetype settings, so that mail gets 'tw' set to 72,
 41     " 'cindent' is on in C files, etc.
 42     " Also load indent files, to automatically do language-dependent indenting.
 43     filetype plugin indent on
 44 
 45     " Put these in an autocmd group, so that we can delete them easily.
 46     augroup vimrcEx
 47         au!
 48 
 49         " For all text files set 'textwidth' to 78 characters.
 50         autocmd FileType text setlocal textwidth=78
 51 
 52         " When editing a file, always jump to the last known cursor position.
 53         " Don't do it when the position is invalid or when inside an event handler
 54         " (happens when dropping a file on gvim).
 55         " Also don't do it when the mark is in the first line, that is the default
 56         " position when opening a file.
 57         autocmd BufReadPost *
 58                     \ if line("'\"") > 1 && line("'\"") <= line("$") |
 59                     \   exe "normal! g`\"" |
 60                     \ endif
 61 
 62     augroup END
 63 
 64 else
 65 
 66     set autoindent      " always set autoindenting on
 67 
 68 endif " has("autocmd")
 69 
 70 " Convenient command to see the difference between the current buffer and the
 71 " file it was loaded from, thus the changes you made.
 72 " Only define it when not defined already.
 73 if !exists(":DiffOrig")
 74     command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
 75                 \ | wincmd p | diffthis
 76 endif
 77 
 78 set backspace=indent,eol,start
 79 
 80 """"""""""""""""""""""""""""""
 81 " BufExplorer
 82 """"""""""""""""""""""""""""""
 83 let g:bufExplorerDefaultHelp=0       " Do not show default help.
 84 let g:bufExplorerShowRelativePath=1  " Show relative paths.
 85 let g:bufExplorerSortBy='mru'        " Sort by most recently used.
 86 let g:bufExplorerSplitRight=0        " Split left.
 87 let g:bufExplorerSplitVertical=1     " Split vertically.
 88 let g:bufExplorerSplitVertSize = 30  " Split width
 89 let g:bufExplorerUseCurrentWindow=1  " Open in new window.
 90 autocmd BufWinEnter \[Buf\ List\] setl nonumber
 91 
 92 " Something about project manage with vim
 93 " autosave
 94 "au VimLeave *.[chs],*.cc mksession! .proj.session
 95 "au VimLeave *.[chs],*.cc wviminfo! .proj.viminfo
 96 
 97 if filereadable("tags")
 98     set tags+=tags
 99 endif
100 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
101 " cscope setting
102 " """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
103 if has("cscope")
104     set cscopetag
105     set csto=1
106     set cst
107     set nocsverb
108     " add any database in current directory
109     if filereadable("cscope.out")
110         cs add cscope.out
111     endif
112     set csverb
113 endif
114 if filereadable("tags")
115     set tags=tags
116 endif
117 
118 set cscopequickfix=s-,c-,d-,i-,t-,e-
119 nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>
120 nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>
121 nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>
122 nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>
123 nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>
124 nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
125 nmap <C-@>i :cs find i <C-R>=expand("<cfile>")<CR>$<CR>
126 nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>
127 
128 let g:tagbar_width = 30
129 let g:tagbar_autoclose = 1
130 let g:tagbar_autofocus = 1
131 nmap <F2> :TagbarToggle<CR>
132 "set background=dark
133 noremap <F3> :set nonumber!<CR>:set foldcolumn=0<CR>
134 
135 " tab占用空格位置数
136 set tabstop=4
137 " 每层缩进占用空格数
138 set shiftwidth=4
139 " tab自动展开为空格
140 set expandtab
141 " 每次退格删除x个空格
142 set softtabstop=4
143 " 行首按tab插入shiftwidth个空格,否则为tabstop个
144 set smarttab
145 
146 " no copy indent
147 set nocp
148 set autowrite
149 set hidden
150 set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
151 set number
152 
153 "show cur file name
154 set statusline+=%f 
155 " auto scoll when 7 lines to the boundary
156 set so=7
157 " show matched '({['
158 set showmatch
159 "auto fold
160 set fdm=syntax
161 set nofen
162 " search ignore case
163 set ignorecase
164 
165 
166 " auto preview,maybe no use
167 set previewheight=10
168 set updatetime=2000
169 nmap <C-@>p :call PreviewWord()<CR>1<CR><CR>
170 "au! CursorHold *.[ch] nested call PreviewWord()
171 func PreviewWord()
172     if &previewwindow           " don't do this in the preview window
173         return
174     endif
175     let w = expand("<cword>")       " get the word under cursor
176     if w =~ '\a'            " if the word contains a letter
177 
178         " Delete any existing highlight before showing another tag
179         silent! wincmd P            " jump to preview window
180         if &previewwindow           " if we really get there...
181             match none          " delete existing highlight
182             wincmd p            " back to old window
183         endif
184 
185         " Try displaying a matching tag for the word under the cursor
186         try
187             exe "ptag! " . w
188         catch
189             return
190         endtry
191 
192         silent! wincmd P            " jump to preview window
193         if &previewwindow       " if we really get there...
194             if has("folding")
195                 silent! .foldopen       " don't want a closed fold
196             endif
197             call search("$", "b")       " to end of previous line
198             let w = substitute(w, '\\', '\\\\', "")
199             call search('\<\V' . w . '\>')  " position cursor on match
200             " Add a match highlight to the word at this position
201             hi previewWord term=bold ctermbg=green guibg=green
202             exe 'match previewWord "\%' . line(".") . 'l\%' . col(".") . 'c\k*"'
203             wincmd p            " back to old window
204         endif
205     endif
206 endfun
207 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值