但是,在.vimrc中我是都設置的.
設置如下:
inoremap ) <c-r>=ClosePair(')')<CR>
inoremap { {}<ESC>i
inoremap } <c-r>=ClosePair('}')<CR>
inoremap [ []<ESC>i
inoremap ] <c-r>=ClosePair(']')<CR>
inoremap ' ''<ESC>i
inoremap " ""<ESC>i
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
end
懇請大俠講講上面這段代碼的細節、原理和爲什麼(無法自動補全.
下面是我的.vimrc的完整內容:
" 顯示中文幫助
if version >= 603
set helplang=cn
set encoding=utf-8
endif
" 設定默認解碼
"set fenc =utf-8
"set fenc =utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936
" 設定不與以前版本兼
set nocompatible
" 設置配色方案
colorscheme murphy
" 顯示行號
set nu
" 偵測文件類型
filetype on
" 設置當文件被改動時自動載入
set autoread
" 記錄歷史的行數
set history=100
" 顯示語法高亮
syntax enable
syntax on
" 在處理未保存或只讀文件的時候,彈出確認
set confirm
" 與windows共享剪貼板
set clipboard+=unnamed
" 載入文件類型插件
filetype plugin on
" 爲特定文件類型載入相關縮進文件
filetype indent on
" 保存全局變量
set viminfo+=!
" 帶有如下符號的單詞不要被換行分割
set iskeyword+=_,$,@,%,#,-
" 設置鼠標一直可用
set mouse=a
" 高亮當前行
set cursorline
" 命令行高度
set cmdheight=1
" 啓動的時候不顯示那個援助索馬里兒童的提示
set shortmess=atI
" 不讓vim發出討厭的滴滴聲
set noerrorbells
" 在被分割的窗口間顯示空白,便於閱讀
set fillchars=vert:\ ,stl:\ ,stlnc:\
""""""""""""""""""""""""""""
" 文件設置
""""""""""""""""""""""""""""
" 不要備份文件(覆蓋文件時不備份)
set nobackup
" 不要生成swap文件,當buffer被丟棄的時候隱藏它
setlocal noswapfile
set bufhidden=hide
" 字符間插入的像素行數目
set linespace=0
" 增強模式中的命令行自動完成操作
set wildmenu
" 置粘貼模式,使得粘貼不錯位
" set paste
" 保存文件,用習慣了Ctrl+S了
" nmap <C-s> <esc>:w<CR>
" imap <C-s> <esc>:w<CR>
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-O>:update<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 搜索和匹配
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 高亮顯示匹配的括號
set showmatch
" 高亮顯示被找到的句子(phrases)
"set hlsearch
" 在搜索時,輸入的詞句的逐字符高亮(類似firefox的搜索),即輸入搜索內容時就顯示搜索結果
set incsearch
" 搜索時忽略大小寫
set ignorecase
" 不要閃爍
set novisualbell
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 文本格式和排版
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 自動格式化
set formatoptions=tcrqn
" 繼承前一行的縮進方式,特別適用於多行註釋
set autoindent
" 爲C程序提供自動縮進
set smartindent
" 使用C樣式的縮進
set cindent
" 製表符爲4
set tabstop=4
" 統一縮進爲4
" 使得按退格鍵時可以一次刪掉 4 個空格
set softtabstop=4
" 設定 << 和 >> 命令移動時的寬度爲 4
set shiftwidth=4
" 不要用空格代替製表符
set noexpandtab
" 設置每行120個字符自動換行
set textwidth=120
" 自動切換當前目錄爲當前文件所在的目錄
set autochdir
" 摺疊代碼
set foldmethod=syntax
" 文件打開時不折疊
set foldlevel=100
"""""""""""""""""""""""""""""""""""""""""""
"自動實例括號
"""""""""""""""""""""""""""""""""""""""""""
inoremap ( ()<ESC>i
inoremap ) <c-r>=ClosePair(')')<CR>
inoremap { {}<ESC>i
inoremap } <c-r>=ClosePair('}')<CR>
inoremap [ []<ESC>i
inoremap ] <c-r>=ClosePair(']')<CR>
inoremap ' ''<ESC>i
inoremap " ""<ESC>i
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""
" set mapleader
""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let mapleader = ","
" platform
function! MySys()
if has("win32")
return "windows"
else
return "linux"
endif
endfunction
" 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
" fast edit .vimrc
if MySys() == 'linux'
" fast reloading of the .vimrc
map <silent> <leader>ss :source ~/.vimrc<cr>
" fast editing of the .vimrc
map <silent> <leader>ee :call SwitchToBuf("~/.vimrc")<cr>
" when .vimrc is edited, reload it
autocmd! bufwritepost .vimrc source ~/.vimrc
elseif MySys() == 'windows'
" Set helplang
set helplang=cn
"Fast reloading of the _vimrc
map <silent> <leader>ss :source ~/_vimrc<cr>
"Fast editing of _vimrc
map <silent> <leader>ee :call SwitchToBuf("~/_vimrc")<cr>
"When _vimrc is edited, reload it
autocmd! bufwritepost _vimrc source ~/_vimrc
endif
if MySys() == 'windows'
source $VIMRUNTIME/mswin.vim
behave mswin
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""
" 插件
"""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""
"加入工程使得能夠訪問ctags
""""""""""""""""""""""""""""""
"project1"
set tags =~/prog/tags
"project2"
set tags +=~/kernelearn/tags
"後面的工程加在tags變量上
""""""""""""""""""""""""""""""
" 設置Taglist
""""""""""""""""""""""""""""""
" 按照名稱排序
let Tlist_Sort_Type = "name"
" 在右側顯示窗口
let Tlist_Use_left_Window = 1
" 如果只有一個buffer,kill窗口也kill掉buffer
let Tlist_Exist_OnlyWindow = 1
" 使taglist只顯示當前文件tag,其它文件的tag都被摺疊起來(同時顯示多個文件中的tag時)
let Tlist_File_Fold_Auto_Close = 1
" 不要顯示摺疊樹
let Tlist_Enable_Fold_Column = 1
"不同時顯示多個文件的tag,只顯示當前文件的
let Tlist_Show_One_File = 1
" 鍵盤映射
nmap tl :TlistToggle<cr>
"""""""""""""""""""""""""""""""""""""""""""""
"設置 winManager winManager管理netrw和Taglist。
"""""""""""""""""""""""""""""""""""""""""""""
let g:winManagerWindowLayout = "BufExplorer,FileExplorer|TagList"
let g:persistentBehaviour = 0
let g:winManagerWidth = 30
let g:defaultExplorer = 0
nmap <C-W><C-F> :FirstExplorerWindow<cr>
nmap <C-W><C-B> :BottomExplorerWindow<cr>
"nmap <silent> <F8> :WMToggle<cr>
nmap wm :WMToggle<cr>
"設置 netrw
""""""""""""""""""""""""""""""
"let g:netrw_winsize = 30
"nmap <silent> <leader>fe :Sexplore!<cr>
"let Tlist_File_Fold_Auto_Close=1
""""""""""""""""""""""""""""""
"設置 MiniBufExploer
""""""""""""""""""""""""""""""
" quickfix模式
autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""
"F5編譯和運行C++程序
"F6編譯和運行C程序
""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" C的編譯和運行
map <F6> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
exec "!gcc % -o %<"
exec "! ./%<"
endfunc
" C++的編譯和運行
map <F5> :call CompileRunGpp()<CR>
func! CompileRunGpp()
exec "w"
exec "!g++ % -o %<"
exec "! ./%<"
endfunc
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set diffexpr=MyDiff()
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
" 自動補全(ctrl-p)時的一些選項: 多於一項時顯示菜單,最長選擇; 顯示當前選擇的額外信息
if v:version >= 700
set completeopt=menu,longest,preview
endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""
" 狀態行相關動作
"""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 顯示狀態欄 (默認值爲 1, 無法顯示狀態欄)
set laststatus=2
" 狀態行顏色
highlight StatusLine guifg=SlateBlue guibg=Yellow
highlight StatusLineNC guifg=Gray guibg=White
function! CurDir()
let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")
return curdir
endfunction
"""""""""""""""""""""""""""""""
"狀態行顯示內容
"""""""""""""""""""""""""""""""
" %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
" 設置在狀態行顯示的信息如下:
"
set nowrapscan
"Highlight current
if has("gui_running")
set cursorline
hi cursorline guibg=#333333
hi CursorColumn guibg=#333333
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => 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 lines=40
set columns=100
"""""""""""""""""""""""""""""""""""""""""""""""""""
"菜單欄、工具欄顯示與隱藏的切換
"""""""""""""""""""""""""""""""""""""""""""""""""""
set guioptions-=T
set guioptions-=m
map <silent> <F4> :if &guioptions =~# 'T' <Bar>
\set guioptions-=T <Bar>
\set guioptions-=m <bar>
\else <Bar>
\set guioptions+=T <Bar>
\set guioptions+=m <Bar>
\endif<CR>
""""""""""""""""""""""""""""""""""""""""""""""""""""
" 日曆插件設置
""""""""""""""""""""""""""""""""""""""""""""""""""""
" let g:calendar_diary = /calendar
" map ca :Calendar<cr>
" 讓 gvim 支持 Alt+n 來切換標籤頁
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
autocmd VimEnter * call BufPos_Initialize()
" 按F11鍵讓VIM全屏
if has('gui_running') && has("win32")
map <F11> <Esc>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
endif