您的位置:首页 > 编程语言

用GVim建立IDE编程环境(Win篇)

2013-09-12 16:59 225 查看

/content/1765086.html

用GVim建立IDE编程环境(Win篇)

11人收藏此文章, 我要收藏发表于8个月前(2012-12-20
00:25) , 已有484次阅读 ,共0个评论

用GVim建立IDE编程环境(Win篇)

2010-10-23 22:14:12| 分类: Vim |字号 订阅

说明:本文摘自网上,具体地址已忘记,在此感谢作者!

说明:本文是作者在完全按照著名的《手把手教你把Vim改装成一个IDE编程环境》一文,在WindowsXP上用gvim建立IDE环境时所作的备忘。

原作地址:http://blog.csdn.net/wooin/archive/2007/10/31/1858917.aspx

0.准备软件及插件。

(a)gvim72.exe

地址ftp://ftp.vim.org/pub/vim/pc/gvim72.exe

(b)vimcdoc-1.7.0-setup.exe

地址http://prdownloads.sourceforge.net/vimcdoc/vimcdoc-1.7.0-setup.exe?download

(c)ec57w32.zip

地址http://prdownloads.sourceforge.net/ctags/ec57w32.zip

(d)taglist_45.zip

地址http://www.vim.org/scripts/download_script.php?src_id=7701

(e)winmanager.zip

地址http://www.vim.org/scripts/download_script.php?src_id=754

(f)minibufexpl.vim

地址http://www.vim.org/scripts/download_script.php?src_id=3640

(g)a.vim

地址http://www.vim.org/scripts/download_script.php?src_id=7218

(h)grep.vim

地址http://www.vim.org/scripts/download_script.php?src_id=7645

(i)visualmark.vim

地址http://www.vim.org/scripts/download_script.php?src_id=4700

1、安装Gvim7.3 : 下载地址http://www.vim.org/download.php#pc

2、安装中文帮助:vimcdoc-1.8.0-setup.exe 地址http://vimcdoc.sourceforge.net/

会自动识别gvim的安装路径,

安装完后,gvim菜单中文出现乱码,在_vimrcset文件中增加:

" 配置多语言环境,解决中文乱码问题

if has("multi_byte")

" UTF-8 编码

set encoding=utf-8

set termencoding=utf-8

set formatoptions+=mM

set fencs=utf-8,gbk

if v:lang =~? '^/(zh/)/|/(ja/)/|/(ko/)'

set ambiwidth=double

endif

if has("win32")

source $VIMRUNTIME/delmenu.vim

source $VIMRUNTIME/menu.vim

language messages zh_CN.utf-8

endif

else

echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"

endif

输入:help,显示中文帮助,说明安装成功:





3、设置语法高亮

编辑安装目录下的_vimrc文件(例如:我的在D:\Program Files\Vim)

加入以下内容:

set nu!

colorscheme desert

syntax enable

syntax on

再打开gvim,打开一个源代码文件:





这些设置使得gvim可以显示行号,并使用了desert配色方案,而且打开了语法高亮功能(用不同颜色显示注释、关键字、字符串等)。

我们还可以让函数名也高亮起来,

这里对C、C++的代码进行配置:在D:\Program Files\Vim\vim73\syntax下找到 c.vim 和 cpp.vim,分别添加以下内容:

syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>[^()]*)("me=e-2

syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>\s*("me=e-1

hi cFunction gui=NONE guifg=#B5A1FF

重新打开gvim,效果如下:





4、程序中跳转

将ec57w32.zip解压,在解压后将文件夹中的ctags.exe复制到D:\ProgramFiles\Vim\vim73下,并编辑_vimrc文件,添加以下内容:

set tags=tags;

set autochdir

然后将D:\ProgramFiles\Vim\vim73加到环境变量的path中。在需要查看的源代码目录下执行命令ctags -R;然后再用gvim打开源代码文件:





按住“CTRL”键,点击对应的函数名或“CTRL+]”,会自动跳转到函数的定义部分,“CTRL+T”则返回;

5、源代码分析工具 taglist

将taglist_45.zip解压,解压后包含一个doc文件夹和一个plugin文件夹,将其中内容分别复制到d:\Program Files\Vim\vim73下的doc及plugin中。

在_vimrc文件中加入以下内容:

let Tlist_Show_One_File=1

let Tlist_Exit_OnlyWindow=1





用gvim打开代码文件(已生成过tags文件),输入:Tlist,TagList窗口即出现在左侧。再输入:

:Tlist,左侧栏消失;左侧栏列出了当前文件中的所有宏, 全局变量, 函数名等, 在查看代码时用这个窗口总揽全局, 切换位置相当方便





6、文件浏览组件 WinManager

winmanager.zip 地址http://www.vim.org/scripts/download_script.php?src_id=754

将winmanager.zip解压和拷贝,解压后包含一个doc文件夹和一个plugin文件夹,将其中内容分别复制到d:\Program Files\Vim\vim73下的doc及plugin中

在_vimrc文件中加入以下内容:

let g:winManagerWindowLayout='FileExplorer|TagList'

nmap wm :WMToggle<cr>

用gvim打开代码文件,normal状态下输入命令"wm",窗口如下:





左上角是浏览文件的窗口,左下角的是TagList窗口;

7、多文档编辑

minibufexpl.vim 地址http://www.vim.org/scripts/download_script.php?src_id=3640

解压后将将minibufexpl.vim复制到d:\Program Files\Vim\vim73\plugin,在_vimrc中添加:

let g:miniBufExplMapCTabSwitchBufs=1

let g:miniBufExplMapWindowsNavVim=1

let g:miniBufExplMapWindowNavArrows=1

let g:miniBufExplorerMoreThanOne=0

用GVIM打开多个源代码文件后,如图:





ctrl+Tab,切换到前一个buffer,并在当前窗口打开文件;

ctrl+shift+Tab,切换到后一个buffer,并在当前窗口打开文件;

ctrl+箭头键,可以切换到上下左右窗口中;

ctrl+h,j,k,l,切换到上下左右的窗口中。

8、在工程中快速查找

grep.vim 地址http://www.vim.org/scripts/download_script.php?src_id=7645

解压后把grep.vim文件拷贝到d:\Program Files\Vim\vim73\plugin,在_vimrc中添加:

nnoremap <silent> <F3> :Grep<CR>

因为windows下是不带grep的,需要下载Grep for Windowshttp://gnuwin32.sourceforge.net/packages/grep.htm

下载后在环境变量中增加grep的路径;

用gvim打开一个源代码文件,光标选择需要查找的内容,按F3,确定要查找的内容和搜索范围,gvim会在弹出的QuickFix窗口中列出所有符合条件的搜索结果





9、h\c切换(针对C、C++)

下载插件:a.vim 地址http://www.vim.org/scripts/download_script.php?src_id=7218

将a.vim复制到d:\Program Files\Vim\vim73\plugin,在_vimrc中添加:

nnoremap <silent> <F12> :A<CR>

用gvim打开源码文件后,按F12即可以在c/h文件中切换,也可以通过输入:A实现。

10、高亮书签

下载插件:visualmark.vim 地址http://www.vim.org/scripts/download_script.php?src_id=4700

将visualmark.vim复制到d:\Program Files\Vim\vim73\plugin。

用gvim打开源码文件,将光标定位在需要添加书签的地方,按下ctrl+F2,即添加了书签。





使用F2在书签之间正向切换,shift+F2反向切换。

11、Python代码补全

下载插件pydiction-1.2.zip

解压后,将里面的python_pydiction.vim文件复制到D:\Program Files\Vim\vimfiles\ftplugin

将complete-dict 和 pydiction.py复制到D:\Program Files\Vim\vimfiles\ftplugin\pydiction

在_vimrc文件中增加配置如下:

filetype plugin on “启用filetype插件

let g:pydiction_location = 'D:\Program Files\Vim\vimfiles\ftplugin\pydiction\complete-dict'

let g:pydiction_menu_height = 20 ”设置弹出菜单的高度,默认是15

用gvim打开一个py文件,输入row+[tab],就可以看到自动补全的代码选项了





12、python编译

下载VimPdb,解压后拷贝VimPdb.py,VimPdb.vim到plugin即可。

用vim代开python代码文件,按F5运行,然后按F2设置断点,在运行到断点后可以用按F12可以查看Stack Trace,F3查看变量和参数值。





不过这种方法使用了一下,经常会死掉,不知道为什么,所以在网上找到了另外一种方法:

在_vimrc增加




python << EOF import time
import vim
def SetBreakpoint():
nLine = int( vim.eval( 'line(".")'))
strLine = vim.current.line i = 0 strWhite = "" while strLine[i] == ' ' or strLine[i] == "\t":
i += 1 strWhite += strLine[i] vim.current.buffer.append(
"%(space)spdb.set_trace() %(mark)s Breakpoint %(mark)s" %
{'space':strWhite, 'mark': '#' * 30}, nLine - 1)
for strLine in vim.current.buffer:
if strLine == "import pdb":
break
else:
vim.current.buffer.append( 'import pdb', 0)
vim.command( 'normal j1')
break
vim.command( 'map <C-M> :py SetBreakpoint()<cr>')

def RemoveBreakpoints():
nCurrentLine = int( vim.eval( 'line(".")'))
nLines = []
nLine = 1
for strLine in vim.current.buffer:
if strLine == 'import pdb' or strLine.lstrip()[:15] == 'pdb.set_trace()':
nLines.append( nLine)
nLine += 1
nLines.reverse()
for nLine in nLines:
vim.command( 'normal %dG' % nLine)
vim.command( 'normal dd')
if nLine < nCurrentLine: nCurrentLine -= 1 vim.command( 'normal %dG' % nCurrentLine)
vim.command( 'map <C-U> :py RemoveBreakpoints()<cr>')
vim.command( 'map <C-D> :!python %<cr>')
EOF





然后在要调试的代码里面用ctrl-M设断点

ctrl-D运行









13、Python代码检查

下载pyflakes,解压缩后把pyflakes.vim文件和pyflakes目录拷贝套ftplugin\python目录中,打开一个有问题的python源代码文件,执行命令:cc,即可进行代码检查:





set nocompatible "使用vim的键盘布局

source $VIMRUNTIME/vimrc_example.vim

source $VIMRUNTIME/mswin.vim

behave mswin

filetype on "侦测文件类型插件

filetype plugin on "为特定文件类型载入相关缩进文件

filetype indent on "为特定文件类型载入相关缩进文件

syntax on "语法高亮

syntax enable

colo torte "设置配色方案

set nu "设置行号

set autoread " 文件被改动时自动载入

set cursorline "高亮显示当前行

set nobackup "不要备份文件

set wildmenu "增加模式中的命令行自动完成操作

"可以在buffer的任何地方使用鼠标

set mouse=a

set selection=exclusive

set selectmode=mouse,key

set shortmess=atI "去掉启动的援助提示

set noerrorbells "取消vim的滴滴声

set showmatch "高亮显示匹配的括号

set matchtime=5 "匹配括号高亮的时间 十分之一秒

set ignorecase "搜索时忽略大小写,这个很实用

set incsearch "搜索时,输入的词句逐字符高亮

set ruler "右下角显示光标位置的状态行

set hlsearch "高亮显示搜索结果

set autoindent "继承前一行的缩进方式

set smartindent "开启新行是使用智能自动缩进

set cindent "使用c样式的缩进

set tabstop=4 "制表符为4

set softtabstop=4

set shiftwidth=4 "统一缩进为4

set noexpandtab "不要用空格代替制表符

set showmode "显示文本处理模式

set confirm "处理未保存或者只读文件时,给出提示

set fileencoding=utf-8 "文件保存编码

set fileencodings=utf-8,gb2312,gbk,gb18030,cp936 "文件载入时检测的编码

set guioptions-=T "注意
= 前面 有 - 号 ,隐藏不常用到的工具条

set guifont=YaHei_Consolas_Hybrid:h13:cANSI " 注意 ANS后面的字母是大写的 i 。。。如果自己安装了字体,在字体选择里面能看见你安装的font,例如它的名字是
YaHei Consolas Hybrid 的话,用下划线连接成:YaHei_Consolas_Hybrid,字母大小写也区分,否则不能识别。。。加载无效果,h后面的数字是字体大小,根据需 要设置即可。。

=====================================================

近段时间一直实用vim进行编程,常用的操作已然熟悉,感觉还不错,效率比起ue来要高,

开始感觉vim在windows下不能和ue相比的就是其不具备ftp功能,不能把windows下的文件

和linux的文件进行交互编辑,最近发现gvim7.2自带的netrw插件能实现这个远程编辑文

件的小功能,感觉还可以,同时把自己搜集整理的配置文件_vimrc做一个网络备份,用

了几个常用的插件功能:

minibuffer、nerd_commenter,SearchComplete,winmanager,perl-support,netrw

文件中有各种配置的详细中文说明,以后想要随时随地使用还可以参考vim的中文帮助文

档修改vimrc配置文件。

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" windows gvim设定

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

source $VIMRUNTIME/vimrc_example.vim

source $VIMRUNTIME/mswin.vim

behave mswin

set diffexpr=MyDiff()

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 一般设定

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 配置多语言环境,解决中文乱码问题

if has("multi_byte")

" UTF-8 编码

set encoding=utf-8

set termencoding=utf-8

set formatoptions+=mM

set fencs=utf-8,gbk

if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'

set ambiwidth=double

endif

if has("win32")

source $VIMRUNTIME/delmenu.vim

source $VIMRUNTIME/menu.vim

language messages zh_CN.utf-8

endif

else

echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"

endif

" set mapleader

"译注:映射快捷键开始命令为","号,g:表示全局设置.其使用方法见下

let mapleader = ","

let g:mapleader = ","

"那么这里可以用,w命令来代替:w!命令,亲自试一下,是不是方便多了,手不

"用再挪大半个键盘打几个键了.mapleader可以自由设定

nmap <leader>w :w!<cr>

nmap <leader>q :q<cr>

nmap <leader>wq :wq<cr>

if has("win32")

"本句的意思是快速重读配置文件,这样,就不需要关闭文件重开一下来读入

map <silent> <leader>s :source $vim/_vimrc<cr>

"译注:上面的那一句看懂了,这句就很简单了,这是快速打开配置文件

map <silent> <leader>e :call SwitchToBuf("$vim/_vimrc")<cr>

"译注:autocmd是自动执行命令,这句的意思是当配置文件被更改后重新

autocmd! bufwritepost _vimrc source $vim/_vimrc

else

map <silent> <leader>s :source ~/.vimrc<cr>

map <silent> <leader>e :call SwitchToBuf("~/.vimrc")<cr>

autocmd! bufwritepost .vimrc source ~/.vimrc

endif

" 不要使用vi的键盘模式,而是vim自己的

set nocompatible

" history文件中需要记录的行数

set history=1024

" 侦测文件类型

filetype on

" 载入文件类型插件

filetype plugin on

" 为特定文件类型载入相关缩进文件

filetype indent on

" 带有如下符号的单词不要被换行分割

set iskeyword+=_,$,@,%,#,-

" 语法高亮

syntax on

syntax enable

" 设置配色方案

colo torte

"显示行号

set nu

set lbr

" 设置当文件被改动时自动载入

set autoread

" 高亮当前行

set cursorline

" 让 gvim 支持 Alt+n 来切换标签页

autocmd VimEnter * call BufPos_Initialize()

" 译注:设置文件类型,是UNIX文件,DOS文件还是Mac文件

set ffs=unix,dos,mac "Default file types

" When you press gv you vimgrep after the selected text

vnoremap <silent> gv :call VisualSearch('gv')<CR>

" => Fuzzy finder

try

call fuf#defineLaunchCommand('FufCWD', 'file', 'fnamemodify(getcwd(), ''%:p:h'')')

map <leader>t :FufCWD **/<CR>

catch

endtry

"译注:本句的作用是移走Windows下的^M字符

noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm

"译注:在指定目录下创建一个临时文件

map <leader>q :e ~/buffer<cr>

" 用户目录变量$VIMFILES

if has("win32")

let $VIMFILES = $VIM.'/vimfiles'

else

let $VIMFILES = $HOME.'/.vim'

endif

"保存一下,是不是超酷的效果啊,颜色请自行配置,一定要和背景色有反差

" 光标十字架

if has("gui_running")

set cursorline

hi cursorline guibg=#333333

set cursorcolumn

hi CursorColumn guibg=#333333

endif

"译注:切换到当前目录

map <leader>cd :cd %:p:h<cr>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 文件设置

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 不要备份文件(根据自己需要取舍)

set nobackup

" 不要生成swap文件,当buffer被丢弃的时候隐藏它

setlocal noswapfile

set bufhidden=hide

" 增强模式中的命令行自动完成操作

set wildmenu

" 在状态行上显示光标所在位置的行号和列号

set ruler

set rulerformat=%20(%2*%<%f%=\ %m%r\ %3l\ %c\ %p%%%)

" 命令行(在状态行下)的高度,默认为1,这里是2

set cmdheight=1

" 使回格键(backspace)正常处理indent, eol, start等

set backspace=2

set backspace=eol,start,indent

" 允许backspace和光标键跨越行边界

set whichwrap+=<,>,h,l

" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)

set mouse=a

set selection=exclusive

set selectmode=mouse,key

" 启动的时候不显示那个援助索马里儿童的提示

"set shortmess=atI

" 通过使用: commands命令,告诉我们文件的哪一行被改变过

set report=0

" 不让vim发出讨厌的滴滴声

set noerrorbells

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 搜索和匹配

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 高亮显示匹配的括号

set showmatch

" 匹配括号高亮的时间(单位是十分之一秒)

set matchtime=5

" 在搜索的时候忽略大小写

set ignorecase

" 在搜索时,输入的词句的逐字符高亮(类似firefox的搜索)

set incsearch

" 输入:set list命令是应该显示些啥?

set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$

" 光标移动到buffer的顶部和底部时保持3行距离

set scrolloff=3

" 不要闪烁

set novisualbell

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"状态行显示内容

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"状态行显示内容

" %F 当前文件名

" %m 当前文件修改状态

" %r 当前文件是否只读

" %Y 当前文件类型

" %{&fileformat} 当前文件编码

" %b 当前光标处字符的 ASCII 码值

" %B 当前光标处字符的十六进制值

" %l 当前光标行号

" %c 当前光标列号

" %V 当前光标虚拟列号 (根据字符所占字节数计算)

" %p 当前行占总行数的百分比

" %% 百分号

" %L 当前文件总行数

" 我的状态行显示的内容(包括文件类型和解码)

set statusline=\ %F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L:%c

" 显示状态栏 (默认值为 1, 无法显示状态栏)

" 译注:默认情况下,只有两个以上的窗口才显示状态栏.其值定义为

" 本选项的值影响最后一个窗口何时有状态行:

" 0: 永不

" 1: 只有在有至少两个窗口时

" 2: 总是

set laststatus=2

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" => VIM userinterface

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"Set 7 lines to the curors - when moving vertical..

set so=7

"Do not redraw, when running macros.. lazyredraw

set lz

"Change buffer - without saving

set hid

"Set magic on

set magic

"How many tenths of a second to blink

set mat=2

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 文本格式和排版

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 自动格式化

"set formatoptions=tcrqn

" 继承前一行的缩进方式,特别适用于多行注释

set autoindent

"译注:开启新行时使用智能自动缩进

set smartindent

" 使用C样式的缩进

"set cindent

" 制表符为4

set tabstop=4

" 统一缩进为4

set softtabstop=4

set shiftwidth=4

" 不要用空格代替制表符

set noexpandtab

" 不要换行

"set nowrap

" 在行和段开始处使用制表符

set smarttab

" 设置每行120个字符自动换行

"set textwidth=120

" 折叠代码

set foldenable " 开始折叠

set foldmethod=syntax " 设置语法折叠

set foldcolumn=0 " 设置折叠区域的宽度

setlocal foldlevel=1 " 设置折叠层数为

set foldlevel=100 " 文件打开时不折叠

set foldclose=all " 设置为自动关闭折叠

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"菜单栏、工具栏显示与隐藏的切换

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set guioptions-=T

set guioptions-=m

map <silent> <F2> :if &guioptions =~# 'T' <Bar>

\set guioptions-=T <Bar>

\set guioptions-=m <bar>

\else <Bar>

\set guioptions+=T <Bar>

\set guioptions+=m <Bar>

\endif<CR>

"窗口最大化

au GUIEnter * simalt ~x "maximum the initial window

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"自动实例括号

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" vnoremap可视模式,使用$1来输入()

vnoremap $1 <esc>`>a)<esc>`<i(<esc>

" 使用$2来输入()

vnoremap $2 <esc>`>a]<esc>`<i[<esc>

" 使用$3来输入()

vnoremap $3 <esc>`>a}<esc>`<i{<esc>

" 使用$$来输入()

vnoremap $$ <esc>`>a"<esc>`<i"<esc>

" 使用$q来输入()

vnoremap $q <esc>`>a'<esc>`<i'<esc>

" 使用$e来输入()

vnoremap $e <esc>`>a"<esc>`<i"<esc>

inoremap $1 ()<esc>i

inoremap $2 []<esc>i

inoremap $3 {}<esc>i

inoremap $4 {<esc>o}<esc>O

inoremap $q ''<esc>i

inoremap $e ""<esc>i

"用于编辑中文的前后双引号,自己编辑常用的成对符号时,一定要注意,

"前面的符号放在<i后,后面的符号放在>a后,不要反了

vnoremap $w <esc>`>a"<esc>`<i"<esc>

"vnoremap $w <esc>`>a><esc>`<i<<esc>

"vnoremap $w <esc>'>a”<esc>`<i“<esc>

" 用空格键来开关折叠

set foldenable

set foldmethod=manual

nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" => Command mode related

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Smart mappings on the command line

cno $h e ~/

cno $d e ~/Desktop/

cno $j e ./

cno $c e <C-\>eCurrentFileDir("e")<cr>

" $q is super useful when browsing on the command line

cno $q <C-\>eDeleteTillSlash()<cr>

" 在vim冒号命令行可以像linux命令行一样使用ctrl+[aekpn]

cnoremap <C-A> <Home>

cnoremap <C-E> <End>

cnoremap <C-K> <C-U>

" 在命令行模式下使用alt+[jk]可以让光标后的文本向上或向下移动

" 在插入模式下使用ctrl+[jk]可以让光标后的文本向上或向下移动

" 上面两行是gvim自己就有的功能

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" => Moving around, tabs and buffers

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Map space to / (search) and c-space to ? (backgwards search)

"map <space> /

"map <c-space> ?

"map <silent> <leader><cr> :noh<cr>

" 译注:窗口分割时,进行切换的按键热键需要连接两次,比如从下方窗口移动

"光标到上方窗口,需要<c-w><c-w>k,非常麻烦,现在重映射为<c-k>,切换的

"时候会变得非常方便.

map <C-j> <C-W>j

map <C-k> <C-W>k

map <C-h> <C-W>h

map <C-l> <C-W>l

" 译注: ,bd命令快速关闭当前缓冲区文件

map <leader>bd :Bclose<cr>

" 译注: ,ba命令快速关闭所有缓冲区文件

map <leader>ba :1,300 bd!<cr>

" Tab configuration

map <leader>tn :tabnew %<cr> " 译注:将当前内容在新标签中打开

map <leader>te :tabedit " 译注:打开空白新标签

map <leader>tc :tabclose<cr> " 译注:关闭当前标签

map <leader>tm :tabmove " 译注:移动当前标签,使用方法为

" When pressing <leader>cd switch to the directory of the open buffer

map <leader>cd :cd %:p:h<cr>

" Specify the behavior when switching between buffers

"try

"set switchbuf=usetab

"set stal=2

"catch

"endtry

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 插件

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" minibufexpl插件的一般设置

"let g:miniBufExplMapWindowNavVim = 1

"let g:miniBufExplMapWindowNavArrows = 1

"let g:miniBufExplMapCTabSwitchBufs = 1

"let g:miniBufExplModSelTarget = 1

" minibuffer操作快捷方式!

nnoremap <TAB> :MBEbn<CR>

nnoremap <C-TAB> :MBEbp<CR>

"let g:bufExplorerDefaultHelp=0

"let g:bufExplorerShowRelativePath=1

"译注:右方向键切换到下一个缓冲区文件

" map <right> :bn<cr>

"译注:左方向键切换到上一个缓冲区文件

" map <left> :bp<cr>

"译注:删除一个缓冲区文件时,不关闭窗口

" plugin - NERD_commenter.vim 注释代码用的,

" [count],cc 光标以下count行逐行添加注释(7,cc)

" [count],cu 光标以下count行逐行取消注释(7,cu)

" [count],cm 光标以下count行尝试添加块注释(7,cm)

" ,cA 在行尾插入 ,并且进入插入模式。 这个命令方便写注释。

" 注:count参数可选,无则默认为选中行或当前行

"-----------------------------------------------------------------

let NERDSpaceDelims=1 " 让注释符与语句之间留一个空格

let NERDCompactSexyComs=1 " 多行注释时样子更好看

" win manager

" 布局规划变量

let g:winManagerWindowLayout = 'FileExplorer'

let g:winManagerWidth = 30

let g:defaultExplorer = 0

map <C-W><C-F> :FirstExplorerWindow<cr>

map <C-W><C-B> :BottomExplorerWindow<cr>

map <C-W><C-T> :WMToggle<cr>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 函数定义

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" if file not opened, create a new tab, or switch to the opened file

function! SwitchToBuf(filename)

" find in current tab

let bufwinnr = bufwinnr(a:filename)

if bufwinnr != -1

exec bufwinnr . "wincmd w"

return

else

" search each tab

tabfirst

let tb = 1

while tb <= tabpagenr("$")

let bufwinnr = bufwinnr(a:filename)

if bufwinnr != -1

exec "normal " . tb . "gt"

exec bufwinnr . "wincmd w"

return

endif

tabnext

let tb = tb +1

endwhile

" not exist, new tab

exec "tabnew " . a:filename

endif

endfunction

function! MyDiff()

let opt = '-a --binary '

if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif

if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif

let arg1 = v:fname_in

if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif

let arg2 = v:fname_new

if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif

let arg3 = v:fname_out

if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif

let eq = ''

if $VIMRUNTIME =~ ' '

if &sh =~ '\<cmd'

let cmd = '""' . $VIMRUNTIME . '\diff"'

let eq = '"'

else

let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'

endif

else

let cmd = $VIMRUNTIME . '\diff'

endif

silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq

endfunction

function! BufPos_ActivateBuffer(num)

let l:count = 1

for i in range(1, bufnr("$"))

if buflisted(i) && getbufvar(i, "&modifiable")

if l:count == a:num

exe "buffer " . i

return

endif

let l:count = l:count + 1

endif

endfor

echo "No buffer!"

endfunction

function! BufPos_Initialize()

for i in range(1, 9)

exe "map <M-" . i . "> :call BufPos_ActivateBuffer(" . i . ")<CR>"

endfor

exe "map <M-0> :call BufPos_ActivateBuffer(10)<CR>"

endfunction

" From an idea by Michael Naumann

function! VisualSearch(direction) range

let l:saved_reg = @"

execute "normal! vgvy"

let l:pattern = escape(@", '\\/.*$^~[]')

let l:pattern = substitute(l:pattern, "\n$", "", "")

if a:direction == 'b'

execute "normal ?" . l:pattern . "^M"

elseif a:direction == 'gv'

call CmdLine("vimgrep " . '/'. l:pattern . '/' . ' **/*.')

elseif a:direction == 'f'

execute "normal /" . l:pattern . "^M"

endif

let @/ = l:pattern

let @" = l:saved_reg

endfunction

function! CmdLine(str)

exe "menu Foo.Bar :" . a:str

emenu Foo.Bar

unmenu Foo

endfunction

" 译注:简单说一下其功能,设置了一个函数CurDir(),该函数调用了getcwd()

" 函数,getcwd()的作用是返回当前路径这个值.

function! CurDir()

let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")

return curdir

endfunction

func! Cwd()

let cwd = getcwd()

return "e " . cwd

endfunc

command! Bclose call <SID>BufcloseCloseIt()

function! <SID>BufcloseCloseIt()

let l:currentBufNum = bufnr("%")

let l:alternateBufNum = bufnr("#")

if buflisted(l:alternateBufNum)

buffer #

else

bnext

endif

if bufnr("%") == l:currentBufNum

new

endif

if buflisted(l:currentBufNum)

execute("bdelete! ".l:currentBufNum)

endif

endfunction
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: