记录我总结的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。可以使用双反撇号(``)命令在当前书签和前一个书签之间切换。
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参考: http://zhidao.baidu.com/link?url=apDhBu8AXiRQ34FSMyI8ubBl1iAmzNquFAhZ3qg8RdNMaGnjJfdYlMtEqWTjxXS43VNHEUD30mUug2x9SY1xfq还有一个相关参数: set ma 允许修改。 set noma 禁止修改。 严格的说明,请使用底行运令查看帮助内容: :h ma :h modif
八、块操作 (原文: http://coolshell.cn/articles/5426.html 作者:陈皓)
块操作: <C-v>
块操作,典型的操作: 0 <C-v> <C-d> I-- [ESC]
^
→ 到行头<C-v>
→ 开始块操作<C-d>
→ 向下移动 (你也可以使用hjkl来移动光标,或是使用%,或是别的)I-- [ESC]
→ I是插入,插入“--
”,按ESC键来为每一行生效。
九、窗口间移动
参考: 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 filename | Open filename for edition |
:w | Save file |
:q | Exit Vim |
:q! | Quit without saving |
:x | Write file (if changes has been made) and exit |
:sav filename | Saves 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 Arrow | move the cursor up one line |
j or Down Arrow | move the cursor down one line |
e | move the cursor to the end of the word |
b | move the cursor to the begining of the word |
0 | move the cursor to the begining of the line |
G | move the cursor to the end of the line |
gg | move the cursor to the begining of the file |
L | move the cursor to the end of the file |
:59 | move 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 |
剪切、复制和粘贴
y | Copy the selected text to clipboard |
p | Paste clipboard contents |
dd | Cut current line |
yy | Copy current line |
y$ | Copy to end of line |
D | Cut to end of line |
搜索
/word | Search word from top to bottom |
?word | Search word from bottom to top |
* | Search the word under cursor |
/\cstring | Search STRING or string, case insensitive |
/jo[ha]n | Search john or joan |
/\< the | Search 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\|joe | Search 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/g | Search something in all the open buffers and replace it with somethingelse |
替换
:%s/old/new/g | Replace all occurences of old by new in file |
:%s/onward/forward/gi | Replace onward by forward, case unsensitive |
:%s/old/new/gc | Replace all occurences with confirmation |
:2,35s/old/new/g | Replace all occurences between lines 2 and 35 |
:5,$s/old/new/g | Replace all occurences from line 5 to EOF |
:%s/^/hello/g | Replace the begining of each line by hello |
:%s/$/Harry/g | Replace the end of each line by Harry |
:%s/onward/forward/gi | Replace onward by forward, case unsensitive |
:%s/ *$//g | Delete all white spaces |
:g/string/d | Delete all lines containing string |
:v/string/d | Delete 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/g | Replace Bill by Steve in current line |
:%s/Bill/Steve/g | Replace Bill by Steve in all the file |
:%s/^M//g | Delete DOS carriage returns (^M) |
:%s/\r/\r/g | Transform DOS carriage returns in returns |
:%s#<[^>]\+>##g | Delete HTML tags but keeps text |
:%s/^\(.*\)\n\1$/\1/ | Delete lines which appears twice |
Ctrl+a | Increment number under the cursor |
Ctrl+x | Decrement number under cursor |
ggVGg? | Change text to Rot13 |
大小写
Vu | Lowercase line |
VU | Uppercase line |
g~~ | Invert case |
vEU | Switch word to uppercase |
vE~ | Modify word case |
ggguG | Set all text to lowercase |
gggUG | Set all text to uppercase |
:set ignorecase | Ignore case in searches |
:set smartcase | Ignore case in searches excepted if an uppercase letter is used |
:%s/\<./\u&/g | Sets first letter of each word to uppercase |
:%s/\<./\l&/g | Sets 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 outfile | Saves lines 1 to 10 in outfile |
:1,10 w >> outfile | Appends lines 1 to 10 to outfile |
:r infile | Insert the content of infile |
:23r infile | Insert the content of infile under line 23 |
文件浏览器
:e . | Open integrated file explorer |
:Sex | Split window and open integrated file explorer |
:Sex! | Same as :Sex but split window vertically |
:browse e | Graphical file explorer |
:ls | List buffers |
:cd .. | Move to parent directory |
:args | List files |
:args *.php | Open file list |
:grep expression *.php | Returns a list of .php files contening expression |
gf | Open file name under cursor |
和 Unix 系统交互
:!pwd | Execute the pwd unix command, then returns to Vi |
!!pwd | Execute the pwd unix command and insert output in file |
:sh | Temporary returns to Unix |
$exit | Retourns to Vi |
对齐
:%!fmt | Align all lines |
!}fmt | Align all lines at the current position |
5!!fmt | Align the next 5 lines |
Tabs/Windows
:tabnew | Creates a new tab |
gt | Show next tab |
:tabfirst | Show first tab |
:tablast | Show last tab |
:tabm n(position) | Rearrange tabs |
:tabdo %s/foo/bar/g | Execute a command in all tabs |
:tab ball | Puts all open files in tabs |
:new abc.txt | Edit abc.txt in new window |
分屏显示
:e filename | Edit filename in current window |
:split filename | Split the window and open filename |
ctrl-w up arrow | Puts cursor in top window |
ctrl-w ctrl-w | Puts 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 file | Split window vertically |
:sview file | Same as :split in readonly mode |
:hide | Close current window |
:nly | Close all windows, excepted current |
:b 2 | Open #2 in this window |
自动完成
Ctrl+n Ctrl+p (in insert mode) | Complete word |
Ctrl+x Ctrl+l | Complete line |
:set dictionary=dict | Define dict as a dictionnary |
Ctrl+x Ctrl+k | Complete 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.org | Define mail as abbreviation of mail@provider.org |
文本缩进
:set autoindent | Turn on auto-indent |
:set smartindent | Turn on intelligent auto-indent |
:set shiftwidth=4 | Defines 4 spaces as indent size |
ctrl-t, ctrl-d | Indent/un-indent in insert mode |
>> | Indent |
<< | Un-indent |
=% | Indent the code between parenthesis |
1GVG= | Indent the whole file |
语法高亮
:syntax on | Turn on syntax highlighting |
:syntax off | Turn off syntax highlighting |
:set syntax=perl | Force syntax highlighting |
via catswhocode