Vim自带帮助文档笔记
Vim自带帮助文档笔记
本文是笔者学习 Vim 自带帮助文档的笔记,主要是为了更深入地学习 Vim 的使用方法。本笔记非常简洁,只总结了对应章节的关键命令和概念。
Vim 自带帮助文档是 Vim 的一个很好的学习资源,它包含了 Vim 的所有功能和使用方法,是学习 Vim 的最好的参考资料之一。强烈推荐想要深入学习 Vim 的人阅读。
本文主要包括用户手册部分的笔记,且只包括了部分章节的笔记,因为我目前只读了这么多(但也受益匪浅了),后续会继续更新。
本文撰写时使用的 Vim 版本为 7.x,Vim 的版本可能会影响到帮助文档的内容,所以请注意 Vim 的版本。
usr_01.txt About the manuals
01.1 Two manuals
- The User manual
- The Reference manual
JUMPING AROUND: CTRL-], CTRL-O, CTRL-T
01.2 Vim installed
$VIMRUNTIME/vimrc_example.vim:set compatible?scriptnames
01.3 Using the Vim tutor
vimtutorvimtutor zh
1
2
vim --clean -c 'e $VIMRUNTIME/tutor/tutor.zh' -c 'w! TUTORCOPY' -c 'q'
vim --clean TUTORCOPY
01.4 Copyright
usr_02.txt The first steps in Vim
02.1 Running Vim for the First Time
vim file.txt
02.2 Inserting text
i, :set showmode, <Esc>
02.3 Moving around
h, j, k, l
02.4 Deleting characters
x, dd, J
02.5 Undo and Redo
u, U, CTRL-R
02.6 Other editing commands
a, o, O, 3a!
02.7 Getting out
ZZ, :q!, e!
02.8 Finding help
:help {subject}:help x:help deleting:help index:help CTRL-A:help i_CTRL-A:help -t:help 'number':help i_<Up>:help E37
help-summary
- Help:
:help som<CTRL-D>,:help som<Tab>,:help :h - Links in bars:
:help pattern.txt,CTRL-],CTRL-O,CTRL-T - Options:
:help 'list',:help options.txt,:help go-<letter> - Normal:
:help gt - Insetrt:
:help i_CTRL-W - Visual:
:help v_o - Command:
:help c_% - Ex-commands:
:help :s - Debugging:
:help >cont - Key combinations:
:help i_CTRL-X,:help CTRL-W,:help g_CTRL-A - Regexp:
:help /\+,:help pattern.txt - Registers:
:help ": - Script:
:help eval.txt,:help expr-!,:help funtion-list,:help append() - Mappings:
:help map.txt,:help mapmode-i,:help :map-local,:help map-bar - Command definitions:
:help command-bar - Window management:
:help CTRL-W_p,:help windows.txt - Helpgrep:
:helpgrep topic,:cnext,:copen - User manual:
:help usr_toc.txt,:help 10.1,:help usr_24.txt - Highlighting groups:
:help hl-WarningMsg - Syntax highlighting:
:help :syn-conceal - Quickfix and Location list: starts with
:cand:l - Autocommand events:
:help BufWinLeave,:help autocommand-events - Command-line switches:
:help -f - Optional features:
:help +conceal - Documentation for included filetype specific functionality:
:help ft-c-syntax,:help ft-php-omni,:help ft-tex-plugin - Error and Warning:
:help E297,:help W10,:help E128
usr_03.txt Moving around
03.1 Word movement
w,b,e,geW,B,E,gE
03.2 Moving to the start or end of a line
^, 0, $, 2$
03.3 Moving to a character
fh,3fh,Fh,3Fhth,3th,Th,3Th
03.4 Matching a parenthesis
%, :set matchpairs?
03.5 Moving to a specific line
3G,3gg50%H(Home),M(Middle),L(Last)
03.6 Telling where you are
CTRL-G:set number:set ruler
03.7 Scrolling around
- A whole screen:
CTRL-F,CTRL-B - Half a screen:
CTRL-U,CTRL-D - One line:
CTRL-E,CTRL-Y zz,zt,zb:set scrolloff=5
03.8 Simple searches
/,?,n,3n,N,3N- Ignorecase:
:set ignorecase - Hisory:
/o<Up> - Whole words:
*,#,3*,/\<the\> - Not Whole words:
g*,g#,/the - High light:
:set hlsearch,:set nohlsearch,:nohlsearch - Tuning searches:
:set incsearch,:set wrapscan - Vimrc:
scriptnames,:edit ~/.vimrc,Go:set hlsearch<Esc>,ZZ
03.9 Simple search patterns
^, $, ., \.
03.10 Using marks
` `,'',CTRL-O,CTRL-I(<Tab>),:jumps- Named marks:
ms,'s, ``s,:marks`
usr_04.txt Making small changes
04.1 Operators and motions
d4w, d2e, d$, d4l
04.2 Changing text
c2wbe<Esc>- More changes:
cc,c$ - Shortcuts:
x(dl),X(dh),D(d$),C(c$),s(cl),S(cc) - Replace:
r,5rx,r<Enter>
04.3 Repeating a change
.
1
2
3
4
5
6
/four<Enter>
cwfive<Esc>
n
.
n
.
04.4 Visual mode
v- Selecting lines:
V - Selecting blocks:
CTRL-V - Other side:
o,O
04.5 Moving text
ddjp,dwelp- More on putting:
P,3p - Swapping two characters:
xp
04.6 Copying text
y, yw, ye, yy(Y), y$
04.7 Using the clipboard
- Gui: Edit->Copy/Paste/Cut, Popup->Copy/Paste/Cut
- Not gui:
"*yy,"*p
04.8 Text objects
diw(excluding white space), daw(delete a word), cas(change a sentence) , cis(change inner sentence)
04.9 Replace mode
R(replace mode), R<Insert>, Raaa<BS>
04.10 Conclusion
x, X, D, dw, db, diw, daw, dG, dgg, c, y, ~, I, A
usr_05.txt Set your settings
05.1 The vimrc file
:edit $MYVIMRC, :version, set incsearch
05.2 The example vimrc file explained
$VIMRUNTIME/vimrc_example.vim:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
set nocompatible
set backspace=indent,eol,start
set autoindent
if has("vms")
set nobackup
else
set backup
endif
set history=50
set ruler
set showcmd
set incsearch
map Q gq
vnoremap _g y:exe "grep /" . escape(@", '\\/') . "/ *.c *.h"<CR>
if $t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
filetype plugin indent on
autocmd FileType text setlocal textwidth=78
autocmd BurReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ else "normal! g`\"" |
\ endif
05.3 Simple mappings
:map <F5> i{<Esc>ea}<Esc>:map \p i(<Esc>ea)<Esc>:map
05.4 Adding a package
packadd! matchit(in vimrc), :help matchit
1
2
3
mkdir -p ~/.vim/pack/fancy
cd ~/.vim/pack/fancy
unzip /tmp/fancy.zip
05.5 Adding a plugin
Global plugins
Getting:
- $VIMRUNTIME/plugin
- $VIMRUNTIME/macros
- Download from the net
- Vim maillist
- Write yourself
Using: cp /tmp/yourplugin.vim ~/.vim/plugin
Filetype plugins
Getting:
Using: filetype plugin on
1
2
3
mv thefile ~/.vim/ftplugin/stuff.vim ||
~/.vim/ftplugin/stuff_def.vim ||
~/.vim/ftplugin/stuff/header.vim
05.6 Adding a help file
1
2
3
4
:!cp /tmp/doit.txt ~/.vim/doc
:helptags ~/.vim/doc
:help doit
:help local-additions
05.7 The option window
:options
05.8 Often used options
:set iskeyword&:set nowrap:set sidescroll=10:set whichwrap=b,s,<,>,[,]:set list:set listchars=tab:>-,trail:-:set iskeyword+=-:set cmdheight=3
usr_06.txt Using syntax highlighting
06.1 Switching it on
:syntax enable
1
2
3
if &t_Co >1 "only when the terminal supports colors
syntax enable
endif
06.2 No or wrong colors?
No colors:
- Terminal not support colors
- Vim doesn’t know the terminal support colors:
TERM=xterm-color; export TERM - Filetype is not recognized:
:set filetype=fortran($VIMRUNTIME/syntax) - No highlighting for your filetype
Wrong colors:
- The colored text is hard to read:
:set backgound=dark(before:syntax enable):syntax reset(after setting ‘background’) - when scrolling bottom to top:
CTRL-L
06.3 Different colors
:colorscheme evening($VIMRUNTIME/colors)
Write your color scheme:
cp $VIMRUNTIME/colors/morning.vim ~/.vim/colors/mine.vim- Edit the color scheme file
- Let Vim always use your:
colorscheme mine
:runtime syntax/colortest.vim
06.4 With colors or without colors
:syntax clear, :syntax off, :syntax manual, :set syntax=ON
06.5 Printing with colors
:TOhtml(:source $VIMRUNTIME/syntax/2html.vimif needed):write main.c.htmlfirefox main.c.htmlCTRL-P
06.6 Further reading
Your own syntax highlighted: :h usr_44.txt
All the details: :h syntax
usr_07.txt Editing more than one file
07.1 Edit another file
:write:edit foo.txt, :edit! foo.txt, :hide edit foo.txt
07.2 A list of files
vim one.c two.c three.c,:next,:next!,:wnext(:write :next)- Where am I:
:args - Moving to other arguments:
:previous,:wprevious,:last,:first,:2next - Auto writing:
:set autowrite - Editing another list of files:
:args five.c six.c seven.h,:args *.txt - Quit without edit the last file:
:q:q
07.3 Jumping from file to file
CTRL-^, '"(left the file), '.(made the last change)
1
2
3
4
:edit foo.txt
50%mF
:edit bar.txt
GmB
marks MCP
07.4 Backup files
:set backup, :set backupext=.bak Keeping the original file: :set patchmode=.orig
07.5 Copy text between files
1
2
3
4
5
6
:edit thisfile
/This
vjjjj$y
:edit otherfile
/There
p
Using registers: "fyas, "l3Y, CTRL-Vjjww"by, "fp, "wdaw
Appending to a file: Vjjj:write >> logfile
07.6 Viewing a file
vim -R file(view file) vim -M file
1
2
:set modifiable
:set write
07.7 Changing the file name
:edit copy.c:saveas move.c:file move.c
usr_08.txt Splitting windows
08.1 Split a window
:split,CTRL-W w- Close the window:
:close - Close all other window:
:only
08.2 Split a window on another file
:split two.c, :new
08.3 Window size
:3split alpha.c, CTRL-W +, CTRL-W -, 4 CTRL-W +, {height}CTRL-W _
08.4 Vertical splits
:vsplit,:vsplit two.c,:vertical new- Moving between windows:
CTRL-W h,CTRL-W j,CTRL-W k,CTRL-W l, CTRL-W t,CTRL-W b
08.5 Moving windows
CTRL-W H, CTRL-W J, CTRL-W K, CTRL-W L
08.6 Commands for all windows
:qall,:wall,:wqall,:qall!- Opening a window for all arguments:
vim -o one.txt two.txt three.txtvim -O one.txt two.txt three.txt:all,:vertical all
08.7 Viewing differences with vimdiff
vimdiff main.c~ main.c- The fold column:
zo(open),zc(close) Diffing in Vim:
1 2
:edit main.c :vertical diffsplit main.c~
or:
1 2
:edit main.c :vertical diffpatch main.c.diff
- Scroll binding:
:set noscrollbind - Jumping to changes:
]c,[c,3]c - Removing changes:
:diffupdate(update highlight),dp(diff put), do(diff obtain)
08.8 Various
set laststatus=1,:tag,:stag(split tag),CTRL-W CTRL-^,set splitbelow,set splitright1 2 3 4 5 6
:leftabove {cmd} "left or above the current window :aboveleft {cmd} "idem :rightbelow {cmd} "right or below the current window :belowright {cmd} "idem :topleft {cmd} "at the top or left of the Vim window :botright {cmd} "at the bottom or right of the Vim window
08.9 Tab pages
:tabedit thatfile, gt, :tab split, :tab help gt
usr_10.txt Making big changes
10.1 Record and playback commands
q{register}{many command}q@{register}
Move and execute: @@
Using registers:
1
2
3
4
5
6
7
G
o<Esc>
"np
{edits}
0
"ny$
dd
Appending to a register: qC/word<Enter>q, "aY, "AY
10.2 Substitution
1
2
3
:[range]substitute/from/to/[flags]
:%s/Professor/Teacher/g
:s+one/two+one or two+
10.3 Command ranges
1
2
3
4
:1,5s/this/that/g
:54s/President/Fool/
:.write otherfile
:.,$s/yes/no/
Using a pattern in a range::?^Chapter?,/^Chapter/s=grey=gray=g
Add and subtract:
1
2
3
/Chapter/-1
/Chapter/+2
:.+3,$-5
Using marks: :'t, 'b
Visual mode and ranges: vjj:, :'>,$
A number of lines: 5:
10.4 The global command
1
2
:[range]global/{pattern}/{command}
:g+//+s/foobar/barfoo/g
10.5 Visual block mode
CTRL-V $- Inserting text:
CTRL-VjjjlllI{string}<Esc>(short line no text was inserted),CTRL-V$A{string}<Esc>(each line text was appended) - Changing text:
CTRL-Vjjjlllc_LONG_<Esc>CTRL-VjjjlllC_LONG_<Esc>~swap case (a -> A and A -> a)Umake uppercase (a -> A and A -> A)umake lowercase (a -> a and A -> a)
- Filling with a character:
CTRL-Vjjjlllrx - Shifting:
CTRL-Vjjj>CTRL-Vjjj<:set shiftwidth=4
- Joining lines:
J,vjjjlllJ,VjjjJ
10.6 Reading and writing part of a file
:$read patch.c,:0read patch.c- Writing a range of lines:
:.,$write temp.c,:.,$write! temp.c - Appending to a file:
:.write a.c,:.write >>a.c
10.7 Formatting text
:set textwidth=72, gqap, gggqG, gqj, .
10.8 Changing case
gUw, guw, g~w, gugu, guu, g~~
10.9 Using an external program
!{motion}{program},!5Gsort<Enter>,!!date<Enter>- When it doesn’t work:
- Reading command output:
:read !ls,:0read !date -u - Writing text to a command:
:write !wc - Redrawing the screen:
CTRL-L
usr_11.txt Recovering from a crash
11.1 Basic recovery
1
2
3
4
vim -r help.txt
:write help.txt.recovered
:edit #
:diffsp help.txt
set updatecount?set updatetime?vim -r ""
11.2 Where is the swap file?
vim -r- Using a specific swap file:
vim -r .help.txt.swo set directory?set shell?
11.3 Crashed or not?
- Unreadable swap file:
- What to do:
:recover,set shortmess+=A
11.4 Further reading
- |swap-file|
- |:preserve|
- |:swapname|
- ‘updatecount’
- ‘updatetime’
- ‘swapsync’
- ‘directory’
- ‘maxmem’
- ‘maxmemtot’
usr_12.txt Clever tricks
12.1 Replace a word
:%s/\<four\>/4/gc
Replacing in several files:
1
2
3
4
5
6
vim *.cpp
qq
:%s/\<GetTesp\>/GetAnswer/g
:wnext
q
@q
:%s/\<GetResp\>/GetAnswer/ge
12.2 Change “Last, First” to “First Last”
:%s/\([^,]*\), \(.*\)/\2 \1/
12.3 Sort a list
1
2
3
/^OBJS
j
:.,/^$/-1!sort
12.4 Reverse line order
:global/^/m 0:'t+1,.g/^/m 't
12.5 Count words
g CTRL-G, CTRL-VjjjgCTRL-G
12.6 Find a man page
K
1
2
3
4
:runtime! ftplugin/man.vim
:Man csh
:Man 3 echo
\K
12.7 Trim blanks
:%s/\s\+$///
12.8 Find where a word is used
vim `grep -l frame_counter *.c`
Finding each line:
1
2
3
4
5
:grep error_string *.c
:cnext
:cprev
:clist
:set 'grepprg'