VIM 总结

     



      对于那些vim初学者,强烈建议看看这个交互式的vim教程 

      记录我总结的VIM技巧

       一、删除

     (1)d/text  

             删除从文本中出现“text”中所指定字样的位置,一直向前直到下一个该字样所出现的位置(但不包括该字   样)之间的内容。

       例子:

       pWin = initscr();

       要删除“initscr();”之前的内容,在命令模式下输入 d/init 即可。

     (2)d$

            删除到某一行的结尾。

      二、复制

    (1) yG

              从当前行复制到文件的最后一行。

           (y是复制命令,而按G会移动到文件尾,二者结合可知。还有很多可以结 合的命令,发挥你的想象力吧。

   (2) y%

             将光标移动到{上,然后按下y%,就可以复制两个括号和它们之间的内容。

            (%可以用来找到匹配的花括号,所以联想到和复制命令结合即可复制“一块”数据。)


    三、替换

          (1)  替换路径

            :%s/\/home\/huntinux\/bin/\/rum\/media\/huntinux\/F\/huntinux_11,1

             或者简洁一点的:

             :%s#/home/huntinux/bin#/run/media/huntinux/F/huntinux_bin#g 

                     第二种使用#作为分割符号翻开替换串与被替换串,可以避免与字符串中的符号冲突(否则需要用转移符号\)

    (2) 替换单个单词

         例如:将rum换成run。

          :s/\<rum\>/run          (只替换第一个)

          :s/\<rum\>/run/g      (替换本行所有)
          :%s/\<rum\>/run/g   (替换文件所有)

   四、格式化代码 (原文)

         (1) 格式化全文

                    gg=G                  

    (2)自动缩进当前行

           ==

        书签(原文)

            可以让vi 在文件中的特定位置放上书签。方法是按 Escape 键,再按 m 键,然后输入另一个表示书签引用的字母表字符。因此,最多可以有 26 个书签,分别名为 a 到z。要返回到上一书签,按 Escape 键,再按反撇号(`),然后输入书签引用字符。

例如,按 Escape 之后按m 和 a 键,就会把当前游标位置保存在书签 a 中。在编辑会话中,以后希望返回到这个游标位置时,只需按 Escape,然后输入 `a。可以使用双反撇号(``)命令在当前书签和前一个书签之间切换。


    六使用vim在文件中插入命令执行的输出结果  

           http://www.cnblogs.com/JessonChan/archive/2010/12/19/1910924.html

                

:r !command , command命令的结果插入光标下一行
:nr! command,  command命令的结果插入n行后
:m,n! command, command以m,n之间的行作为标准输入,标准输出会代替m,n之间的行
如 
:r !date 在下一行插入当前日期
:3r !ls 在第3行后插入文件(夹)名字

:3,10! pwd 把第3-10行用当前文件路径代替

在代码中加入日期
:r !date +\%F
注意%需要转义。(前面加一个\)


    七以只读形式打开文件

vim  -R file
或者

运行底行命令:
:set modifiable
如果要恢复,则:
:set nomodifiable
还有一个相关参数:
set ma
允许修改。
set noma
禁止修改。
严格的说明,请使用底行运令查看帮助内容:
:h ma
:h modif
参考: http://zhidao.baidu.com/link?url=apDhBu8AXiRQ34FSMyI8ubBl1iAmzNquFAhZ3qg8RdNMaGnjJfdYlMtEqWTjxXS43VNHEUD30mUug2x9SY1xfq


块操作  (原文: http://coolshell.cn/articles/5426.html 作者:陈皓)

块操作: <C-v>

块操作,典型的操作: 0 <C-v> <C-d> I-- [ESC]

  • ^ → 到行头
  • <C-v> → 开始块操作
  • <C-d> → 向下移动 (你也可以使用hjkl来移动光标,或是使用%,或是别的)
  • I-- [ESC] → I是插入,插入“--”,按ESC键来为每一行生效。

Rectangular blocks


窗口间移动

参考: http://doc.chinaunix.net/linux/201305/2766308.shtml

ctrl w + h/j/k/l 左/下/上/右

ctrl w + w 下一个

ctrl w + p 上一个

切分窗口用sp/vsp 


移动

参考:http://easwy.com/blog/archives/advanced-vim-skills-basic-move-method/

zz : 光标到屏幕中间

zt :到顶部

zb :到底部


十一显示man文档帮助

将光标移动到需要察看man文档的函数上,然后按shift+k即可。



十二、没有 '+'寄存器

寄存器'+'可以用来赋值系统剪切板的内容,如果你的vim没有这个寄存器,那么是因为在编译的时候没有加上 xterm_clipboard选项

下载最新的 vim7.4 ,按照源码包中的提示,编译安装即可

http://superuser.com/questions/235505/compiling-vim-with-xterm-clipboard-support

In ubuntu , you can do this:

sudo apt-get install vim

To see if your vim has 'xterm_clipboard'

vim --version | grep xterm_clipboard






=======================一些小技巧=======================

1、临时推出vim,进入终端:

ctrl+z   挂起vim

fg  返回

如果挂起了多个进程,可以用 jobs 命令察看, 然后 fg %作业号 返回该进程。

2、:reg  可以察看寄存器的内容。

安装了vim-gnome 之后可以使用 "+  和 "* 两个寄存器,他们可以用来从vim外面复制内容。



==========其他一些参考============

http://www.oschina.net/news/43167/130-essential-vim-commands

超过 130 个你需要了解的 vim 命令

从 1970 年开始,vi 和 vim 就成为了程序员最喜爱的文本编辑器之一。5年前,我写了一个问自己名为 “每个程序员都应该知道的 100 个 vim 命令” 这次算是之前那篇文章的改进版,希望你会喜欢。

基础

:e filenameOpen filename for edition
:wSave file
:qExit Vim
:q!Quit without saving
:xWrite file (if changes has been made) and exit
:sav filenameSaves file as filename
.Repeats the last change made in normal mode
5.Repeats 5 times the last change made in normal mode

在文件中移动

k or Up Arrowmove the cursor up one line
j or Down Arrowmove the cursor down one line
emove the cursor to the end of the word
bmove the cursor to the begining of the word
0move the cursor to the begining of the line
Gmove the cursor to the end of the line
ggmove the cursor to the begining of the file
Lmove the cursor to the end of the file
:59move cursor to line 59. Replace 59 by the desired line number.
20|move cursor to column 20.
%Move cursor to matching parenthesis
[[Jump to function start
[{Jump to block start

剪切、复制和粘贴

yCopy the selected text to clipboard
pPaste clipboard contents
ddCut current line
yyCopy current line
y$Copy to end of line
DCut to end of line

搜索

/wordSearch word from top to bottom
?wordSearch word from bottom to top
*Search the word under cursor
/\cstringSearch STRING or string, case insensitive
/jo[ha]nSearch john or joan
/\< theSearch the, theatre or then
/the\>Search the or breathe
/\< the\>Search the
/\< ¦.\>Search all words of 4 letters
/\/Search fred but not alfred or frederick
/fred\|joeSearch fred or joe
/\<\d\d\d\d\>Search exactly 4 digits
/^\n\{3}Find 3 empty lines
:bufdo /searchstr/Search in all open files
bufdo %s/something/somethingelse/gSearch something in all the open buffers and replace it with somethingelse

替换

:%s/old/new/gReplace all occurences of old by new in file
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/old/new/gcReplace all occurences with confirmation
:2,35s/old/new/gReplace all occurences between lines 2 and 35
:5,$s/old/new/gReplace all occurences from line 5 to EOF
:%s/^/hello/gReplace the begining of each line by hello
:%s/$/Harry/gReplace the end of each line by Harry
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/ *$//gDelete all white spaces
:g/string/dDelete all lines containing string
:v/string/dDelete all lines containing which didn’t contain string
:s/Bill/Steve/Replace the first occurence of Bill by Steve in current line
:s/Bill/Steve/gReplace Bill by Steve in current line
:%s/Bill/Steve/gReplace Bill by Steve in all the file
:%s/^M//gDelete DOS carriage returns (^M)
:%s/\r/\r/gTransform DOS carriage returns in returns
:%s#<[^>]\+>##gDelete HTML tags but keeps text
:%s/^\(.*\)\n\1$/\1/Delete lines which appears twice
Ctrl+aIncrement number under the cursor
Ctrl+xDecrement number under cursor
ggVGg?Change text to Rot13

大小写

VuLowercase line
VUUppercase line
g~~Invert case
vEUSwitch word to uppercase
vE~Modify word case
ggguGSet all text to lowercase
gggUGSet all text to uppercase
:set ignorecaseIgnore case in searches
:set smartcaseIgnore case in searches excepted if an uppercase letter is used
:%s/\<./\u&/gSets first letter of each word to uppercase
:%s/\<./\l&/gSets first letter of each word to lowercase
:%s/.*/\u&Sets first letter of each line to uppercase
:%s/.*/\l&Sets first letter of each line to lowercase

读写文件

:1,10 w outfileSaves lines 1 to 10 in outfile
:1,10 w >> outfileAppends lines 1 to 10 to outfile
:r infileInsert the content of infile
:23r infileInsert the content of infile under line 23

文件浏览器

:e .Open integrated file explorer
:SexSplit window and open integrated file explorer
:Sex!Same as :Sex but split window vertically
:browse eGraphical file explorer
:lsList buffers
:cd ..Move to parent directory
:argsList files
:args *.phpOpen file list
:grep expression *.phpReturns a list of .php files contening expression
gfOpen file name under cursor

和 Unix 系统交互

:!pwdExecute the pwd unix command, then returns to Vi
!!pwdExecute the pwd unix command and insert output in file
:shTemporary returns to Unix
$exitRetourns to Vi

对齐

:%!fmtAlign all lines
!}fmtAlign all lines at the current position
5!!fmtAlign the next 5 lines

Tabs/Windows

:tabnewCreates a new tab
gtShow next tab
:tabfirstShow first tab
:tablastShow last tab
:tabm n(position)Rearrange tabs
:tabdo %s/foo/bar/gExecute a command in all tabs
:tab ballPuts all open files in tabs
:new abc.txtEdit abc.txt in new window

分屏显示

:e filenameEdit filename in current window
:split filenameSplit the window and open filename
ctrl-w up arrowPuts cursor in top window
ctrl-w ctrl-wPuts cursor in next window
ctrl-w_Maximize current window vertically
ctrl-w|Maximize current window horizontally
ctrl-w=Gives the same size to all windows
10 ctrl-w+Add 10 lines to current window
:vsplit fileSplit window vertically
:sview fileSame as :split in readonly mode
:hideClose current window
:­nlyClose all windows, excepted current
:b 2Open #2 in this window

自动完成

Ctrl+n Ctrl+p (in insert mode)Complete word
Ctrl+x Ctrl+lComplete line
:set dictionary=dictDefine dict as a dictionnary
Ctrl+x Ctrl+kComplete with dictionnary

Marks

m {a-z}Marks current position as {a-z}
' {a-z}Move to position {a-z}
''Move to previous position

缩写

:ab mail mail@provider.orgDefine mail as abbreviation of mail@provider.org

文本缩进

:set autoindentTurn on auto-indent
:set smartindentTurn on intelligent auto-indent
:set shiftwidth=4Defines 4 spaces as indent size
ctrl-t, ctrl-dIndent/un-indent in insert mode
>>Indent
<<Un-indent
=%Indent the code between parenthesis
1GVG=Indent the whole file

语法高亮

:syntax onTurn on syntax highlighting
:syntax offTurn off syntax highlighting
:set syntax=perlForce syntax highlighting

 

via catswhocode


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值