[整理]vim常用命令速查卡片
发表于 : 2006-05-01 21:11
最近在学vim,由于命令较多查找不方便,就自己整理了这个
linuxsir上也有类似的,这个要比那个多些
尽量抽取些比较常见的,有些命令只有在经常编辑文本时才能体会它的方便
这些都是最基本的,本想把参见命令组合也加上,不过太多太灵活了~
很多时候其实你想做到什么效果,试一下真的能做到,所以多发挥想象试哈
程序部分太多了,有很多方便编写和查看程序的方法,ctags什么的等,这些大家就自己研究了
送给大家一句《建筑永恒之道》中的话
“学习所有方法,然后忘记它”
附上我的.vimrc
" 安装: sudo apt-get install vim-gtk
" 用法: 将本文件(.vimrc)拷贝到$HOME/
" 使用 murphy 调色板
" colo murphy
"中文help
if version >= 603
set helplang=cn
endif
" 设置自动缩进
set autoindent
" 设定文件浏览器目录为当前目录
set bsdir=buffer
" 设置编码
set enc=utf-8
" 设置文件编码
set fenc=utf-8
" 设置文件编码检测类型及支持格式
set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
" 指定菜单语言
set langmenu=zh_CN.UTF-8
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
" 设置语法高亮度
" set syn=cpp
" 语法高亮
syntax enable
"显示行号
set nu!
set ruler
" 查找结果高亮度显示,立即匹配
set hlsearch
set showmatch
set incsearch
"设置退格键为普通模式
set backspace=2
"设定自动换行
set wrap
"HTML<>匹配
"set mps+=<:>
" C/C++注释
" set comments=://
" 修正自动C式样注释功能 <2005/07/16>
set comments=s1:/*,mb:*,ex0:/
" 增强检索功能
set tags=./tags,./../tags,./**/tags
" 保存文件格式
set fileformats=unix,dos
" 键盘操作
map <Up> gk
map <Down> gj
" 命令行高度
set cmdheight=1
"cscope"
if has("cscope")
set csprg=/usr/bin/cscope
set csto=0
set cst
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
set cscopetag
set cscopequickfix=s-,g-,c-,d-,t-,e-,f-,i-
endif
if has('gui_running')
let do_syntax_sel_menu=1
endif
set nobackup
set fileencodings=ucs-bom,utf-8,gbk
"显示文件编码信息
set statusline=%<%f\ %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P
if has('mouse')
set mouse=a
endif
" Key mappings to ease browsing long lines
noremap <C-J> gj
noremap <C-K> gk
noremap <Down> gj
noremap <Up> gk
inoremap <Down> <C-O>gj
inoremap <Up> <C-O>gk
if has('autocmd')
function! SetFileEncodings(encodings)
let b:my_fileencodings_bak=&fileencodings
let &fileencodings=a:encodings
endfunction
function! RestoreFileEncodings()
let &fileencodings=b:my_fileencodings_bak
unlet b:my_fileencodings_bak
endfunction
function! CheckFileEncoding()
if &modified && &fileencoding != ''
exec 'e! ++enc=' . &fileencoding
endif
endfunction
function! ConvertHtmlEncoding(encoding)
if a:encoding ==? 'gb2312'
return 'gbk' " GB2312 imprecisely means GBK in HTML
elseif a:encoding ==? 'iso-8859-1'
return 'latin1' " The canonical encoding name in Vim
elseif a:encoding ==? 'utf8'
return 'utf-8' " Other encoding aliases should follow here
else
return a:encoding
endif
endfunction
function! DetectHtmlEncoding()
if &filetype != 'html'
return
endif
normal m`
normal gg
if search('\c<meta http-equiv=\("\?\)Content-Type\1 content="text/html; charset=[-A-Za-z0-9_]\+">') != 0
let reg_bak=@"
normal y$
let charset=matchstr(@", 'text/html; charset=\zs[-A-Za-z0-9_]\+')
let charset=ConvertHtmlEncoding(charset)
normal ``
let @"=reg_bak
if &fileencodings == ''
let auto_encodings=',' . &encoding . ','
else
let auto_encodings=',' . &fileencodings . ','
endif
if charset !=? &fileencoding &&
\(auto_encodings =~ ',' . &fileencoding . ',' || &fileencoding == '')
silent! exec 'e ++enc=' . charset
endif
else
normal ``
endif
endfunction
function! GnuIndent()
setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
setlocal shiftwidth=2
setlocal tabstop=8
endfunction
function! RemoveTrailingSpace()
if $VIM_HATE_SPACE_ERRORS != '0' &&
\(&filetype == 'c' || &filetype == 'cpp' || &filetype == 'vim')
normal m`
silent! :%s/\s\+$//e
normal ``
endif
endfunction
" Highlight space errors in C/C++ source files (Vim tip #935)
if $VIM_HATE_SPACE_ERRORS != '0'
let c_space_errors=1
endif
" Use Canadian spelling convention in engspchk (Vim script #195)
let spchkdialect='can'
" Show syntax highlighting attributes of character under cursor (Vim
" script #383)
map <Leader>a :call SyntaxAttr()<CR>
" Automatically find scripts in the autoload directory
au FuncUndefined * exec 'runtime autoload/' . expand('<afile>') . '.vim'
" File type related autosetting
au FileType c,cpp setlocal cinoptions=:0,g0,(0,w1 shiftwidth=4 tabstop=4
au FileType diff setlocal shiftwidth=4 tabstop=4
au FileType html setlocal autoindent indentexpr=
au FileType changelog setlocal textwidth=76
" Text file encoding autodetection
au BufReadPre *.gb call SetFileEncodings('gbk')
au BufReadPre *.big5 call SetFileEncodings('big5')
au BufReadPre *.nfo call SetFileEncodings('cp437')
au BufReadPost *.gb,*.big5,*.nfo call RestoreFileEncodings()
au BufWinEnter *.txt call CheckFileEncoding()
" Detect charset encoding in an HTML file
au BufReadPost *.htm* nested call DetectHtmlEncoding()
" Recognize standard C++ headers
au BufEnter /usr/include/c++/* setf cpp
au BufEnter /usr/include/g++-3/* setf cpp
" Setting for files following the GNU coding standard
au BufEnter /usr/* call GnuIndent()
" Remove trailing spaces for C/C++ and Vim files
au BufWritePre * call RemoveTrailingSpace()
endif
linuxsir上也有类似的,这个要比那个多些
尽量抽取些比较常见的,有些命令只有在经常编辑文本时才能体会它的方便
这些都是最基本的,本想把参见命令组合也加上,不过太多太灵活了~
很多时候其实你想做到什么效果,试一下真的能做到,所以多发挥想象试哈
程序部分太多了,有很多方便编写和查看程序的方法,ctags什么的等,这些大家就自己研究了
送给大家一句《建筑永恒之道》中的话
“学习所有方法,然后忘记它”
附上我的.vimrc
" 安装: sudo apt-get install vim-gtk
" 用法: 将本文件(.vimrc)拷贝到$HOME/
" 使用 murphy 调色板
" colo murphy
"中文help
if version >= 603
set helplang=cn
endif
" 设置自动缩进
set autoindent
" 设定文件浏览器目录为当前目录
set bsdir=buffer
" 设置编码
set enc=utf-8
" 设置文件编码
set fenc=utf-8
" 设置文件编码检测类型及支持格式
set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
" 指定菜单语言
set langmenu=zh_CN.UTF-8
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
" 设置语法高亮度
" set syn=cpp
" 语法高亮
syntax enable
"显示行号
set nu!
set ruler
" 查找结果高亮度显示,立即匹配
set hlsearch
set showmatch
set incsearch
"设置退格键为普通模式
set backspace=2
"设定自动换行
set wrap
"HTML<>匹配
"set mps+=<:>
" C/C++注释
" set comments=://
" 修正自动C式样注释功能 <2005/07/16>
set comments=s1:/*,mb:*,ex0:/
" 增强检索功能
set tags=./tags,./../tags,./**/tags
" 保存文件格式
set fileformats=unix,dos
" 键盘操作
map <Up> gk
map <Down> gj
" 命令行高度
set cmdheight=1
"cscope"
if has("cscope")
set csprg=/usr/bin/cscope
set csto=0
set cst
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
set cscopetag
set cscopequickfix=s-,g-,c-,d-,t-,e-,f-,i-
endif
if has('gui_running')
let do_syntax_sel_menu=1
endif
set nobackup
set fileencodings=ucs-bom,utf-8,gbk
"显示文件编码信息
set statusline=%<%f\ %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P
if has('mouse')
set mouse=a
endif
" Key mappings to ease browsing long lines
noremap <C-J> gj
noremap <C-K> gk
noremap <Down> gj
noremap <Up> gk
inoremap <Down> <C-O>gj
inoremap <Up> <C-O>gk
if has('autocmd')
function! SetFileEncodings(encodings)
let b:my_fileencodings_bak=&fileencodings
let &fileencodings=a:encodings
endfunction
function! RestoreFileEncodings()
let &fileencodings=b:my_fileencodings_bak
unlet b:my_fileencodings_bak
endfunction
function! CheckFileEncoding()
if &modified && &fileencoding != ''
exec 'e! ++enc=' . &fileencoding
endif
endfunction
function! ConvertHtmlEncoding(encoding)
if a:encoding ==? 'gb2312'
return 'gbk' " GB2312 imprecisely means GBK in HTML
elseif a:encoding ==? 'iso-8859-1'
return 'latin1' " The canonical encoding name in Vim
elseif a:encoding ==? 'utf8'
return 'utf-8' " Other encoding aliases should follow here
else
return a:encoding
endif
endfunction
function! DetectHtmlEncoding()
if &filetype != 'html'
return
endif
normal m`
normal gg
if search('\c<meta http-equiv=\("\?\)Content-Type\1 content="text/html; charset=[-A-Za-z0-9_]\+">') != 0
let reg_bak=@"
normal y$
let charset=matchstr(@", 'text/html; charset=\zs[-A-Za-z0-9_]\+')
let charset=ConvertHtmlEncoding(charset)
normal ``
let @"=reg_bak
if &fileencodings == ''
let auto_encodings=',' . &encoding . ','
else
let auto_encodings=',' . &fileencodings . ','
endif
if charset !=? &fileencoding &&
\(auto_encodings =~ ',' . &fileencoding . ',' || &fileencoding == '')
silent! exec 'e ++enc=' . charset
endif
else
normal ``
endif
endfunction
function! GnuIndent()
setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
setlocal shiftwidth=2
setlocal tabstop=8
endfunction
function! RemoveTrailingSpace()
if $VIM_HATE_SPACE_ERRORS != '0' &&
\(&filetype == 'c' || &filetype == 'cpp' || &filetype == 'vim')
normal m`
silent! :%s/\s\+$//e
normal ``
endif
endfunction
" Highlight space errors in C/C++ source files (Vim tip #935)
if $VIM_HATE_SPACE_ERRORS != '0'
let c_space_errors=1
endif
" Use Canadian spelling convention in engspchk (Vim script #195)
let spchkdialect='can'
" Show syntax highlighting attributes of character under cursor (Vim
" script #383)
map <Leader>a :call SyntaxAttr()<CR>
" Automatically find scripts in the autoload directory
au FuncUndefined * exec 'runtime autoload/' . expand('<afile>') . '.vim'
" File type related autosetting
au FileType c,cpp setlocal cinoptions=:0,g0,(0,w1 shiftwidth=4 tabstop=4
au FileType diff setlocal shiftwidth=4 tabstop=4
au FileType html setlocal autoindent indentexpr=
au FileType changelog setlocal textwidth=76
" Text file encoding autodetection
au BufReadPre *.gb call SetFileEncodings('gbk')
au BufReadPre *.big5 call SetFileEncodings('big5')
au BufReadPre *.nfo call SetFileEncodings('cp437')
au BufReadPost *.gb,*.big5,*.nfo call RestoreFileEncodings()
au BufWinEnter *.txt call CheckFileEncoding()
" Detect charset encoding in an HTML file
au BufReadPost *.htm* nested call DetectHtmlEncoding()
" Recognize standard C++ headers
au BufEnter /usr/include/c++/* setf cpp
au BufEnter /usr/include/g++-3/* setf cpp
" Setting for files following the GNU coding standard
au BufEnter /usr/* call GnuIndent()
" Remove trailing spaces for C/C++ and Vim files
au BufWritePre * call RemoveTrailingSpace()
endif