📄 tips.txt
字号:
==============================================================================Counting words, lines, etc. *count-items*To count how often any pattern occurs in a buffer, set 'report' to 0, and usethe substitute command to replace the pattern with itself. The reportednumber of substitutions is the number of items. Examples:> :set report=0> :%s/./&/g characters> :%s/\i\+/&/g words> :%s/^//g lines> :%s/the/&/g "the" anywhere> :%s/\<the\>/&/g "the" as a wordYou might want to reset 'hlsearch' or do ":nohlsearch".==============================================================================Restoring the cursor position *restore-position*Sometimes you want to write a mapping that makes a change somewhere in thefile and restores the cursor position, without scrolling the text. Forexample, to change the date mark in a file:> map <F2> msHmtgg/Last [cC]hange:\s*/e+1<CR>"_D"=strftime("%Y %b %d")<CR>p'tzt`sBreaking up saving the position: ms store cursor position in the 's' mark H go to the first line in the window mt store this position in the 't' markBreaking up restoring the position: 't go to the line previously at the top of the window zt scroll to move this line to the top of the window `s jump to the original position of the cursor==============================================================================Renaming files *rename-files*Say I have a directory with the following files in them (directory picked atrandom :-):buffer.ccharset.cdigraph.c...and I want to rename *.c *.bla. I'd do it like this:> $ vim> :r! ls *.c> :%s/\(.*\).c/mv & \1.bla> :w !sh> :q!==============================================================================Speeding up external commands *speed-up*In some situations, execution of an external command can be very slow. Thiscan also slow down wildcard expansion on Unix. Here are a few suggestions toincrease the speed.If your .cshrc (or other file, depending on the shell used) is very long, youshould separate it into a section for interactive use and a section fornon-interactive use (often called secondary shells). When you execute acommand from Vim like ":!ls", you do not need the interactive things (forexample, setting the prompt). Put the stuff that is not needed after theselines:> if ($?prompt == 0) then> exit 0> endifAnother way is to include the "-f" flag in the 'shell' option, e.g.:> :set shell=csh\ -f(the backslash is needed to include the space in the option).This will make csh completely skip the use of the .cshrc file. This may causesome things to stop working though.==============================================================================Useful mappings *useful-mappings*Here are a few mappings that some people like to use. *map-backtick*> :map ' `Make the single quote work like a backtick. Puts the cursor on the column ofa mark, instead of going to the first non-blank character in the line. *emacs-keys*For Emacs-style editing on the command-line:> " start of line> :cnoremap <C-A> <Home>> " back one character> :cnoremap <C-B> <Left>> " delete character under cursor> :cnoremap <C-D> <Del>> " end of line> :cnoremap <C-E> <End>> " forward one character> :cnoremap <C-F> <Right>> " recall newer command-line> :cnoremap <C-N> <Down>> " recall previous (older) command-line> :cnoremap <C-P> <Up>> " back one word> :cnoremap <Esc><C-B> <S-Left>> " forward one word> :cnoremap <Esc><C-F> <S-Right>NOTE: This requires that the '<' flag is excluded from 'cpoptions'. |<>| *format-bullet-list*This mapping will format any bullet list. It requires that there is an emptyline above and below each list entry. The expression commands are used tobe able to give comments to the parts of the mapping.> let m = ":map _f :set ai<CR>" " need 'autoindent' set> let m = m . "{O<Esc>" " add empty line above item> let m = m . "}{)^W" " move to text after bullet> let m = m . "i <CR> <Esc>" " add space for indent> let m = m . "gq}" " format text after the bullet> let m = m . "{dd" " remove the empty line> let m = m . "5lDJ" " put text after bullet> execute m |" define the mapping(<> notation |<>|. Note that this is all typed literally. ^W is "^" "W", notCTRL-W. You can copy/paste this into Vim if '<' is not included in'cpoptions')Note that the last comment starts with |", because the ":execute" commanddoesn't accept a comment directly.You also need to set 'textwidth' to a non-zero value, e.g.,> set tw=70A mapping that does about the same, but takes the indent for the list from thefirst line (Note: this mapping is a single long line with a lot of spaces):> :map _f :set ai<CR>}{a <Esc>WWmmkD`mi<CR><Esc>kkddpJgq}'mJO<Esc>j *collapse*These two mappings reduce a sequence of empty (;b) or blank (;n) lines into asingle line> :map ;b GoZ<Esc>:g/^$/.,/./-j<CR>Gdd> :map ;n GoZ<Esc>:g/^[ <Tab>]*$/.,/[^ <Tab>]/-j<CR>Gdd==============================================================================Compressing the help files *gzip-helpfile*For those of you who are really short on disk space, you can compress the helpfiles and still be able to view them with Vim. This makes accessing the helpfiles a bit slower and requires the "gzip" program.(1) Compress all the help files: "gzip doc/*.txt".(2) Edit "doc/tags" and change the ".txt" to ".txt.gz":> :%s=\(\t.*\.txt\)\t=\1.gz\t=(3) Add these lines to your vimrc:> augroup helpgzip> autocmd!> autocmd BufReadPre *.txt.gz set bin> autocmd BufReadPost *.txt.gz let ch_save = &ch|set ch=2> autocmd BufReadPost *.txt.gz '[,']!gunzip> autocmd BufReadPost *.txt.gz set nobin> autocmd BufReadPost *.txt.gz let &ch = ch_save|unlet ch_save> autocmd BufReadPost *.txt.gz execute ":doautocmd BufReadPost " . expand("%:r")> augroup END> set helpfile={dirname}/help.txt.gzWhere {dirname} is the directory where the help files are. If you havealready included autocommands to edit ".gz" files (from$VIMRUNTIME/vimrc_example.vim), you should omit the three "autocmd" lines.Setting 'cmdheight' to two is to avoid the |hit-return| prompt; it is notmandatory. You must also make sure that $VIMRUNTIME is set to where the otherVim files are, when they are not in the same location as the compressed "doc"directory. See |$VIMRUNTIME|.==============================================================================Executing shell commands in a window *shell-window*There have been questions for the possibility to execute a shell in a windowinside Vim. The answer: you can't! Including this would add a lot of code toVim, which is a good reason not to do this. After all, Vim is an editor, itis not supposed to do non-editing tasks. However, to get something like this,you might try splitting your terminal screen or display window with the"splitvt" program. You can probably find it on some ftp server. The personthat knows more about this is Sam Lantinga <slouken@cs.ucdavis.edu>.An alternative is the "window" command, found on BSD Unix systems, whichsupports multiple overlapped windows. Or the "screen" program, found atwww.uni-erlangen.de, which supports a stack of windows.==============================================================================Hex editing *hex-editing* *using-xxd*When you need to edit binary data, and prefer to do that on a hexadecimallisting, you can use the xxd program. Use this to convert from binary tohexadecimal:> :%!xxdand use this to convert it back:> :%!xxd -rIf one has a particular extension that one uses for binary files (such as exe,bin, etc), you may find it helpful to automate the process with the followingbit of autocmds for your <.vimrc>. Change that "*.bin" to whatevercomma-separated list of extension(s) you find yourself wanting to edit:> " vim -b : edit binary using xxd-format!> augroup Binary> au!> au BufReadPre *.bin let &bin=1> au BufReadPost *.bin if &bin | %!xxd> au BufReadPost *.bin set ft=xxd | endif> au BufWritePre *.bin if &bin | %!xxd -r> au BufWritePre *.bin endif> au BufWritePost *.bin if &bin | %!xxd> au BufWritePost *.bin set nomod | endif> augroup END==============================================================================Using <> notation in autocommands *autocmd-<>*The <> notation is not recognized in the argument of an :autocmd. To avoidhaving to use special characters, you could use a self-destroying mapping toget the <> notation and then call the mapping from the autocmd. Example: *map-self-destroy*> " This is for automatically adding the name of the file to the menu list.> " It uses a self-destroying mapping!> " 1. use a line in the buffer to convert the 'dots' in the file name to \.> " 2. store that in register '"'> " 3. add that name to the Buffers menu list> " WARNING: this does have some side effects, like overwriting the> " current register contents and removing any mapping for the "i" command.> "> autocmd BufNewFile,BufReadPre * nmap i :nunmap i<CR>O<C-R>%<Esc>:.g/\./s/\./\\./g<CR>0"9y$u:menu Buffers.<C-R>9 :buffer <C-R>%<C-V><CR><CR>> autocmd BufNewFile,BufReadPre * normal iAnother method, perhaps better, is to use the ":execute" command. In thestring you can use the <> notation by preceding it with a backslash. Don'tforget to double the number of existing backslashes and put a backslash before'"'.> autocmd BufNewFile,BufReadPre * exe "normal O\<C-R>%\<Esc>:.g/\\./s/\\./\\\\./g\<CR>0\"9y$u:menu Buffers.\<C-R>9 :buffer \<C-R>%\<C-V>\<CR>\<CR>"For a real buffer menu, user functions should be used (see |:function|), butthen the <> notation isn't used, which defeats using it as an example here. vim:ts=8:sw=8:tw=78:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -