Use Vim (Note for vimbook-OPL.pdf)
Learn use vim as my main text editor 總體上感覺: 看到10章之後,速度慢慢就上去了,可能是自己慢慢熟悉命令了。 這個Note僅僅是我在練習時順便整理的。
The Tutorial
Get start
I just think vim is a very good productivity tools.
Did you think so?
尝试使用中文,感觉这个输入法的速度都变快了,有没有?
字体比较小,怎样把字体变大呢?(cmd=)
背景也能不能设置为黑色的呢?(GUI 下 theme=evening)
都需要自己探索,学习。
1.Vim:Commands
Moving Around
hjkl Move cursor ### Deleting Characters
x: Delete charcter under score your cursor ### Changing modals
esc: Change modal
i inserts a character before the character under the cursor
v: Visual modal (Select multi lines text) ### Undo and Redo
u: Undo
U: Undo line
Ctrl r twice: Undoes the undo
Getting Out
ZZ: Writes files and exits 保存并关闭文件
:q! quit-and-throw-things-away 退出不保存
:w 写入文件不退出
:q 退出
Other Editing Commands
a insert text after the cursor
dd delete a line
o a new line below the cursor,then enter insert mode.
O a new line above the cursor,then enter insert mode.
:help display help
some text enclosed in vertical bars (etc: |tutor),then press
ctrl ] Jump to tag(Jump to the location of the tag given by the word under the cursor)
ctrl t pop tag(Back to the location of the tag)
get help in NORMAL mode on subject
etc.
:help x
:help deleting
:help ctrl-a
:help ctrl-h
get help in INSERT mode on subject
prefix key i_
:help i_ctrl-h
special key such as Arrow key
:help <Up> get help on <Up> arrow key
other help prefix key , see note
**Using a Count to Edit Faster**
If you want to type aaaaaaaaa
you can
9ia<Esc> then it will insert aaaaaaaaa
or
9aa<Esc> append aaaaaaaaa to after the cursor
If you want move up 10 lines
you can
10k
to move down 10 lines
10j
to move cursor left 5 characters
5h
to move cursor right 5 characters
5l
2.Editing a Little Faster
Word Movement
w forward 1 word
b backward 1 word
3w forward 3 words
4b backward 4 words
Moving to the Start or End of a line
$ move to the end of line
^ move to the begin of line
2$ move to the end of 2 line
0 move to the begin of line
Searching Along a Single Line
fx forward search single character x
Fx backward search single character x
2fx twice search single character x
tx cursor stop in searched character x begin
Tx cursor stop in searched character x after
Moving to a Specific Line
gg Go to file start
G Go to file end
Telling Where Are in a File
:set number display line number
:set nonumber display line number off
Where am I?
ctrl+g display a status line that indicates where you are in the file.
Scrolling Up and Down
ctrl+u scroll up half a screen
ctrl+d scroll down half a screen
Deleting Text
dw delete a word
dd delete one line
d2w delete two word
d3d delete three line
d$ delete from cursor to end of line
d0 or d^ delete from cursor to begin of line
3d2w delete 2 words repeat 3 times for a total of 6 words
Chaging Text
cw change a word
c3w change 3 words
cc change a line
Joining Lines
J join the current line with the next one
3J join 3 lines to one line
Replacing Characters
rx replaces the character under the cursor with x
5ra replaces the character under the cursor with aaaaa
Changin Case
~ change the character under the cursor with lowercase or uppercase
Keyboard Macros
q character command records keystrokes into the register named character. (The char-
acter must be between a and z.)
---
qa Start recording a macro in register a.
^ Move to the beginning of the line.
i#define<Esc> Insert the string #define at the beginning of the line.
j go to next line
q Stop recording the macro.
---
@a use macro
10@a use macro 10 times
3.Searching
Simple Searches
/string search string if you want to search forward use /<Enter>
/<Up>or<Down> use history search string
Searching Options
Highlighting
:set hlsearch set highlighting search
:set nohlsearch no highlighting search
:nohlsearch clear the current highlighting
Incremental Searches
:set incsearch
:set noincsearch
n next matched string ### Searching Backward
?string search string backward
n last matched string
Changing Direction
n normal next matched string
N reverse search between ? and /
Basic Regular Expressions
The Beginning(^) and End ($) of a line
/str$ search line end with str
/^str search line begin with str
Match Any Single Character(.)
etc.
/a.p apple adplus result will show like left words
Regular Expression Summary
x The literal character x
^ Start of line
$ End of line
. A single character
\character Turns off the special meaning of many characters, gives special meaning to a few others
4. Text Blocks and Multiple Files
Cut,Paste and Copy
Use p (put) command
dd -> p You can delete a line ,then put into your cursor in what line you want
d$ -> p You can delete from cursor to end of line , then put into where you want to put under your cursor
d2w -> p delete 2 words then put to another place
Character Twiddling
etc.
if you want to type 'the' then you type 'teh' you can put your cursor on 'e' then
xp x->delete the character p-> paste character after the cursor
More on “Putting”
p places the text after the cursor
P places the text before the cursor
3p paste 3 times
Marks
:marks Display all marks
ma make a mark named a
mb make a mark named b
`a jump to mark a
`b jump to mark b
d'a from cursor delete mark a content
Yanking
yank (Most other editors call "copy" operation)
yy yank a line then use p to paste
3y yank 3 lines
Which File Am I On?
:args
Edit multi files
you can use vim to open a.txt b.txt
vim a.txt b.txt
then
:next Next file
:previous Previous file
:wnext Write then go to next file
:wprevious Write then go to previous file
:rewind Edit first file
:last Edit last file
ctrl+^(6) switch to alternate file (You can use :next to jump other file then two files are alternate)
5.Windows
Opening a New Window
:split split to up/down two window
ctrl+w w move cursor to alternate window
ctrl+w j move cursor to down window
ctrl+w k move cursor to up window
Opening Another Window with Another File
:split file etc. :split YQRealLink.m
:split +/str file search str and open file etc. :split +/define YQRealLink.m search define in YQRealLink.m
:20 split file Open file set window size 20 rows
Split Summary
:count split +command file
count The size of window
+command An initial command
file The name of file to edit(You can use autocompleted to fix filename)
The :new Command
open a new window without content of file(This is a new file)
Changing Window Size
ctrl-w+ etc. ctrl+w 10 + increase 10 rows to current window
ctrl-w- etc. ctrl+w 20 - decrease 20 rows to current window
. Repeat last command
ctrl-w= etc. ctrl+w = make all window size equal
(count)ctrl-w_ makes current window (count) lines high
Buffers
:buffers show current buffer list
stats explain
- Inactive
h Hidden
% Current
# Alternate buffer
+ File has been modified
Selecting a Buffer
:buffer number etc. :buffer 2 Select second buffer file
or
:buffer file etc. :buffer file2.txt
:sbuffer number(or file) splits the window and starts editing the buffer
Annotations: P51 Other buffer-related commands
6.Basic Visual Mode
Entering Visual Mode(v)
:help v_d describes what the d command does in visual mode
v visual block
V visual line
You can use <Esc> to leave all mode ### Editing with Visual Mode
d delete highlighted text
D delete highlighted line
y places the highlighted text into a register
Y places the highlighted line into a register
c change text
C change line
J join all the highlighted lines into one long line
If you want to join the lines without adding spaces,use the
gJ command
Indent
< shiftwidth(shift tab)
> width(tab)
= indents the text
ctrl ] jump to definition of the function highlighted
K man highlighted text ### Visual Block Mode
:help v_b_r get help on the visual block r command
ctrl-v Enter visual block mode
then
I insert after blocked text
and you can use hjkl to select block
c delete string then insert on each line in the block
C delete the character from cursor to end of line then insert new text to block
$ extend block to the end of line
A add text to the end of each line #### Replacing
r applies all the selected characters with a single character #### Shifting
> right tab
< left tab
one
two
three
7. Commands for Programmers
Syntax Coloring
:syntax on #### Syntax Coloring Problems
:set background? get current background setting
:set background=light set background to light
:set background=dark set background to dark
:set filetype=c(Or java ) tell Vim types of syntax highlighting to use
:set shiftwidth=4 change the size of the shift width ### Automatic Indentation
set: cindent
set: smartindent
set: autoindent
to set all open c,cpp file use cindent to .vimrc file
:filetype on
:autocmd FileType c,cpp :set cindent
= using Vim's internal formatting program
Locating Items in a Program
[ctrl-i,]ctrl-i
8. Basic Abbreviations,Keyboard Mapping,and Initialization Files
Abbreviations
:abbreviate ad advertisement when you type ad<space> or <tab> then will appear advertisement
see all abbreviate use
:abbreviate
see you vimrc file
:version
write all your settings to a file
:mkvimrc file
My .vimrc is locate on ~/.vimrc
9.Basic Command-Mode Commands
: Enter command line mode
:p(rint) Just print current line text
:1,5 print Print 1-5 lines content
:5 print Print fifth line text
:1,$ print Print all content
ma mark selected content to a
:'a print print a mark content
:shell Enter shell mode (As Terminal) type exit to back to vim
10.Basic GUI Usage
11.Dealling with Text Files
Automatic Text Wrapping
Text Formatting Command
:range center width etc. :1,5 center 30 to center a range of lines(width default is 80)
:range right width
:range left margin
The formatoptions Option
:set formatoptions=cq
c Automatically wrap comment. Insert the comment leader automatically.
q Allow gq to format comments. #### File Formats
:set fileformats=unix,dos set file fromat to unix,dos
:set fileformat? see current file format
:set fileformat=unix set file format to unix #### Changing How the Last Line Ends
:set endofline Last line ends in <EOL>
:set noendofline Last line does not have an <EOL> #### Troff-Related Movement
) moves forward one sentence
( backward sentence
} moves forward one paragraph (most as one blank line as division)
{ backward paragraph ### Section Moving
[[ move a section backward
]] move a section forward
12.Automatic Completion
ctrl-p backward search word to complete
Many options you can customize in advanced chapter
ctrl-n forward search word to complete ### Customization of Completion
:set ignorecase tells the editor to tray all words regardless of case ### Specifying a Dictionary
:set dictionary=file,file,...
etc.
single dictionary
:set dictionary=/Users/yaqinking/Documents/dict/anime_dic.txt
multi dictionary
:set dictionary=/usr/dict/words,/usr/doc/words
!You can specify your input method dict to you vim dict to auto complete your type exp.
Line mode
ctrl-x ctrl-l line completion ### Adjusting the screen
ctrl-x ctrl-y scroll up one line
ctrl-x ctrl-e scroll down one line
14.File Recovery and Command-Line Arguments
:set backup open backup
:set backupext=string add extension type to backup file
etc.
:set backupext=.bak
It will be backup to file.txt.bak
:set nobackup close backup
:set backupdir=~/tmp/
$vim -r file
recovery file from swap file
:swapname show what swap file your current use
If you don't want to use swap file (Default swap file is on :set swapfile)
:set noswapfile
Controlling When the Swap File Is Writen
:set updatetime=23000 23seconds(milliseconds)
:set updatecount=400 the number of character 400 you typed it will be writen to swap file
15.Miscellaneous Commands
ga(or :ascii) prints the number of the chracter under the cursor.
5gg(or 5G) go to 5 line
30gg(or 30G) go to 30 line
ctrl-l Screen Redraw
z10 resize current window to 10 lines
:intro View introductory flash screen
16.Cookbook
Chracter Twiddling
teh->the xp ### Replacing One Word with Another Using One Command
If you want to replace "Oreimou" to "Oreimou Radio",
If "Oreimou" line are 100-110,
:100,110s/Oreimou/Oreimou Radio/g
:100,110 100 line to 110 line
s :substitute
Oreimou old word
Oreimou Radio new word
g global change
If you want to change all use 1,$
1 first line
$ last line
Interactively Replacing One Word With Another
1. /Radio<Enter> Search
2. cw<Esc> change word
3. n next word Radio
4. cw<Esc> change word
5. repeat 3-4
Moving Text
If you want to moving some line
1. V select line
2. ma mark as a
3. move cursor to where you want put line
4. d'a delete marked a content(Put it to pasteboard)
5. p paste to cursor current line
Moving paragraph
1. V select start line
2. ma mark as a
3. } move to paragraph end
4. mb mark as b
5. move cursor to where you want put line
6. :'a,'b move . move
Copying a Block of Text from One File to Another
0. :split file split window
1. V select line (etc. select 2 lines) then
2. 2y yank 2lines text
3. ctrl-w-w(or ctrl-w-p) switch to another window
4. p paste
or
1. ma mark as a
2. y'a yank 'a
3. ctrl-w-w switch to another window
4. p paste ### Drawing Comment Boxes
put into .vimrc
:ab #b /****************************************
:ab #e <Space>****************************************/
When you want to type #b just type #bb then delete second b it will not expanded.
Another better option use external program like boxes(see http://www.vim.org)
Oops, I Left the File Write-Protected
:w filename save to another file
17.Topics Not Covered
File Encoding
:set fe=encoding set file encoding
:set fe=twiwan(or prc) traditional Chinese (or Simplified Chinese)
:set fileencoding=encoding long format
:set fe=japan Japanese Ecoding
The Details
18.Complete Basic Editing
e move to end of a word
ge backward move to end of a word
3ge 3 backward move to endo af a word
^ move to first non-black character on the line
0 move to beginning of the line
Repeating Single-Chracter Searches
fx searches fo the first x (to repeat search use ; to reverse use , )
Moving Lines up and down
- moves up to the first non-blank character line
= moves down ### Jumping Around
<count>- move <count> up line
<count>G go to <count> line
ctrl-o Jump to previous location
<TAB> Jump to next location ### Where Am I,in Detail
ctrl-g Display summary information at the bottom of the screen
1ctrl-g add path to show
2ctrl-g add buffer to show
gctrl-g another tpe of status information
Scrolling Up
ctrl-u scrolls up half a screen
:set scroll=10 set ctrl-u commands scrolls up number
2ctrl-u scrolls up 2 line
5ctrl-y scrolls up 5 line
ctrl-b scrolls up an entire screen
Scrolling Down
ctrl-d move down The amount is controlled by the 'scroll' option
ctrl-e move down one line
ctrl-f move down one screen of data (also <PageDown> or <S-Down> Shift-ArrowDown)
Define How Much to Scroll
:set scrolljump=5 ### Delete to the End of the Line
<count>D delete line (or <count> amount lines)
C change line text current cursor to end of line then enter INSERT mode
Mmmm, basic command has d and d two forms, and you can add <count> to dozens do.
Usally,d -> delete current word or character D -> delete cursor to end of line
3d -> delete 3 lines
a append at begin line
A append at end of line
gi insert at begin line (same as a)
Arithmetic
ctrl-a increase number
ctrl-x decrease number
Jpining Lines with Spaces
J join the current line with the next one
gJ join line without spaces
3gJ join 3 line without spaces
Replace Mode
r replace a chracter
R enter replace mode
Chaning Case
<count>~ change a chracter case
<count>g~ change current line and next line text case
<count>g~~ change line case
<count>gUU change entire line case
19.Advanced Searching Using Regular Expressions
Searching Options
Case Sensitivity
:set ignorecase turn on ignorecase
:set noignorecase turn off
:set smartcase WORD will be matches WORD
Instant Word Searches
* search under cursor word
g* go to searched words under cursor word
g# reverse
Search Offsets
/nisekoi/2 go to searched nisekoi next 2 line position
/nisekoi/b2(or s2 start) moves the cursor to the beginning of the match
/nisekoi/e moves the cursor onto the last character of the match