每個程序員都有自己心愛的代碼編輯和查看工具,相信不少Linux

很多編輯器都支持在源代碼中自動添加作者信息的功能,SourceInsight就有這項(xiàng)功能。Vim默認(rèn)是沒有這個功能,但是只要我們稍作配置,就可以實(shí)現(xiàn)這個功能了。
在Linux下我們需要修改Vim的配置文件vimrc,位于:
/etc/vim/vimrc
在window下Vim的配置文件位于安裝目錄下的_vimrc。
然后在配置文件中添加如下信息,就可以實(shí)現(xiàn)在源代碼中自動添加作者信息了:
“將鍵盤上的F4功能鍵映射為添加作者信息的快捷鍵
map ms:call TItleDet()'s
functiON AddTitle()
call append(0,"/*******************************************************************************")
"call append(1,"#")
call append(1," * Author : YangShuo")
"call append(3,"#")
call append(2," * Email : ys@farsight.com.cn")
"call append(5,"#")
call append(3," * LaST modified : ".strftime("%Y-%m-%d %H:%M"))
"call append(7,"#")
call append(4," * Filename : ".expand("%:t"))
"call append(9,"#")
call append(5," * Description : ")
"call append(11,"#")
call append(6," * *****************************************************************************/")
echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endf
“更新最近修改時間和文件名
normal m'
execute '/# *Last modified:/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
normal "
normal mk
execute '/# *Filename:/s@:.*$@\=":\t\t".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copy right."| echohl None
endfunction
“判斷前10行代碼里面,是否有Last modified這個單詞,
“如果沒有的話,代表沒有添加過作者信息,需要新添加;
“如果有的話,那么只需要更新即可
function TitleDet()
let n=1
while n < 10
let line = getline(n)
if line =~'^\#\s*\S*Last\smodified:\S*.*$'
call UpdateTitle()
return
endif
let n = n + 1
endwhile
call AddTitle()
endfunction
這段配置在Linux和window下均可正常運(yùn)行。這樣在一個C/C++/JAVA/C#的源代碼里面,只需要按一下F4,就可以自動添加作者信息了。大家只需要修改上面這段配置關(guān)于作者信息的代碼,就可以添加自己的信息了。
這是在我電腦上運(yùn)行的結(jié)果,看一看,是不是很帥:
趕快動手吧,把這段配置添加到你的vimrc里,這樣你就可以輕松地在源文件開始添加你的相關(guān)信息了,讓我們的代碼更加規(guī)范。
史上最強(qiáng)的 Vim 配置文件?-純文本版
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" _
" __ | \
" / | /
" \__ | \
" by Amix - http://amix.dk/
"
" Maintainer: Amir Salihefendic <amix3k at gmail.com>
" Version: 2.7
" Last Change: 12/10/06 00:09:21
"
" Sections:
" ----------------------
" *> General
" *> Colors and Fonts
" *> Fileformats
" *> VIM userinterface
" ------ *> Statusline
" *> Visual
" *> Moving around and tabs
" *> General Autocommands
" *> Parenthesis/bracket expanding
" *> General Abbrevs
" *> Editing mappings etc.
" *> Command-line config
" *> Buffer realted
" *> Files and backups
" *> Folding
" *> Text options
" ------ *> Indent
" *> Spell checking
" *> Plugin configuration
" ------ *> Yank ring
" ------ *> File explorer
" ------ *> Minibuffer
" ------ *> Tag list (ctags) - not used
" ------ *> LaTeX Suite things
" *> Filetype generic
" ------ *> Todo
" ------ *> VIM
" ------ *> HTML related
" ------ *> Ruby & PHP section
" ------ *> Python section
" ------ *> Cheetah section
" ------ *> Vim section
" ------ *> Java section
" ------ *> JavaScript section
" ------ *> C mappings
" ------ *> SML
" ------ *> Scheme bindings
" *> Snippets
" ------ *> Python
" ------ *> javaScript
" *> Cope
" *> MISC
"
" Tip:
" If you find anything that you can't understand than do this:
" help keyword OR helpgrep keywords
" Example:
" Go into command-line mode and type helpgrep nocompatible, ie.
" :helpgrep nocompatible
" then press <leader>c to see the results, or :botright cw
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Get out of VI's compatible mode..
set nocompatible
"Sets how many lines of history VIM har to remember
set history=400
"Enable filetype plugin
filetype plugin on
filetype indent on
"Set to auto read when a file is changed from the outside
set autoread
"Have the mouse enabled all the time:
set mouse=a
"Set mapleader
let mapleader = ","
let g:mapleader = ","
"Fast saving
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>
"Fast reloading of the .vimrc
map <leader>s :source ~/vim_local/vimrc<cr>
"Fast editing of .vimrc
map <leader>e :e! ~/vim_local/vimrc<cr>
"When .vimrc is edited, reload it
autocmd! bufwritepost vimrc source ~/vim_local/vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Colors and Fonts
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Enable syntax hl
syntax enable
"Set font to Monaco 10pt
if MySys() == "mac"
set gfn=Bitstream\ Vera\ Sans\ Mono:h14
set nomacatsui
set termencoding=macroman
elseif MySys() == "linux"
set gfn=Monospace\ 11
endif
if has("gui_running")
set guioptions-=T
let psc_style='cool'
colorscheme ps_color
else
set background=dark
colorscheme zellner
endif
"Some nice mapping to switch syntax (useful if one mixes different languages in one file)
map <leader>1 :set syntax=cheetah<cr>
map <leader>2 :set syntax=xhtml<cr>
map <leader>3 :set syntax=python<cr>
map <leader>4 :set ft=javascript<cr>
map <leader>$ :syntax sync fromstart<cr>
autocmd BufEnter * :syntax sync fromstart
"Highlight current
if has("gui_running")
set cursorline
hi cursorline guibg=#333333
hi CursorColumn guibg=#333333
endif
"Omni menu colors
hi Pmenu guibg=#333333
hi PmenuSel guibg=#555555 guifg=#ffffff
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Fileformats
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Favorite filetypes
set ffs=unix,dos,mac
nmap <leader>fd :se ff=dos<cr>
nmap <leader>fu :se ff=unix<cr>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => VIM userinterface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Set 7 lines to the curors - when moving vertical..
set so=7
"Turn on WiLd menu
set wildmenu
"Always show current position
set ruler
"The commandbar is 2 high
set cmdheight=2
"Show line number
set nu
"Do not redraw, when running macros.. lazyredraw
set lz
"Change buffer - without saving
set hid
"Set backspace
set backspace=eol,start,indent
"Bbackspace and cursor keys wrap to
set whichwrap+=<,>,h,l
"Ignore case when searching
set ignorecase
set incsearch
"Set magic on
set magic
"No sound on errors.
set noerrorbells
set novisualbell
set t_vb=
"show matching bracets
set showmatch
"How many tenths of a second to blink
set mat=2
"Highlight search things
set hlsearch
""""""""""""""""""""""""""""""
" => Statusline
""""""""""""""""""""""""""""""
"Always hide the statusline
set laststatus=2
function! CurDir()
let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")
return curdir
endfunction
"Format the statusline
set statusline=\ %F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L:%c
""""""""""""""""""""""""""""""
" => Visual
""""""""""""""""""""""""""""""
" 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"
else
execute "normal /" . l:pattern . "^M"
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
"Basically you press * or # to search for the current selection !! Really useful
vnoremap <silent> * :call VisualSearch('f')<CR>
vnoremap <silent> # :call VisualSearch('b')<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around and tabs
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Map space to / and c-space to ?
map <space> /
map <c-space> ?
"Smart way to move btw. windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
"Actually, the tab does not switch buffers, but my arrows
"Bclose function ca be found in "Buffer related" section
map <leader>bd :Bclose<cr>
map <down> <leader>bd
"Use the arrows to something usefull
map <right> :bn<cr>
map <left> :bp<cr>
"Tab configuration
map <leader>tn :tabnew %<cr>
map <leader>te :tabedit
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
try
set switchbuf=usetab
set stal=2
catch
endtry
"Moving fast to front, back and 2 sides ;)
imap <m-$> <esc>$a
imap <m-0> <esc>0i
imap <D-$> <esc>$a
imap <D-0> <esc>0i
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General Autocommands
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Switch to current dir
map <leader>cd :cd %:p:h<cr>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parenthesis/bracket expanding
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vnoremap $1 <esc>`>a)<esc>`<i(<esc>
")
vnoremap $2 <esc>`>a]<esc>`<i[<esc>
vnoremap $3 <esc>`>a}<esc>`<i{<esc>
vnoremap $$ <esc>`>a"<esc>`<i"<esc>
vnoremap $q <esc>`>a'<esc>`<i'<esc>
vnoremap $w <esc>`>a"<esc>`<i"<esc>
"Map auto complete of (, ", ', [
inoremap $1 ()<esc>:let leavechar=")"<cr>i
inoremap $2 []<esc>:let leavechar="]"<cr>i
inoremap $4 {<esc>o}<esc>:let leavechar="}"<cr>O
inoremap $3 {}<esc>:let leavechar="}"<cr>i
inoremap $q ''<esc>:let leavechar="'"<cr>i
inoremap $w ""<esc>:let leavechar='"'<cr>i
au BufNewFile,BufRead *.\(vim\)\@! inoremap " ""<esc>:let leavechar='"'<cr>i
au BufNewFile,BufRead *.\(txt\)\@! inoremap ' ''<esc>:let leavechar="'"<cr>i
imap <m-l> <esc>:exec "normal f" . leavechar<cr>a
imap <d-l> <esc>:exec "normal f" . leavechar<cr>a
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General Abbrevs
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"My information
iab xdate <c-r>=strftime("%d/%m/%y %H:%M:%S")<cr>
iab xname Amir Salihefendic
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Editing mappings etc.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Remap VIM 0
map 0 ^
"Move a line of text using control
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z
if MySys() == "mac"
nmap <D-j> <M-j>
nmap <D-k> <M-k>
vmap <D-j> <M-j>
vmap <D-k> <M-k>
endif
func! DeleteTrailingWS()
exe "normal mz"
%s/\s\+$//ge
exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()
set completeopt=menu
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Command-line config
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
func! Cwd()
let cwd = getcwd()
return "e " . cwd
endfunc
func! DeleteTillSlash()
let g:cmd = getcmdline()
if MySys() == "linux" || MySys() == "mac"
let g:cmd_edited = substitute(g:cmd, "\\(.*\[/\]\\).*", "\\1", "")
else
let g:cmd_edited = substitute(g:cmd, "\\(.*\[\\\\]\\).*", "\\1", "")
endif
if g:cmd == g:cmd_edited
if MySys() == "linux" || MySys() == "mac"
let g:cmd_edited = substitute(g:cmd, "\\(.*\[/\]\\).*/", "\\1", "")
else
let g:cmd_edited = substitute(g:cmd, "\\(.*\[\\\\\]\\).*\[\\\\\]", "\\1", "")
endif
endif
return g:cmd_edited
endfunc
func! CurrentFileDir(cmd)
return a:cmd . " " . expand("%:p:h") . "/"
endfunc
"Smart mappings on the command line
cno $h e ~/
cno $d e ~/Desktop/
cno $j e ./
cno $q <C-\>eDeleteTillSlash()<cr>
cno $c e <C-\>eCurrentFileDir("e")<cr>
cno $tc <C-\>eCurrentFileDir("tabnew")<cr>
cno $th tabnew ~/
cno $td tabnew ~/Desktop/
"Bash like
cnoremap <C-A> <Home>
cnoremap <C-E> <End>
cnoremap <C-K> <C-U>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Buffer realted
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Fast open a buffer by search for a name
map <c-q> :sb
"Open a dummy buffer for paste
map <leader>q :e ~/buffer<cr>
"Restore cursor to file position in previous editing session
set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
" Buffer - reverse everything ... :)
map <F9> ggVGg?
" Don't close window, when deleting a buffer
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
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Files and backups
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Turn backup off
set nobackup
set nowb
set noswapfile
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Folding
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Enable folding, I find it very useful
set nofen
set fdl=0
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text options
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=2
map <leader>t2 :set shiftwidth=2<cr>
map <leader>t4 :set shiftwidth=4<cr>
au FileType html,python,vim,javascript setl shiftwidth=2
au FileType html,python,vim,javascript setl tabstop=2
au FileType java setl shiftwidth=4
au FileType java setl tabstop=4
set smarttab
set lbr
set tw=500
""""""""""""""""""""""""""""""
" => Indent
""""""""""""""""""""""""""""""
"Auto indent
set ai
"Smart indet
set si
"C-style indeting
set cindent
"Wrap lines
set wrap
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Spell checking
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <leader>sn ]s
map <leader>sp [s
map <leader>sa zg
map <leader>s? z=
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Plugin configuration
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""
" => Vim Grep
""""""""""""""""""""""""""""""
let Grep_Skip_Dirs = 'RCS CVS SCCS .svn'
let Grep_Cygwin_Find = 1
""""""""""""""""""""""""""""""
" => Yank Ring
""""""""""""""""""""""""""""""
map <leader>y :YRShow<cr>
""""""""""""""""""""""""""""""
" => File explorer
""""""""""""""""""""""""""""""
"Split vertically
let g:explVertical=1
"Window size
let g:explWinSize=35
let g:explSplitLeft=1
let g:explSplitBelow=1
"Hide some files
let g:explHideFiles='^\.,.*\.class$,.*\.swp$,.*\.pyc$,.*\.swo$,\.DS_Store$'
"Hide the help thing..
let g:explDetailedHelp=0
""""""""""""""""""""""""""""""
" => Minibuffer
""""""""""""""""""""""""""""""
let g:miniBufExplModSelTarget = 1
let g:miniBufExplorerMoreThanOne = 2
let g:miniBufExplModSelTarget = 0
let g:miniBufExplUseSingleClick = 1
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplVSplit = 25
let g:miniBufExplSplitBelow=1
let g:bufExplorerSortBy = "name"
autocmd BufRead,BufNew :call UMiniBufExplorer
""""""""""""""""""""""""""""""
" => Tag list (ctags) - not used
""""""""""""""""""""""""""""""
"let Tlist_Ctags_Cmd = "/sw/bin/ctags-exuberant"
"let Tlist_Sort_Type = "name"
"let Tlist_Show_Menu = 1
"map <leader>t :Tlist<cr>
""""""""""""""""""""""""""""""
" => LaTeX Suite things
""""""""""""""""""""""""""""""
set grepprg=grep\ -nH\ $*
let g:Tex_DefaultTargetFormat="pdf"
let g:Tex_ViewRule_pdf='xpdf'
"Bindings
autocmd FileType tex map <silent><leader><space> :w!<cr> :silent! call Tex_RunLaTeX()<cr>
"Auto complete some things ;)
autocmd FileType tex inoremap $i \indent
autocmd FileType tex inoremap $* \cdot
autocmd FileType tex inoremap $i \item
autocmd FileType tex inoremap $m \[<cr>\]<esc>O
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Filetype generic
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Todo
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
au BufNewFile,BufRead *.todo so ~/vim_local/syntax/amido.vim
""""""""""""""""""""""""""""""
" => VIM
""""""""""""""""""""""""""""""
autocmd FileType vim map <buffer> <leader><space> :w!<cr>:source %<cr>
""""""""""""""""""""""""""""""
" => HTML related
""""""""""""""""""""""""""""""
" HTML entities - used by xml edit plugin
let xml_use_xhtml = 1
"let xml_no_auto_nesting = 1
"To HTML
let html_use_css = 1
let html_number_lines = 0
let use_xhtml = 1
""""""""""""""""""""""""""""""
" => Ruby & PHP section
""""""""""""""""""""""""""""""
autocmd FileType ruby map <buffer> <leader><space> :w!<cr>:!ruby %<cr>
autocmd FileType php compiler php
autocmd FileType php map <buffer> <leader><space> <leader>cd:w<cr>:make %<cr>
""""""""""""""""""""""""""""""
" => Python section
""""""""""""""""""""""""""""""
"Run the current buffer in python - ie. on leader+space
au FileType python so ~/vim_local/syntax/python.vim
autocmd FileType python map <buffer> <leader><space> :w!<cr>:!python %<cr>
autocmd FileType python so ~/vim_local/plugin/python_fold.vim
"Set some bindings up for 'compile' of python
autocmd FileType python set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
autocmd FileType python set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
"Python iMaps
au FileType python set cindent
au FileType python inoremap <buffer> $r return
au FileType python inoremap <buffer> $s self
au FileType python inoremap <buffer> $c ##<cr>#<space><cr>#<esc>kla
au FileType python inoremap <buffer> $i import
au FileType python inoremap <buffer> $p print
au FileType python inoremap <buffer> $d """<cr>"""<esc>O
"Run in the Python interpreter
function! Python_Eval_VSplit() range
let src = tempname()
let dst = tempname()
execute ": " . a:firstline . "," . a:lastline . "w " . src
execute ":!python " . src . " > " . dst
execute ":pedit! " . dst
endfunction
au FileType python vmap <F7> :call Python_Eval_VSplit()<cr>
""""""""""""""""""""""""""""""
" => Cheetah section
"""""""""""""""""""""""""""""""
autocmd FileType cheetah set ft=xml
autocmd FileType cheetah set syntax=cheetah
"""""""""""""""""""""""""""""""
" => Vim section
"""""""""""""""""""""""""""""""
autocmd FileType vim set nofen
"""""""""""""""""""""""""""""""
" => Java section
"""""""""""""""""""""""""""""""
au FileType java inoremap <buffer> <C-t> System.out.println();<esc>hi
"Java comments
autocmd FileType java source ~/vim_local/macros/jcommenter.vim
autocmd FileType java let b:jcommenter_class_author='Amir Salihefendic (amix@amix.dk)'
autocmd FileType java let b:jcommenter_file_author='Amir Salihefendic (amix@amix.dk)'
autocmd FileType java map <buffer> <F2> :call JCommentWriter()<cr>
"Abbr'z
autocmd FileType java inoremap <buffer> $pr private
autocmd FileType java inoremap <buffer> $r return
autocmd FileType java inoremap <buffer> $pu public
autocmd FileType java inoremap <buffer> $i import
autocmd FileType java inoremap <buffer> $b boolean
autocmd FileType java inoremap <buffer> $v void
autocmd FileType java inoremap <buffer> $s String
"Folding
function! JavaFold()
setl foldmethod=syntax
setl foldlevelstart=1
syn region foldBraces start=/{/ end=/}/ transparent fold keepend extend
syn match foldImports /\(\n\?import.\+;\n\)\+/ transparent fold
function! FoldText()
return substitute(getline(v:foldstart), '{.*', '{...}', '')
endfunction
setl foldtext=FoldText()
endfunction
au FileType java call JavaFold()
au FileType java setl fen
au BufEnter *.sablecc,*.scc set ft=sablecc
""""""""""""""""""""""""""""""
" => JavaScript section
"""""""""""""""""""""""""""""""
au FileType javascript so ~/vim_local/syntax/javascript.vim
function! JavaScriptFold()
setl foldmethod=syntax
setl foldlevelstart=1
syn region foldBraces start=/{/ end=/}/ transparent fold keepend extend
function! FoldText()
return substitute(getline(v:foldstart), '{.*', '{...}', '')
endfunction
setl foldtext=FoldText()
endfunction
au FileType javascript call JavaScriptFold()
au FileType javascript setl fen
au FileType javascript imap <c-t> console.log();<esc>hi
au FileType javascript imap <c-a> alert();<esc>hi
au FileType javascript setl nocindent
au FileType javascript inoremap <buffer> $r return
au FileType javascript inoremap <buffer> $d //<cr>//<cr>//<esc>ka<space>
au FileType javascript inoremap <buffer> $c /**<cr><space><cr>**/<esc>ka
""""""""""""""""""""""""""""""
" => HTML
"""""""""""""""""""""""""""""""
au FileType html,cheetah set ft=xml
au FileType html,cheetah set syntax=html
""""""""""""""""""""""""""""""
" => C mappings
"""""""""""""""""""""""""""""""
autocmd FileType c map <buffer> <leader><space> :w<cr>:!gcc %<cr>
"""""""""""""""""""""""""""""""
" => SML
"""""""""""""""""""""""""""""""
autocmd FileType sml map <silent> <buffer> <leader><space> <leader>cd:w<cr>:!sml %<cr>
""""""""""""""""""""""""""""""
" => Scheme bidings
""""""""""""""""""""""""""""""
autocmd BufNewFile,BufRead *.scm map <buffer> <leader><space> <leader>cd:w<cr>:!petite %<cr>
autocmd BufNewFile,BufRead *.scm inoremap <buffer> <C-t> (pretty-print )<esc>i
autocmd BufNewFile,BufRead *.scm vnoremap <C-t> <esc>`>a)<esc>`<i(pretty-print <esc>
""""""""""""""""""""""""""""""
" => SVN section
"""""""""""""""""""""""""""""""
map <F8> :new<CR>:read !svn diff<CR>:set syntax=diff buftype=nofile<CR>gg
""""""""""""""""""""""""""""""
" => Snippets
"""""""""""""""""""""""""""""""
"You can use <c-j> to goto the next <++> - it is pretty smart ;)
"""""""""""""""""""""""""""""""
" => Python
"""""""""""""""""""""""""""""""
autocmd FileType python inorea <buffer> cfun <c-r>=IMAP_PutTextWithMovement("def <++>(<++>):\n<++>\nreturn <++>")<cr>
autocmd FileType python inorea <buffer> cclass <c-r>=IMAP_PutTextWithMovement("class <++>:\n<++>")<cr>
autocmd FileType python inorea <buffer> cfor <c-r>=IMAP_PutTextWithMovement("for <++> in <++>:\n<++>")<cr>
autocmd FileType python inorea <buffer> cif <c-r>=IMAP_PutTextWithMovement("if <++>:\n<++>")<cr>
autocmd FileType python inorea <buffer> cifelse <c-r>=IMAP_PutTextWithMovement("if <++>:\n<++>\nelse:\n<++>")<cr>
"""""""""""""""""""""""""""""""
" => JavaScript
"""""""""""""""""""""""""""""""
autocmd FileType cheetah,html,javascript inorea <buffer> cfun <c-r>=IMAP_PutTextWithMovement("function <++>(<++>) {\n<++>;\nreturn <++>;\n}")<cr>
autocmd filetype cheetah,html,javascript inorea <buffer> cfor <c-r>=IMAP_PutTextWithMovement("for(<++>; <++>; <++>) {\n<++>;\n}")<cr>
autocmd FileType cheetah,html,javascript inorea <buffer> cif <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}")<cr>
autocmd FileType cheetah,html,javascript inorea <buffer> cifelse <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}\nelse {\n<++>;\n}")<cr>
"""""""""""""""""""""""""""""""
" => HTML
"""""""""""""""""""""""""""""""
autocmd FileType cheetah,html inorea <buffer> cahref <c-r>=IMAP_PutTextWithMovement('<a href="<++>"><++></a>')<cr>
autocmd FileType cheetah,html inorea <buffer> cbold <c-r>=IMAP_PutTextWithMovement('<b><++></b>')<cr>
autocmd FileType cheetah,html inorea <buffer> cimg <c-r>=IMAP_PutTextWithMovement('<img src="<++>" alt="<++>" />')<cr>
autocmd FileType cheetah,html inorea <buffer> cpara <c-r>=IMAP_PutTextWithMovement('<p><++></p>')<cr>
autocmd FileType cheetah,html inorea <buffer> ctag <c-r>=IMAP_PutTextWithMovement('<<++>><++></<++>>')<cr>
autocmd FileType cheetah,html inorea <buffer> ctag1 <c-r>=IMAP_PutTextWithMovement("<<++>><cr><++><cr></<++>>")<cr>
"""""""""""""""""""""""""""""""
" => Java
"""""""""""""""""""""""""""""""
autocmd FileType java inorea <buffer> cfun <c-r>=IMAP_PutTextWithMovement("public<++> <++>(<++>) {\n<++>;\nreturn <++>;\n}")<cr>
autocmd FileType java inorea <buffer> cfunpr <c-r>=IMAP_PutTextWithMovement("private<++> <++>(<++>) {\n<++>;\nreturn <++>;\n}")<cr>
autocmd FileType java inorea <buffer> cfor <c-r>=IMAP_PutTextWithMovement("for(<++>; <++>; <++>) {\n<++>;\n}")<cr>
autocmd FileType java inorea <buffer> cif <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}")<cr>
autocmd FileType java inorea <buffer> cifelse <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}\nelse {\n<++>;\n}")<cr>
autocmd FileType java inorea <buffer> cclass <c-r>=IMAP_PutTextWithMovement("class <++> <++> {\n<++>\n}")<cr>
autocmd FileType java inorea <buffer> cmain <c-r>=IMAP_PutTextWithMovement("public static void main(String[] argv) {\n<++>\n}")<cr>
"Presse c-q insted of space (or other key) to complete the snippet
imap <C-q> <C-]>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Cope
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"For Cope
map <silent> <leader><cr> :noh<cr>
"Orginal for all
map <leader>n :cn<cr>
map <leader>p :cp<cr>
map <leader>c :botright cw 10<cr>
map <c-u> <c-l><c-j>:q<cr>:botright cw 10<cr>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => MISC
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Remove the Windows ^M
noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm
"Paste toggle - when pasting something in, don't indent.
set pastetoggle=<F3>
"Remove indenting on empty lines
map <F2> :%s/\s*$//g<cr>:noh<cr>''
"Super paste
inoremap <C-v> <esc>:set paste<cr>mui<C-R>+<esc>mv'uV'v=:set nopaste<cr>
"A function that inserts links & anchors on a TOhtml export.
" Notice:
" Syntax used is:
" *> Link
" => Anchor
function! SmartTOHtml()
TOhtml
try
%s/"\s\+\*> \(.\+\)</" <a href="#\1" style="color: cyan">\1<\/a></g
%s/"\(-\|\s\)\+\*> \(.\+\)</" \ \ <a href="#\2" style="color: cyan;">\2<\/a></g
%s/"\s\+=> \(.\+\)</" <a name="\1" style="color: #fff">\1<\/a></g
catch
endtry
exe ":write!"
exe ":bd"
endfunction
# 最佳vim技巧
----------------------------------------
# 信息來源
----------------------------------------
www.vim.org : 官方站點(diǎn)
comp.editors : 新聞組
http://www.newriders.com/books/opl/ebooks/0735710015.html : Vim書籍
http://vimdoc.sourceforge.net/cgi-bin/vim2html2.pl : 關(guān)于vim的可查詢文檔
http://vimdoc.sourceforge.net/vimfaq.html : VIM FAQ
----------------------------------------
# 基礎(chǔ)
----------------------------------------
* # g* g# : 尋找光標(biāo)處的狹義單詞(<cword>) (前向/后向)
% : 括號配對尋找 {}[]()
matchit.vim : 使得 % 能夠配對標(biāo)記 <tr><td><script> <?php 等等
<C-N><C-P> : 插入模式下的單詞自動完成
<C-X><C-L> : 行自動完成(超級有用)
/<C-R><C-W> : 把狹義單詞 <cword> 寫到 搜索命令 行
/<C-R><C-A> : 把廣義單詞 <cWORD> 寫到 搜索命令 行
:set ignorecase : 搜索時忽略大小寫
:syntax on : 在 Perl,HTML,PHP 等中進(jìn)行語法著色
:h regexp<C-D> : 按下 control-D 鍵即可得到包含有 regexp 的幫助主題的列表
: (使用TAB可以實(shí)現(xiàn)幫助的自動補(bǔ)齊)
----------------------------------------
# 使更新 _vimrc 更容易
:nmap ,s :source $VIM/_vimrc
# 譯釋:nmap 是綁定一個在normal模式下的快捷鍵
:nmap ,v :e $VIM/_vimrc
# 譯釋:在normal模式下,先后按下 ,s 兩個鍵執(zhí)行_vimrc,而 ,v 則是編輯_vimrc
----------------------------------------
# visual 模式 (例子是:輕松添加其他的 HTML Tags)
:vmap sb "zdi<b><C-R>z</b><ESC> : 在visual模式下選中的文字前后分別加上<b>和</b>
# 譯釋:vmap 是綁定一個在visual模式下的快捷鍵
# 譯釋:原理:在visual模式下,"zd 把一個選中的區(qū)域命名為z 然后刪除,
# i 進(jìn)入插入模式,輸入<b>,<C-R>z 撤銷剛才的刪除,然后再寫入</b>,
# 最后<ESC>返回normal模式
# 譯釋:"z 命令創(chuàng)建一個選中的區(qū)域?yàn)閞egister,并把它命名為z
# 譯釋:更令人開心的有:在visual模式下選中幾行,然后輸入 2> ,
# 則選中的行會全部縮進(jìn)兩個tab
# 555,偶一開始還是用 :xx,xx s/^/\t\t/,好傻啊!
:vmap st "zdi<?= <C-R>z ?><ESC> : 在visual模式下選中的文字前后分別加上<?= 和 ?>
----------------------------------------
# 文件瀏覽
:Ex : 開啟目錄瀏覽器,注意首字母E是大寫的
:Sex : 在一個分割的窗口中開啟目錄瀏覽器
:ls : 顯示當(dāng)前buffer的情況
:cd .. : 進(jìn)入父目錄
:args : 顯示目前打開的文件
:lcd %:p:h : 更改到當(dāng)前文件所在的目錄
# 譯釋:lcd是緊緊改變當(dāng)前窗口的工作路徑,% 是代表當(dāng)前文件的文件名,
# 加上 :p擴(kuò)展成全名(就是帶了路徑),加上 :h析取出路徑
:autocmd BufEnter * lcd %:p:h : 自動更改到當(dāng)前文件所在的目錄
# 譯釋:autocmd指定一個自動命令,BufEnter指定一個事件,* 指定事件的對象,
# lcd %:p:h 指定一個動作
# hehe,好像和寫記敘文差不多
----------------------------------------
# 緩沖區(qū)(buffer)瀏覽器 (第三方的一個最流行的腳本)
# 需要下載 bufexplorer.vim ,http://www.vim.org/script.php?script_id=42 上就有
\be : 在緩沖區(qū)瀏覽器中打開緩沖區(qū)列表
\bs : 以分割窗口的形式打開緩沖區(qū)瀏覽器
----------------------------------------
# 大小寫轉(zhuǎn)換
guu : 行小寫
gUU : 行大寫
g~~ : 行翻轉(zhuǎn)(當(dāng)然指大小寫啦)
# 譯釋: g 是大小寫轉(zhuǎn)換命令(greate),u/U/~是三種轉(zhuǎn)換形式(小寫/大寫/翻轉(zhuǎn)),
# 最后一個重復(fù)則表示該轉(zhuǎn)換是對于一行而言的
guw : 字大寫(狹義字) 譯注:建議對比iw
gUw : 字小寫(狹義字)
g~w : 字翻轉(zhuǎn)(狹義字)
# 譯釋:最后一個w 表示該轉(zhuǎn)換是對于一個字而言的,由于是在normal模式下,
# 所以這個w 表示一個狹義字<cword>
vEU : 字大寫(廣義字)
vE~ : 字翻轉(zhuǎn)(廣義字)
# 譯釋:vE 這個指令組合會進(jìn)入visual模式,然后選擇一個廣義字<CWORD>
ggguG : 把整個文章全部小寫(ft!bt!)
gf : 取當(dāng)前光標(biāo)處的廣義字作為文件名,然后試圖打開它!
# 譯釋:為什么是廣義字呢?因?yàn)檫@樣可以方便的取到路徑啊,像/var/www/html/index.htm
ga : 顯示光標(biāo)處字符的ascii,hex,oct,...暈菜的一堆轉(zhuǎn)換
ggVGg? : 用rot13編碼整個文件(暈!)
# 譯釋:gg到文件首行首字符,V進(jìn)入Visual-Line模式,G到文件末行首字符,
# 這樣就選中了整篇文章,然后g?就是用rot13編碼整個文件啦
#
# 【關(guān)于rot13——誰讓英文是偶數(shù)個字母啊】
# ROT13 是一種簡單的編碼,它把字母分成前后兩組,每組13個,編碼和解碼
# 的算法相同,僅僅交換字母的這兩個部分,即:[a..m] --> [n..z] 和 [n..z]
# --> [a..m] 。 ROT13 用簡易的手段使得信件不能直接被識別和閱
# 讀,也不會被搜索匹配程序用通常的方法直接找到。經(jīng)常用于 USENET 中發(fā)表一
# 些攻擊性或令人不快的言論或有簡單保密需要的文章。
# 由于 ROT13 是自逆算法,所以,解碼和編碼是同一個過程。
<C-A>,<C-X> : 增加,減少 光標(biāo)處的狹義字所表示的數(shù)字
:(,僅僅是分割了這兩個命令,不是命令的一部分)
: Win32的用戶可能需要重新定義一下Ctrl-A,呵呵
# 譯注:good guy, 令人不得不想到perl的數(shù)字串
<C-R>=5*5 : 插入25 (這是一個迷你計算器耶!)
----------------------------------------
# 好玩的東東
:h 42 : 也可以訪問 http://www.google.com/search?q=42
: 第一個結(jié)果就是 News. Douglas Adams 1952 - 2001.
: Floor 42 extends its deepest sympathies to
: the family, friends, and fans of Douglas Adams.
:h holy-grail
:h!
----------------------------------------
# 標(biāo)記和移動
'. : 跳到最后修改的那一行 (超級有用)(ft,怎么又是這個評價)
`. : 不僅跳到最后修改的那一行,還要定位到修改點(diǎn)
<C-O> : 依次沿著你的跳轉(zhuǎn)記錄向回跳 (從最近的一次開始)
<C-I> : 依次沿著你的跳轉(zhuǎn)記錄向前跳
:ju(mps) : 列出你跳轉(zhuǎn)的足跡
:help jump-motions
:history : 列出歷史命令記錄
:his c : 命令行命令歷史
:his s : 搜索命令歷史
q/ : 搜索命令歷史的窗口
q: : 命令行命令歷史的窗口
:<C-F> : 歷史命令記錄的窗口
----------------------------------------
# 縮寫和鍵盤映射(原文中文件舉例都用了c:/aaa/x,偶全給他改成/path/file了,哼唧)
:map <f7> :'a,'bw! /path/file
# 譯釋:map是映射一個normal模式下的鍵
# 這里是把F7鍵映射成把標(biāo)記a到標(biāo)記b中間的內(nèi)容另存為一個文件/path/file
# 標(biāo)記(mark)的方法:把光標(biāo)移動到需要標(biāo)記的地方,輸入m,然后輸入標(biāo)記名,例如a
# 引用標(biāo)記的方法:'a ,即:單引號加標(biāo)記名
:map <f8> :r /path/file
# 譯釋:把F8鍵映射成在當(dāng)前位置插入文件/path/file的內(nèi)容
:map <f11> :.w! /path/file2<CR>
# 譯釋:.(點(diǎn)號)表示當(dāng)前行
# 所以F11就是把當(dāng)前行存為/path/file2
# 最后的<CR>表示一個回車
:map <f12> :r /path/file2<CR>
:ab php : 列出php表示的縮寫
# 譯釋:定義一個縮寫使用::iab hm hmisty
# 一個有趣的現(xiàn)象是,它列出的會是php和它的前子串開頭的縮寫
# 例如,有這么幾個縮寫:
# h => hmisty1 , hm => hmisty2 , hmi => hmisty3, m => hmisty4
# 那么使用 :ab hm會顯示這么幾個縮寫:hm 和 h
# 而不是你想象中的 hm 和 hmi
:map , : 列出以逗號開始的鍵盤映射
# 譯釋:一般而言,我們稱這些逗號開始的組合鍵為“逗號命令”
# 不過hmisty更喜歡用;構(gòu)成“分號命令”
# 而且不是用map,而是用imap
# 因?yàn)榕紤忻矗瑧械冒?lt;Esc>,所以直接在insert模式下就執(zhí)行命令了
# 為什么用分號呢?因?yàn)槲易畛S盟鼘懗绦虬?br /> # perl/C/C++/object pascal/java,都是用分號結(jié)束一個語句
# 我們一般很少在分號后面連續(xù)寫其他字符
# 所以用“分號+其他鍵”就很少會在輸入的時候造成沖突
# 在鍵盤映射中常用的表示
<CR> : 回車
<ESC> : Esc
<LEADER> : 轉(zhuǎn)義符號 \
<BAR> : 管道符號 |
----------------------------------------
# 列出寄存器(Registers)
:reg : 顯示所有當(dāng)前的registers
"1p : "表示引用register,1表示一個名字叫做1的register,
: p就是粘貼(paste)命令
# 譯釋:"也用來定義register
# 先輸入 ",表示定義register
# 然后輸入名字,如0~9,a~z
# 然后執(zhí)行刪除或復(fù)制命令,如dd或y,
# 或者是visual模式下的d(刪除選中的部分)或y(復(fù)制選中的部分)
# 則被刪除或復(fù)制的部分就被存入了這個命名的register
#
# 觀察:一個特殊的register, "" ,里面存儲了一個匿名的刪除/復(fù)制
# 在你執(zhí)行dd或y的時候,被作用的部分被存到了""中
# 這些和perl是多么像啊
----------------------------------------
# Useful trick
"ayy@a : 把當(dāng)前行作為一個Vim命令來執(zhí)行
# 譯釋:"ayy 是定義當(dāng)前行到register a,然后@a是執(zhí)行register a中存儲的指令
# yy: 復(fù)制一行
# 10yy: 復(fù)制從此向下的10行
yy@" : 用上面所提到的那個匿名register
----------------------------------------
# 從其他程序獲取輸出 (需要外部程序)
:r!ls.exe : 讀取ls的輸出到當(dāng)前位置
!!date : 讀取date的輸出 (但是會替換當(dāng)前行的內(nèi)容)
# 譯釋:其實(shí)你輸入了!!后,vim就自動轉(zhuǎn)換到 :.! 等待你繼續(xù)輸入
# 使用外部程序sort進(jìn)行排序(sort是Unix標(biāo)準(zhǔn)命令,ls,date也是)
:%!sort -u : 使用sort程序排序整個文件(用結(jié)果重寫文件)
# 譯釋:%表示整個文件的所有行
# !sort表示執(zhí)行外部命令sort
# -u是sort的參數(shù),man sort看看,這個參數(shù)的意義是合并相同的行
# u就是unique,如果兩行內(nèi)容相同,則結(jié)果中只保留一行的說
:'a,'b!sort -u : 對mark a 到mark b中間的內(nèi)容進(jìn)行排序
!1} sort -u : 排序當(dāng)前段落 (只能在normal模式下使用!!)
# 譯釋:!表示使用filter,1}表示filter的對象是從當(dāng)前行開始向后數(shù)一段
# 段落指到空行處結(jié)束,不包括空行
# 其實(shí)你一旦輸入 !1},vim就自動計算當(dāng)前段落應(yīng)該到那一行(eg.+5),然后生成
# :.,.+5! 等待之后輸入sort -u,回車,完成操作
# .表示當(dāng)前行,.+5當(dāng)然就是當(dāng)前行向后數(shù)5行
----------------------------------------
# 多文檔操作 (基礎(chǔ))
# 譯注:用 :ls! 可以顯示出當(dāng)前所有的buffer
:bn : 跳轉(zhuǎn)到下一個buffer
:bp : 跳轉(zhuǎn)到上一個buffer
:wn : 存盤當(dāng)前文件并跳轉(zhuǎn)到下一個(又是“超級……”,ft!)
:wp : 存盤當(dāng)前文件并跳轉(zhuǎn)到上一個
:bd : 把這個文件從buffer列表中做掉
:bun : 卸掉buffer (關(guān)閉這個buffer的窗口但是不把它從列表中做掉)
:badd file.c : 把文件file.c添加到buffer列表
:b 3 : 跳到第3個buffer
:b main : 跳到一個名字中包含main的buffer,例如main.c
: (ultra,這個怎么翻譯?:()
:sav php.html : 把當(dāng)前文件存為php.html并打開php.html
:sav! %<.bak : 換一個后綴保存
:e! : 返回到修改之前的文件(修改之后沒有存盤)
:w /path/% : 把文件存到一個地兒
:e # : 編輯標(biāo)記為#的buffer(這個buffer必須含有一個可編輯的文件)
: 用ls命令就能看到哪一個buffer有#
: %a表示當(dāng)前正在編輯的buffer
: u 表示不能編輯或者已經(jīng)被做掉的buffer
:e #3 : 編輯編號為3的buffer(這個buffer必須含有一個可編輯的文件)
:rew : 回到第一個可編輯的文件
:brew : 回到第一個buffer
:sp fred.txt : 在一個水平分割的窗口中打開文件fred.txt
# 譯注:vs fred.txt可以實(shí)現(xiàn)垂直分割
:sball : 把當(dāng)前所有含有可編輯文件的buffer顯示到一個分割窗口中
: (偶該考慮把super翻譯成 高級指令 了,ft)
:map <F5> :ls<CR>:e # : 在normal模式下按F5鍵,則會顯示所有含有一個
: 可編輯文件的buffer,然后提示你輸入buffer的序號,
: 輸入后回車,則編輯這個buffer
# 譯注:這是一個鍵盤綁定
:set hidden : 允許不保存buffer而切換buffer (w/o=without)
----------------------------------------
# 在分割窗口中快速切換
:map <C-J> <C-W>j<C-W>_
# 譯注:原文此處有誤,前面應(yīng)該加上冒號
# 這是一個鍵盤綁定,把Ctrl-J定義成切換到下一個窗口并最大化
:map <C-K> <C-W>k<C-W>_
----------------------------------------
# 命令錄制 (最佳技巧,ft)
qq #錄制到q
... #輸入一系列復(fù)雜的指令
q #再次按q停止錄制
@q #執(zhí)行q中存儲的指令
@@ #重復(fù)執(zhí)行
# 編輯register/錄制
"ap #把register a中的內(nèi)容貼到當(dāng)前位置
... #現(xiàn)在你可以修改它了
"add#刪除之,重新存入register a
@a #執(zhí)行register a中的指令
----------------------------------------
# _vimrc基礎(chǔ)
:set incsearch : 實(shí)時匹配你輸入的內(nèi)容
:set wildignore=*.o,*.obj,*.bak,*.exe : tab鍵的自動完成現(xiàn)在會忽略這些
:set shiftwidth=4 : 現(xiàn)在自動縮進(jìn)將是4個字符
# 譯注:一個tab位通常是8個字符
# 所以,我們還要設(shè)定 :set tabstop=4,這樣,所有的縮進(jìn)都是4字符了
# emacs默認(rèn)就是4字符縮進(jìn)吧?
:set vb t_vb=". : 沉默方式(不要叫beep!)
----------------------------------------
# 加載windows iexplorer來瀏覽(我想這只有在windows下用gvim才能用到)
:nmap ,f :update<CR>:silent !start c:\progra~1\intern~1\iexplore.exe file://%:p
# 譯釋:nmap是做一個normal模式下的鍵盤綁定
# 這里綁定了一個逗號命令 ,f
# :update是寫這個文件,與:w不同,它只有當(dāng)文件被修改了的時候才寫
# :silent別讓彈出窗口報告執(zhí)行結(jié)果
# !...后面就是執(zhí)行windows命令了。呵呵,去問bill gates什么意思吧。
# 不過偶用gvim 6.1試過了,好用!
:nmap ,i :update<CR>: !start c:\progra~1\intern~1\iexplore.exe <cWORD><CR>
----------------------------------------
# 用VIM編輯ftp文件
:cmap ,r :Nread ftp://209.51.134.122/public_html/index.html
:cmap ,w :Nwrite ftp://209.51.134.122/public_html/index.html
# 譯注:原文丟失了開頭的冒號
# cmap是命令(command)模式綁定
gvim ftp://209.51.134.122/public_html/index.html
# 這一句就是開始編輯一個ftp遠(yuǎn)端的文件,ft
----------------------------------------
# 附加到一個register (就是用大寫的register名字啦!)
"a5yy #復(fù)制5行到a中
10j #下移10行
"A5yy #再添加5行到a中
----------------------------------------
[I : 顯示光標(biāo)處的狹義字可以匹配的行(高級指令)
# 譯注:# 可以全文查找與光標(biāo)處的狹義字相匹配的字,
# 這在查找函數(shù)原型和實(shí)現(xiàn),或者變量使用的時候很有用
----------------------------------------
# 常規(guī)縮進(jìn)
:'a,'b>>
# 譯釋:把mark a到mark b之間的內(nèi)容進(jìn)行兩次縮進(jìn)
# 在visual模式下縮進(jìn) (無限可重復(fù))
:vnoremap < <gv
# 譯釋::vnoremap 重定義了visual模式下 < 符號的含義
# 把它定義成 <gv
# 即:先<向外縮進(jìn),然后gv重新選擇上一次選擇了的區(qū)域
# 這樣在visual模式下就可以實(shí)現(xiàn)連續(xù)按<而連續(xù)縮進(jìn)了
:vnoremap > >gv
# 同里,內(nèi)縮
----------------------------------------
# 查找(譯注:建議先學(xué)習(xí)正則表達(dá)式)
# 譯注:查找命令不用進(jìn)入:命令模式,直接按/就可以了
# 如果沒有修飾,可以不要右邊的/
# 和smth bbs差不多啦,呵呵
/joe/e : 光標(biāo)停留在匹配單詞最后一個字母處
/joe/e+1 : 光標(biāo)停留在匹配單詞最后一個字母的下一個字母處
/joe/s : 光標(biāo)停留在匹配單詞第一個字母處
/^joe.*fred.*bill/ : ft,標(biāo)準(zhǔn)正則表達(dá)式
/^[A-J]\+/ : 找一個以A~J中一個字母重復(fù)兩次或以上開頭的行
/forum\(\_.\)*pent : 多行匹配
/fred\_s*joe/i : 中間可以有任何空白,包括換行符\n
# 譯注:這個和perl不太一樣的哦
/fred\|joe : 匹配FRED或JOE
/\<fred\>/i : 匹配fred,fred必須是一個獨(dú)立的單詞,而不是子串
# 譯注:這和perl也不太一樣,perl是用\b做單詞定界符的
/\<\d\d\d\d\> : 匹配4個數(shù)字
\<\d\{4}\> : 也是匹配4個數(shù)字
# 在visual模式下查找
:vmap g/ y/<C-R>"<CR> : 匹配選中的高亮文字
# 譯釋:vmap是在visual模式下的鍵盤映射
# 映射了g/這個命令組合
# y 把選中的高亮文字寫入匿名register "
# / 打開搜索模式
# <C-R> 準(zhǔn)備粘貼register
# " 粘貼了""中的內(nèi)容
# <CR> 回車,執(zhí)行
:vmap <silent> g/ y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR> : with spec chars
# 譯釋:@#$&^*@#%&*#$@!
# 跨行匹配,\_ 表示允許匹配換行符,或者說,允許匹配新行
# 譯注:小心,和perl不一樣
/<!--\_p\{-}--> : 匹配多行注釋
/fred\_s*joe/i : 似乎上面有了,ft
/bugs\(\_.\)*bunny : 中間可以有無數(shù)東西
:h \_ : 看看關(guān)于 \_ 的幫助
# 查找當(dāng)前光標(biāo)位置所在子例程/函數(shù)(subroutine/function)的聲明
:nmap gx yiw/^\(sub\<bar>function\)\s\+<C-R>"<CR>
# 譯釋:nmap 做一個normal模式下的鍵盤綁定
# y 進(jìn)入復(fù)制狀態(tài),后面需要一個motion
# 接著就用 iw 指出了這個motion,是inner word
# inner word也是狹義字<cword>,但是和 w 不同
# w 是從光標(biāo)位置開始向后看
# 而inner word總是把光標(biāo)移到第一個字母,從而總能得到一個完整的狹義字
# 試一試 gUw 和 gUiw 就知道區(qū)別了,呵呵。
# 在多個文檔中搜索
:bufdo /searchstr
:argdo /searchstr
----------------------------------------
# 替換
# 譯注:替換命令需要先進(jìn)入:命令模式
:%s/fred/joe/igc : 一個常見的替換命令,修飾符igc和perl中一樣意思
:%s/\r//g : 刪除DOS方式的回車^M
:%s= *$== : 刪除行尾空白
:'a,'bg/fred/s/dick/joe/igc : 非常有用!(ft,又來了!)
# 譯釋:'a,'b指定一個范圍:mark a ~ mark b
# g//用一個正則表達(dá)式指出了進(jìn)行操作的行必須可以被fred匹配
# 看后面,g//是一個全局顯示命令
# s/dick/joe/igc則對于這些滿足條件的行進(jìn)行替換
# 列復(fù)制
# 譯注:@#%&^#*^%#$!
:%s= [^ ]\+$=&&= : 復(fù)制最后一列
:%s= \f\+$=&&= : 一樣的功能
:%s= \S\+$=&& : ft,還是一樣
# 反向引用,或稱記憶
:s/\(.*\):\(.*\)/\2 : \1/ : 顛倒用:分割的兩個字段
:%s/^\(.*\)\n\1/\1$/ : 刪除重復(fù)行
# 非貪婪匹配,\{-}
:%s/^.\{-}pdf/new.pdf/ : 只是刪除第一個pdf
# 跨越可能的多行
:%s/<!--\_.\{-}-->// : 又是刪除多行注釋(咦?為什么要說“又”呢?)
:help /\{-} : 看看關(guān)于 非貪婪數(shù)量符 的幫助
:s/fred/<c-r>a/g : 替換fred成register a中的內(nèi)容,呵呵
# 寫在一行里的復(fù)雜命令
:%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/
# 譯注:就是用 | 管道啦
# 或者
:%s/suck\|buck/loopy/gc : 或者(或者需要\,ft!,|不是或者)
# ft, \不就是轉(zhuǎn)義了么!這個和perl真是不同了!
# 調(diào)用VIM函數(shù)
:s/__date__/\=strftime("%c")/ : 插入時間串
# 處理列,替換所有在第三列中的str1
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2:
# 交換第一列和最后一列 (共4列)
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1:
# filter all form elements into paste register
# 把所有的form元素(就是html里面的form啦)放到register里?
# ft, 頭疼,不解釋了
:redir @*|sil exec 'g#<\(input\|select\|textarea\|/\=form\)\>#p'|redir END
:nmap ,z :redir @*<Bar>sil exec 'g@<\(input\<Bar>select\<Bar>textarea\<Bar>/\=fo
----------------------------------------
# 全局(global)顯示命令,就是用 :g+正則表達(dá)式
# 譯釋: :g/{pattern}/{cmd} 就是全局找到匹配的行
# 然后對這些行執(zhí)行命令{cmd}
:g/\<fred\>/ : 顯示所有能夠?yàn)閱卧~fred所匹配的行
:g/<pattern>/z#.5 : 顯示內(nèi)容,還有行號,呵呵
:g/<pattern>/z#.5|echo "==========" : 漂亮的顯示,ft!
# 全局命令 (其他)
:g/^\s*$/d : 刪除所有空行
:g!/^dd/d : 刪除不含字串'dd'的行
:v/^dd/d : 同上
# 譯釋:v == g!,就是不匹配!
:g/fred/,/joe/d : not line based (very powerfull)
:v/./.,/./-1join : 壓縮空行
:g/^$/,/./-j : 壓縮空行
:g/<input\|<form/p : 或者 要用\|
:g/^/pu _ : 把文中空行擴(kuò)增一倍 (pu = put)
: 即:原來兩行間有一個空行,現(xiàn)在變成2個
:g/^/m0 : 按行翻轉(zhuǎn)文章 (m = move)
:g/fred/t$ : 拷貝行,從fred到文件末尾(EOF)
:%norm jdd : 隔行刪除
# 譯釋:% 指明是對所有行進(jìn)行操作
# norm指出后面是normal模式的指令
# j是下移一行,dd是刪除行
# incrementing numbers
:.,$g/^\d/exe "norm! \<c-a>" : 增加在BOL(beginning of line)處的數(shù)字
# 譯注:.,$ 指明命令從當(dāng)前行執(zhí)行到最后一行
# 如果沒有 .,$ 限定范圍,那么g//就會對整個文件進(jìn)行操作
# exe 是執(zhí)行后面的命令組合
:.,$g/^\d/exe "norm \<c-p>" : Win32下必須重定義Ctrl-A
# 保存全局命令的結(jié)果 (注意必須使用添加模式)
:g/fred/y A : 添加所有為fred所匹配的行到register a
:'a,'b g/^Error/ . w >> errors.txt
# 復(fù)制每一行,然后在復(fù)制出來的每一行兩側(cè)加上一個 print '復(fù)制出來的內(nèi)容'
:g/./yank|put|-1s/'/"/g|s/.*/Print '&'/
----------------------------------------
# 全局命令和替換命令聯(lián)姻 (強(qiáng)大的編輯能力)
:'a,'bg/fred/s/joe/susan/gic : 可以使用反向引用來匹配
:g/fred/,/joe/s/fred/joe/gic : non-line based (ultra)
----------------------------------------
# 先找fred,然后找joe,然后#$^$%^#$%^@%^%&%^*!
:/fred/;/joe/-2,/sid/+3s/sally/alley/gIC
----------------------------------------
# 重定向到register * 和 粘貼register *
:redir @* : 重定向命令的輸出結(jié)果(最下方命令行上的結(jié)果)
: 到register * (ft,* 代表0~1,a~z,..)
:redir END : 結(jié)束重定向
# 處理粘貼
"*yy : 上面講過了,就是復(fù)制到register *中
"*p : 然后貼出來
----------------------------------------
:redir >> out.txt : 重定向到一個文件
----------------------------------------
# 重新格式化文本
gq<CR>
gqap (a是motion p是段落(visual模式))
ggVGgq 重新格式化整個文章
----------------------------------------
# 對多個文檔實(shí)施命令
:argdo %s/foo/bar/ : 對所有:args列表中的文檔執(zhí)行命令
:bufdo %s/foo/bar/
:windo %s/foo/bar/
:argdo exe '%!sort'|w! : 使用外部命令
----------------------------------------
# 命令行的一些好玩的東東
gvim -h : 啟動的時候啟動幫助(Win32)
vi -h 或 vim -h : 這個是unix下用
ls | gvim - : 編輯一個數(shù)據(jù)流!
gvim -o file1 file2 : 以分割窗口打開兩個文件
# 指出打開之后執(zhí)行的命令
gvim.exe -c "/main" joe.c : 打開joe.c,然后跳轉(zhuǎn)到'main'
# 對一個文件執(zhí)行多個命令
vim -c "%s/ABC/DEF/ge | update" file1.c
# 對一組文件執(zhí)行多個命令
vim -c "argdo %s/ABC/DEF/ge | update" *.c
# 自動編輯文件 (編輯命令序列Ex commands已經(jīng)包含在convert.vim中了)
vim -s "convert.vim" file.c
# 不要加載.vimrc和任何plugins (啟動一個干凈的VIM)
gvim -u NONE -U NONE -N
----------------------------------------
# GVIM 不同的地方
gvim -d file1 file2 : vimdiff (比較不同)
dp : 把光標(biāo)處的不同放到另一個文件
do : 在光標(biāo)處從另一個文件取得不同
----------------------------------------
# Vim陷阱
# 在vim的正則表達(dá)式中, + 和 | 都必須加轉(zhuǎn)義符 \
# 小心,這和perl不一樣!
/fred\+/ : 匹配fred或freddy但是不匹配free
----------------------------------------
# \v ,或叫做very magic (通常都是這么叫)可以取消轉(zhuǎn)義符
/codes\(\n\|\s\)*where : 普通的正則表達(dá)式
/\vcodes(\n|\s)*where : very magic,| 不用加 \ 了!
----------------------------------------
# 把東西送到命令行/搜索行 (SUPER:偶不再翻譯這種嘆詞了)
<C-R><C-W> : 送一個狹義詞
<C-R><C-A> : 送一個廣義詞
<C-R>- : 送一個小型刪除寄存器register
<C-R>[0-9a-z] : 送一個命名寄存器register
<C-R>% : 送文件名過去 (#也行)
----------------------------------------
# 操作寄存器
:let @a=@_ : 清除register a
:let @*=@a : 寄存器賦值
:map <f11> "qyy:let @q=@q."zzz"
# 譯注:猜猜這個無聊的綁定是什么意思?
----------------------------------------
# 關(guān)于幫助的幫助
:h quickref : 翻到VIM Quick Reference頁(有用!)
:h tips : Vim自己的tips
:h visual<C-D><tab> : 得到一個關(guān)于visual關(guān)鍵字的幫助列表
: 然后用tab鍵去選擇
:h ctrl<C-D> : 顯示所有關(guān)于Ctrl的幫助
:h :r : :ex冒號命令
:h CTRL-R : 普通模式命令
:h \r : \r在正則表達(dá)式中是什么意思呢?
:h i_CTRL-R : insert模式下的Ctrl-R
:h c_CTRL-R : 命令行(command-line)模式下的Ctrl-R
:h v_CTRL-V : visual模式下的Ctrl-V
:h tutor : VIM 指南
gvim -h : 關(guān)于 VIM 命令的幫助
vi/vim -h
<C-S>T : Control Shift T go backwards in help
: 偶不清楚有什么用:(
----------------------------------------
# 選項(xiàng)設(shè)置在哪里?
:scriptnames : 列出所有加載的 plugins, _vimrcs
:verbose set history : 顯示history的值并指出設(shè)置文件的位置
----------------------------------------
# 制作你自己的VIM幫助
:helptags /vim/vim61/doc : 重建 /doc 中所有的 *.txt 幫助文件
:help add-local-help
----------------------------------------
# 用外部程序來運(yùn)行程序 (例如 perl :)
map <f2> :w<CR>:!perl -c %<CR>
# 譯釋::w<CR>寫文件
# :!perl -c %<CR>用perl來運(yùn)行當(dāng)前文件
# 當(dāng)前文件必須有文件名!
----------------------------------------
# 插入DOS換行符
:%s/nubian/<C-V><C-M>&/g : Ctrl-V是一種轉(zhuǎn)義,它說要解釋<C-M>
:%s/nubian/<C-Q><C-M>&/g : 對于Win32應(yīng)該這樣
:%s/nubian/^M&/g : 你看到的^M是一個字符
:%s/nubian/\r&/g : 更好的形式
----------------------------------------
# 把最后一個命令貼到當(dāng)前位置
i<c-r>:
# 把最后一個搜索指令貼到當(dāng)前位置
i<c-r>/
# 譯釋:i是進(jìn)入insert模式,
# Ctrl-r是開啟插入模式下register的引用
# :和/分別引用了兩個register的內(nèi)容
----------------------------------------
# 更多的完成功能
<C-X><C-F> :插入當(dāng)前目錄下的一個文件名到當(dāng)前位置
# 在insert模式下使用
# 然后用 Ctrl-P/Ctrl-N 翻頁
----------------------------------------
# 替換一個visual區(qū)域
# 選擇一個區(qū)域,然后輸入 :s/Emacs/Vim/ 等等,vim會自動進(jìn)入:模式
:'<,'>s/Emacs/Vim/g : 前面的'<,'>是vim自動添加的
----------------------------------------
# 在文件中插入行號(不是顯示行號,是插入!)
:g/^/exec "s/^/".strpart(line(".")." ", 0, 4)
----------------------------------------
# 用VIM的方式來編號行
:set number :顯示行號
:set nonu :取消顯示
:%s/^/\=strpart(line('.')." ",0,&ts)
#從任意行開始編號(需要perl,嘿嘿)
:'a,'b!perl -pne 'BEGIN{$a=223} substr($_,2,0)=$a++'
#似乎有點(diǎn)小問題,你試試看:)
qqmnYP`n^Aq : 記錄到q 然后用 @q 重復(fù)
#似乎不能工作,你試試看:)
# 遞增已存在數(shù)字到文件末
:.,$g/^\d/exe "normal! \<c-a>"
# 高級遞增,看:
http://vim.sourceforge.net/tip_view.php?tip_id=150
----------------------------------------
# 高級遞增 ("真的很有用",ft)
" 把下面幾句放到 _vimrc #vimrc腳本用 " 做行注釋符
let g:I=0
function! INC(increment)
let g:I =g:I + a:increment
return g:I
endfunction
" 例如從mark a 到mark b 遞增,從223開始,步長為5
:let I=223
:'a,'bs/$/\=INC(5)/
" (原文:create a map for INC)
" 但是cab是清楚命令行縮寫啊?怎么回事?
cab viminc :let I=223 \| 'a,'bs/$/\=INC(5)/
----------------------------------------
# 加密(小心使用,不要忘了密碼)
:X : 然后vim會提示你輸入密碼
:h :X
----------------------------------------
# 模式行(modeline)
# 第二版新加,感謝tcpip
vim:noai:ts=2:sw=4:readonly: #讓文檔只讀
# 譯釋:這一行必須以vim:開頭,而且只能在文檔的前5行或后5行之內(nèi)
# 后面是需要執(zhí)行的命令,依次是:
# noai noautoindent
# ts=2 tabstop=2
# sw=4 shiftwidth=4
# readonly readonly
:h modeline #看看關(guān)于modeline的幫助先!
----------------------------------------
# Creating your own GUI Toolbar entry
# 對于text模式下的vim沒用,不翻了
amenu Modeline.Insert\ a\ VIM\ modeline <Esc><Esc>ggOvim:ff=unix ts=4 ss=4<CR>v
----------------------------------------
# 一個保存當(dāng)前光標(biāo)下的狹義字到一個文件的函數(shù)
function! SaveWord() "這里用!是強(qiáng)制覆蓋以前的定義
normal yiw
exe ':!echo '.@0.' >> word.txt'
endfunction
map ,p :call SaveWord() #使用該函數(shù)的一個例子
----------------------------------------
# 刪除重復(fù)行的函數(shù)
function! Del()
if getline(".") == getline(line(".") - 1)
norm dd
endif
endfunction
:g/^/ call Del() #使用該函數(shù)的一個例子
----------------------------------------
# 雙字節(jié)編碼 (non alpha-numerics)
:digraphs : 顯示編碼表
:h dig : 幫助
i<C-K>e' : 輸入 é
i<C-V>233 : 輸入 é (Unix)
i<C-Q>233 : 輸入 é (Win32)
ga : 查看字符的hex值
----------------------------------------
# 文件名自動完成 (例如 main_c.c)
:e main_<tab> : tab 鍵完成
gf : 打開光標(biāo)處廣義字命名的文件 (normal模式)
main_<C-X><C-F> : 文件名自動完成(insert模式)
----------------------------------------
# Vim復(fù)雜使用
# 交換兩個單詞
:%s/\<\(on\|off\)\>/\=strpart("offon", 3 * ("off" == submatch(0)), 3)/g
----------------------------------------
# 把text文件轉(zhuǎn)換成html文件(oh,ft)
:runtime! syntax/2html.vim : 轉(zhuǎn)換 txt 成 html
:h 2html : 看看幫助
----------------------------------------
# VIM 有一個內(nèi)部自帶的 grep 命令
:grep some_keyword *.c : 得到一個包含some_keyword的c文件名列表
:cn : 去下一個出現(xiàn)的位置
----------------------------------------
# 強(qiáng)制無后綴文件的語法著色方式 .pl
:set syntax=perl
# 取消語法著色
:set syntax off
# 改變色彩主題 (在~vim/vim??/colors中的任何文件)
:colorscheme blue
----------------------------------------
:set noma (non modifiable) : 防止修改
:set ro (Read Only) : 只讀保護(hù)
----------------------------------------
# Sessions (打開一系列文件)
gvim file1.c file2.c lib/lib.h lib/lib2.h :在"session"中加載一系列文件
:mksession : 生成一個Session文件 (默認(rèn)是Session.vim)
:q
gvim -S Session.vim : 重新讀取一個session,也就讀取了所有文件,ft
----------------------------------------
# 標(biāo)記(tags) (跳轉(zhuǎn)到subroutines/functions)
taglist.vim : 最流行的插件
:Tlist : 顯示Tags (functions的列表)
<C-]> : 跳轉(zhuǎn)到光標(biāo)處的function
: 這個鍵 Ctrl-] 和vim幫助中是一樣的
----------------------------------------
# Just Another Vim Hacker JAVH
# Juat Another Perl Hacker JAPH,嘿嘿
vim -c ":%s/^/WhfgTNabgureRIvzSUnpxre/|:%s/[R-T]/ /Ig|:normal ggVGg?"
# 譯釋:呵呵,誰來解釋一下吧!
# 其實(shí)不過是在啟動vim的時候執(zhí)行了一個命令
# 先寫入了 Just Another Vim Hacker 的rot13編碼
# 然后再解碼
----------------------------------------
終于翻完了,呵呵。好累啊!
__END__