Vim 基本配置
閱讀原文(http://my.oschina.net/roockee/blog/97121)
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 Windows http://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代碼補全
解壓後,將裏面的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