Vim自带帮助文档笔记

Posted by wsxq2 on 2018-03-22
TAGS:  Vim

本文最后一次编辑时间:2019-04-18 22:59:42 +0800

注:本文主要针对Linux操作系统。

usr_01.txt About the manuals

01.1 Two manuals

  1. The User manual
  2. 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

vimtutor vimtutor zh

1
2
vim --clean -c 'e $VIMRUNTIME/tutor/tutor.zh' -c 'w! TUTORCOPY' -c 'q'
vim --clean TUTORCOPY

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

  1. Help: :help som<CTRL-D>, :help som<Tab>, :help :h
  2. Links in bars: :help pattern.txt, CTRL-], CTRL-O, CTRL-T
  3. Options: :help 'list', :help options.txt, :help go-<letter>
  4. Normal: :help gt
  5. Insetrt: :help i_CTRL-W
  6. Visual: :help v_o
  7. Command: :help c_%
  8. Ex-commands: :help :s
  9. Debugging: :help >cont
  10. Key combinations: :help i_CTRL-X, :help CTRL-W, :help g_CTRL-A
  11. Regexp: :help /\+, :help pattern.txt
  12. Registers: :help ":
  13. Script: :help eval.txt, :help expr-!, :help funtion-list, :help append()
  14. Mappings: :help map.txt, :help mapmode-i, :help :map-local, :help map-bar
  15. Command definitions: :help command-bar
  16. Window management: :help CTRL-W_p, :help windows.txt
  17. Helpgrep: :helpgrep topic, :cnext, :copen
  18. User manual: :help usr_toc.txt, :help 10.1, :help usr_24.txt
  19. Highlighting groups: :help hl-WarningMsg
  20. Syntax highlighting: :help :syn-conceal
  21. Quickfix and Location list: starts with :c and :l
  22. Autocommand events: :help BufWinLeave, :help autocommand-events
  23. Command-line switches: :help -f
  24. Optional features: :help +conceal
  25. Documentation for included filetype specific functionality: :help ft-c-syntax, :help ft-php-omni, :help ft-tex-plugin
  26. Error and Warning: :help E297, :help W10, :help E128

usr_03.txt Moving around

03.1 Word movement

w, b, e, ge W, 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, 3Fh th, 3th, Th, 3Th

03.4 Matching a parenthesis

%, :set matchpairs?

03.5 Moving to a specific line

3G, 3gg 50% H(Home), M(Middle), L(Last)

03.6 Telling where you are

  1. CTRL-G
  2. :set number
  3. :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:

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:

  1. $VIMRUNTIME/macros
  2. Download from the net
  3. Vim maillist
  4. 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:

  1. Terminal not support colors
  2. Vim doesn’t know the terminal support colors: TERM=xterm-color; export TERM
  3. Filetype is not recognized: :set filetype=fortran($VIMRUNTIME/syntax)
  4. No highlighting for your filetype

Wrong colors:

  1. The colored text is hard to read: :set backgound=dark(before :syntax enable) :syntax reset(after setting ‘background’)
  2. when scrolling bottom to top: CTRL-L

06.3 Different colors

:colorscheme evening($VIMRUNTIME/colors) Write your color scheme:

  1. cp $VIMRUNTIME/colors/morning.vim ~/.vim/colors/mine.vim
  2. Edit the color scheme file
  3. 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

  1. :TOhtml(:source $VIMRUNTIME/syntax/2html.vim if needed)
  2. :write main.c.html
  3. firefox main.c.html
  4. CTRL-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.txt vim -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 splitright

1
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

  1. q{register}
  2. {many command}
  3. q
  4. @{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: /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) U make uppercase (a -> A and A -> A) u make 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

  1. |swap-file|
  2. |:preserve|
  3. |:swapname|
  4. ‘updatecount’
  5. ‘updatetime’
  6. ‘swapsync’
  7. ‘directory’
  8. ‘maxmem’
  9. ‘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'

20.1 Command line editing

20.2 Command line abbreviations

20.3 Command line completion

20.4 Command line history

20.5 Command line window