ex:
set nocompatible
set expandtab
set tabstop=4 et
set shiftwidth=4 et
set softtabstop=4 et
set nu
set ai
au InsertLeave *.cpp write
au InsertLeave *.h write
au InsertLeave *.hpp write
:inoremap <c-j> <ESC>
:inoremap l; <ESC>$a;<ESC>
:map ls :ls<cr>
:map t1 :b 1<cr>
:map t2 :b 2<cr>
:map t3 :b 3<cr>
:map t4 :b 4<cr>
:map t5 :b 5<cr>
:map t6 :b 6<cr>
:map t7 :b 7<cr>
:map t8 :b 8<cr>
:map t9 :b 9<cr>
:inoremap ( ()<ESC>i
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
:inoremap [ []<ESC>i
:inoremap { <c-r>=DealBrace()<cr>
:inoremap < <c-r>=HasInclude()<cr>
:noremap cp :call CompileThisFile()<cr>
function CompileThisFile()
execute '!echo -e "\033[40;31;1m begin-------------------------------------------------------------------------------------------\033" -e "\033[0m" ""'
execute '!g++ -c % '
execute '!echo -e "\033[40;31;1m end-------------------------------------------------------------------------------------------\033" -e "\033[0m" ""'
endfunction
function DealBrace()
let lineNum=line(".")
let line=getline(lineNum-1)
let pos=match(line,'struct ')
if pos>=0
return "{};\<ESC>hi\<cr>\<cr>\<ESC>ki\<tab>"
endif
let pos=match(line,'class ')
if pos>=0
return "{};\<ESC>hi\<cr>\<cr>\<ESC>ki\<tab>"
endif
return "{}\<ESC>i\<cr>\<cr>\<ESC>ki\<tab>"
endfunction
function HasInclude()
let line=getline('.')
let pos=match(line,'include')
if pos<0
return '<'
else
return "<>\<ESC>i"
endif
endfunction
加载自己的vimrc
加载自己的vimrc:使用source命令 source ~/.vimrc
自动加载自己的vimrc:vim每次启动都会加载~/.vimrc文件。 所以只需要把自己的vimrc放在目录~下就可以了。
在vimrc中执行shell命令:
exe '!command'
如果不想要执行的结果显示 可以用下面的命令:
silent exec
exe '!command'
兼容VI模式:
"是否兼容VI,compatible为兼容,nocompatible为不完全兼容
"如果设置为compatible,则tab将不会变成空格
vim中的空格:
设置tab键为4个空格
set expandtab
set tabstop=4 et
然后要输入实际的tab键的方法:
当'et'打开,要插入实际的制表位,需用CTRL-V<Tab>
vim函数:
注意返回值 '<ESC>' 和 "<ESC>"的差别
ex:
:inoremap <S-ENTER> <c-r>=SkipPair()<CR>
:inoremap <S-SPACE> <ESC>la
:inoremap <C-ENTER> <ESC>A;<CR>
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { <c-r>=ClsoeBrace()<CR>
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap ;; <ESC>A;<CR>
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf
function Semicolon()
"echo getline('.')[col('.')]
if getline('.')[col('.')] == ')'
return "<ESC>A;"
elseif getline('.')[col('.')] == '}'
return "\<ESC>A;"
elseif getline('.')[col('.')] == ']'
return "\<ESC>A;"
else
return ";"
endif
endf
function SkipPair()
if getline('.')[col('.') - 1] == ')'
return "\<ESC>o"
else
normal j
let curline = line('.')
let nxtline = curline
while curline == nxtline
if getline('.')[col('.') - 1] == '}'
normal j
let nxtline = nxtline + 1
let curline = line('.')
continue
else
return "\<ESC>i"
endif
endwhile
return "\<ESC>o"
endif
endf
function ClsoeBrace()
if getline('.')[col('.') - 2] == '='
return "{}\<ESC>i"
elseif getline('.')[col('.') - 3] == '='
return "{}\<ESC>i"
elseif getline('.')[col('.') - 1] == '{'
return "{}\<ESC>i"
elseif getline('.')[col('.') - 2] == '{'
return "{}\<ESC>i"
elseif getline('.')[col('.') - 2] == ','
return "{}\<ESC>i"
elseif getline('.')[col('.') - 3] == ','
return "{}\<ESC>i"
else
return "{\<ENTER>}\<ESC>O"
endif
endf
多文档编辑:
1. vim 文档1名 文档2名 ...
2. vim ,进入vim后使用:e 文档名 打开文档
3.多文档编辑的命令如下
:n 编辑下一个文档。:2n 编辑下两个文档。
:N 编辑上一个文档。注意,该方法只能用于同时打开多个文档。
:e 文档名 这是在进入vim后,不离开 vim 的情形下打开其他文档。
:e# 或 Ctrl+ˆ 编辑上一个文档,用于两个文档相互交换编辑时使用。?# 代表的是编辑前一次编辑的文档
:files 或 :buffers 或 :ls 可以列出目前 缓冲区 中的所有文档。加号 + 表示 缓冲区已经被修改过了。#代表上一次编辑的文档,%是目前正在编辑中的文档
:b 文档名或编号 移至该文档。
:f 或 Ctrl+g 显示当前正在编辑的文档名称。
:f 檔名 改变编辑中的文档名。(file)
自动保存:
au InsertLeave *.cpp write
意思是:针对所有.cpp文件,在退出编辑模式(InsertLeave)时,自动执行write命令。这样就能在每次按Esc键时自动保存了。
vim中执行命令:
execute