在网上广为流传着一句话:VIM是编辑器之神,EMACS是神的编辑器.可见VIM的地位之重.我用vim也有差不多两年了,至今仍停留在新手阶段,只会一些vim的基本操作.但是就算只会这些基本操作,我的编码效率也得到显著地提高,所以我想将vim推荐给大家.
我觉得vim的一个很突出的特点就是它拥有多种模式:命令模式,插入模式,可视模式.在每一个模式下,触发相同的按键,它的功能是不同的.这样就使得我们好像凭空多出了几副键盘,原本一些按键需要将手移动到很远的地方才能按到,如今却不用把手移出工作区,只用切换模式就可以了.
vim拥有着很完美的键位分布,它便于记忆而有能够显著地提高输入效率.
单独一个vim可能很单薄,但是vim提供了一个插件接口,我们可以通过插件实现很多功能.
待施工…
set showmode
(option)
display the mode at thr bottom of the window
x
– delete a character
dd
– delete a line
J
– delete a line break
u
– undo
CTRL-R
– redo
a
– append
o
– open a new line
q!
– quit and throw things away
e!
– reloads the original version of the file
wq
– write the file and quit
w
– move the cursor forward one wordb
– move backward to the start of the previous worde
– moves to the next end of a wordge
– moves to the previous end of a word$
– moves the cursor to the end of a line^
– moves to the first non-blank character of the line0
(zero) – moves to the very first non-blank character of the linefx
– searches forward in the line for the single character x.tx
– works like the “fx” command,except it stops one character before the searched character.these four command can be repeated with “;”,"," repeats in the other direction
%
– moves to the matching parenG
– positions you at the end of the filegg
– positions you at the start of the filenumber G
– positions you to the given line numberset number
option
– display a line number in front of every line
set ruler
option
– display the cursor position in the lower right corner of the Vim window
CTRL-u
– scrolls down half a screen of text
CTRL-d
– scrolls the text up half a screen
CTRL-f
– scroll forward by a whole screen
CTRL-b
– scroll backward by a whole screen
zz
– put the cursor line at the middle
more commands: zt
zb
/string
– search for a string
to find the next occurrence of the same string use the “n” command,the “N” command repeats the last search the opposite direction
?
– works like “/” but searches backwards
set ignorecase
option
– don’t care about upper or lowercase in a word
you can use UP DOWN to use the words you searched before
set hlsearch
option
– highlight all matches
set incsearch
option
– display the match for the string while you are still typing it
set nowrapscan
option
– stops the search at the end of the file
we will take about it in usr_027.txt
CTRL-o
– jumps to older positions
hint:o for older
CTRL-i
– jumps back to newer positions
ma
– marks the place under the cursor as mark a
`a
– moves you to the beginning of the line containing the mark
operator-motion.You first type an operator command.For example,“d” is the delete operator.Then you type a motion command like “4l” or “w”.This way you can operate on any text you can move over.
c
– It acts just like the “d” operator,except it leaves you in Insert mode.
s
– change one character
S
– change a whole line
r
– waits for you to type a character,and will replace the character under the cursor with it
.
– repeats the last changev
– enter the visual mode
V
– enter the visual mode and select the whole line
CTRL-v
– start bolockwise mode
when you delete something with the “d”,“x”,or another command,the text is saved.you can paste is back by using the p command.
xp
– swapping two characters
y
p
– put the text in the next line of the cursor
1.n+dd 2.p
set nocompatible
– setting the ‘compatible’ option off,not completely Vi compatible
set backspace=indent,eol,start
– specifies where in Insert mode the backspace is allowed to delete the character in front of the cursor.
indent – the white space at the start of the line
eol – a line break
start – the character before where insert mode started
set autoindent
– makes Vim use the indent of the previous line for a newly created line.
set history=50
– keep 50 commands and 50 search patterns in the history.
set ruelr
– always display the current cursor position in the lower right corner of the Vim window
set showcmd
– display an incomplete command in the lower right corner of the Vim window
for example:map
it tell Vim that F5 key does the job behind it.
type options
command to the option window
set cmdheight=3
– set the number of lines used for messagesedit name.type
– vim will close the current file and open the new onevim one.c two.c three.c
– this command starts vim and tells it that you will be editing three files.vim displays just the first file.
next or 2next
– to edit the next file you use this command
previous
– to go back one file
last
– to move to the very last file in the list
first
– to move back to the first one again
args
– show the list of file.this is short for “arguments”.the output might look like this:one.c [two.c] three.c
args five.c six.c seven.h
– use this command to edit three other files
set backup
– produces a backup file for the file you edit
set backupext=.bak
– if you do not like the fact that the backup files end with ~,you can change the extension
use y
and p
commands
when you want to copy serveral pieces of text from one file to another,having to switch between the files and writing the target file takes a lot of time.to avoid this,copy each piece of text to its own register.for example: fyas
when collecting lines of text into one file,you can use this command:
:write >> logfile
this will write the text of the current file to the end of “logfile”
to append a few lines,select them in visual mode befor typing:write
vim -R file
– to start vim in readonly mode
vim -M file
– every attempt to change the text will fail
file move.c
– change the name of the file you are editing,but don’t write to file
saveas move.c
– editting a new file by using an existing file that contains most of what you need and changes the file’s name
split
– splits the screen into two windows and leaves the cursor in the top one
close
– to close the window
only
– close all windows,except for the current one
split name.type
– opens a second window and starts editing the given filenew
– open a window on a new,empty filethe :split
command can take a number argument.If specified,this will be the height of the new window.for example:
3split alpha.c
CTRL-w +
– increase the size of a window
CTRL-W -
– decrease the size of a window
{height}CTRL-W _
– set the window height to a specified number of lines
vsplit or vsplit name.type
– to make the window appear at the left side
vnew
– like new
moving between window
CTRL-w h
– move to the window on the left
CTRL-w j
– move to the window below
CTRL-w k
– move to the window above
CTRL-w l
– move to the window on the right
CTRL-w t
– move to the TOP window
CTRL-w b
– move to the bottom window
CTRL-w K
– moving window to the top
CTRL-w H
– moving window to the far left
CTRL-w J
– moving window to the bottom
CTRL-w L
– moving window to the far right
qall wall wqall
– some commands
vim -o one.txt two.txt three.txt
– open a window for each file,the -O
argument is used to get vertically split windows
when Vim is already running the :all
command opens a window for each file in the argument list.vertical all
does it with vertical splits
tabedit thatfile
– this will edit the file “thatfile” in a window that occupies the whole Vim window
gt
– switch between tab pages,it means:Goto tab
tab split
– this makes a new page with one window that is editing the same buffer as window we were in
the q{register}
command starts recording keystroks into the register name {register}. The register name must be between a and z.
type your commands
to finish recording, press q
(without any extra character).
you can now execute the macro by typing the command @{register}
writing to an uppercase register name means to append to the register with same letter,bue lowercase.
the “:substitute”(abbreviated version"") command enables you to perform string replacesments on a whole range of lines.the general form of this command is as follows:
:[range]substitute/from/to/[flags]
this command changes the “from” string to the “to” string in the lines specified with [range].
some examples about [range]
some flags you can use
the s
command, and many other : commands, can be applied to a selection of lines. this is called a range.
1,5
– line 1 to 5
54
- address one specific line
.
– the current line
$
– the last line
%
– all the lines
the global
command is one of the more powerful features of Vim. It allows you to find a match for a pattern and execute a command there. The general form is:
[range]global/{pattern}/{command}
Inserting text
you start by pressing CTRL-V
to enter visual block mode. Now you move the cursor to define your block. Next you type I
to enter Insert mode, followed by the text to insert. As you type, the text appears on the first line only. After you press ESC
to end the insert, the text will magically be inserted in the rest of the lines contained in the visual selection.tips:if the block spans short lines that do not extend into the block, the text is not inserted in that line.
the A
command works the same way, except that is appends after the right side of the block. And it does insert text in a short line
the Visual block c
command deletes the block and then throws you into insert mode to enable you to type in a string. The string will be inserted in each lien in the block.tips:just like with I
the short line is not changed
other commands that change the characters in the block:
~
swap case (a -> A and A -> a)
U
makt uppercase (a -> A and A -> A)
u
make lowercase (a -> a and A -> a)
to fill the whole block with one character, use the r
command.
the command >
shifts the selected text to the right one shift amount
the shift amount is specified with the shiftwidth
option. to change is to use 4 spaces:
set shiftwidth=4
the command <
command removes one shift amount of whitespace at the left edge of the block.
the J
command joins all selected lines together into one line.
when you are writing an e-mail message, you may want to include another file. This can be done with the :read {filename}
command.
the read
command accepts a range. the file will be put below the last line number of this range. thus $r patch
appends the file “patch” at the end of the file. if you want to read the file above, this can be done with the line number zero.
to write a range of lines to a file, the write
command can be used. without a range it writes the whole file. with a range only the specified lines are written
write >>collection
– the >>
tells vim the “collection” file is not to be written as a new file, but the line must be appended at the end.gu+motion
gU+motion
g~+motion
guu
gUU
g~~
– make the operator works on lines
the command !{motion}{program}
takes a block of text and filters it through an external program.
use the \<
item to match the start of a word. use \>
to match the end of a word.
replace in several files – use tag
g CTRL-g
– count words
with visual mode, select the text you want to count words in. then type g CTRL-g
.