vi操作实用命令

   由于在往vim中粘贴含有 // 注释行 的C代码时,会出现后续所有行都被加上注释 // . 以前也出现过,也是将就着用着。今天总算想到上网搜下找到解决方法。并汇总了一些为自己觉得比较常用的实用命令。

 


GOOGLE:  vim set paste and how to restore;   vim paste c code problem with comment line auto complete;

http://computersplace.net/drive-data-recovery/backup-recovery/vivim-avoid-tabs-making-copypaste.html

So before pasting anything into vim one should always :set paste .

If you find that VIM's auto indenting makes pasting in text, well, suck then enter this: :set paste. and. :set nopaste. to reverse it.


http://vim.1045645.n5.nabble.com/Anoying-autocomplete-functionality-td3248619.html


http://stackoverflow.com/questions/8124312/make-vim-function-return-text-without-indent

=====================================

http://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim

The :... g and :... v commands bear some explanation as they are incredibly powerful. :... g is a prefix for "globally" applying a subsequent command to all lines which match a pattern (regular expression) while :... v applies such a command to all lines which do NOT match the given pattern ("v" from "conVerse"). As with other ex commands these can be prefixed by addressing/range references. Thus :.,+21g/foo/d means "delete any lines containing the string "foo" from the current one through the next 21 lines" while :.,$v/bar/d means "from here to the end of the file, delete any lines which DON'T contain the string "bar."

==> WYH测试结果:   :.,$v/fPIC/d  等效于  : .,$g!/fPIC/d
    但是,不能写成: .,$/fPIC/!d 因为它相当于要执行shell命令'd' (而且shell也没有这个命令)! 很危险的!

The : addresses can also refer to marks. Thus you can use: :'a,'bg/foo/j to join any line containing the string foo to its subsequent line, if it lies between the lines between the 'a' and 'b' marks. (Yes, all of the preceding ex command examples can be limited to subsets of the file's lines by prefixing with these sorts of addressing expressions).


More powerful is the :r! command. This reads the results of a command. It's the same as suspending the vi session, running a command, redirecting its output to a temporary file, resuming your vi session, and reading in the contents from the temp. file.


More powerful is the :r! command. This reads the results of a command. It's the same as suspending the vi session, running a command, redirecting its output to a temporary file, resuming your vi session, and reading in the contents from the temp. file.
 这个好理解,读取外部命令的执行结果到当前buffer中。

Even more powerful are the ! (bang) and :... ! (ex bang) commands. These also execute external commands and read the results into the current text. However, they also filter selections of our text through the command! This we can sort all the lines in our file using 1G!Gsort (G is the vi "goto" command; it defaults to going to the last line of the file, but can be prefixed by a line number, such as 1, the first line). This is equivalent to the ex variant :1,$!sort. Writers often use ! with the Unix fmt or fold utilities for reformating or "word wrapping" selections of text. A very common macro is {!}fmt (reformat the current paragraph). Programmers sometimes use it to run their code, or just portions of it, through indent or other code reformatting tools.

Using the :r! and ! commands means that any external utility or filter can be treated as an extension of our editor. I have occasionally used these with scripts that pulled data from a database, or with wget or lynx commands that pulled data off a website, or ssh commands that pulled data from remote systems.

 

Most commands accept a amount and direction, for example:
 ?cW = change till end of word
 ?3cW = change 3 words
 ?BcW = to begin of full word, change full word
 ?ciW = change inner word.
 ?ci" = change inner between ".."
 ?ci( = change text between ( .. )

 
'. jump back to last edited line.   ==〉不管光标移动了多少次,都能准确找到编辑位置的!
等效于: gi 命令! 但是,``命令则是在各次跳转间移动!


The Control+R mechanism is very useful:-) (in insert mode)
 " the unnamed register, containing the text of the last delete or yank
 % the current file name
 # the alternate file name
 * the clipboard contents (X11: primary selection)
 + the clipboard contents
 / the last search pattern
 : the last command-line
 . the last inserted text
 - the last small (less than a line) delete
 =5*5 insert 25 into text (mini-calculator)
 
 
 I recommend NERDtree instead of the built-in explorer. It has changed the way I used vim for projects and made me much more productive. Just google for it.
 
 

Session Operation:

a. save session
:mks sessionname
 
b. force save session
:mks! sessionname
 
c. load session
gvim or vim -S sessionname
 
  =================================================
 
vi{ 选中{}中间的内容,不包括{}  等效: viB
va{ 选中{}中间内容,包括{}
vi( 选中()中间内容       等效: vib
vi< 选中<>中间内容
vi[ 选中[]中间内容
vit 选中<tag></tag>中间的内容
vi" 选中""中间内容
vi' 选中''中间的内容

vis 选中一个句子
vip 选中一个段落     //WYH: 测试发现,这两个是选中两个空行之间的内容!
...

:h text-objects

 

 

 

 

你可能感兴趣的:(vi操作实用命令)