http://vcd.gro.clinux.org/doc/usr_03.html#usr_03.txt
vim常用技巧
所以我初步设想分三大部分来写
(1)vim基础
(2)vim tips的翻译和解释
(3)vimrc
由于水平所限,有错误和可笑之处,恳请批评。
这里就附带说一下vim基础了
很简单的概括一下:
1.程序
Unix: vim
Win : gvim
2.工作方式
normal mode普通模式
insert mode插入模式
visual mode可视模式
visual-line mode
visual-block mode
command-line mode命令行模式
search mode搜索模式
模式切换:记住Esc总可以返回 普通模式
normal = i => insert
normal = v => visual
normal = Shift-V => visual-line
normal = Ctrl-V => visual-block
normal = : => command-line
normal = / => search
3.光标移动
开始,会用方向键和PgUp,PgDn就可以用了
vim 和 perl 一样,你掌握一个子集,就可以很好的使用了
4.最后,说一下两个概念<cword>和<cWORD>
<cword>称为狭义字,它指一个单词,其中不包含空白和特殊字符
<cWORD>称为广义字,它所指的单词可以包含一些特殊字符
具体有那些字符可以看help
通常,它们会和你想的一样:)
因为第二部分经常用到这个概念,所以在此说一下
—————————————-
# 基础
—————————————-
* # g* g# : 寻找光标处的狭义单词(<cword>) (前向/后向)
% : 括号配对寻找 {}
matchit.vim : 使得 % 能够配对标记 <tr><td><script> <?php 等等
<C-N><C-P> : 插入模式下的单词自动完成
<C-X><C-L> : 行自动完成(超级有用)
/<C-R><C-W> : 把狭义单词 <cword> 写到 搜索命令 行
/<C-R><C-A> : 把广义单词 <cWORD> 写到 搜索命令 行
:set ignorecase : 搜索时忽略大小写
:syntax on : 在 Perl,HTML,PHP 等中进行语法着色
:h regexp<C-D> : 按下 control-D 键即可得到包含有 regexp 的帮助主题的列表
: (使用TAB可以实现帮助的自动补齐)
—————————————-
# 使更新 _vimrc 更容易
:nmap ,s :source \(VIM/_vimrc<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">译释:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">nmap </font></span><span style="font-size: 9pt; font-family: 宋体;">是绑定一个在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">normal</font></span><span style="font-size: 9pt; font-family: 宋体;">模式下的快捷键</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:nmap ,v :e \)VIM/_vimrc
# 译释:在normal模式下,先后按下 ,s 两个键执行_vimrc,而 ,v 则是编辑_vimrc
—————————————-
# visual 模式 (例子是:轻松添加其他的 HTML Tags)
:vmap sb “zdi<b><C-R>z</b><ESC> : 在visual模式下选中的文字前后分别加上<b>和</b>
# 译释:vmap 是绑定一个在visual模式下的快捷键
# 译释:原理:在visual模式下,”zd 把一个选中的区域命名为z 然后删除,
# i 进入插入模式,输入<b>,<C-R>z 撤销刚才的删除,然后再写入</b>,
# 最后<ESC>返回normal模式
# 译释:“z 命令创建一个选中的区域为register,并把它命名为z
# 译释:更令人开心的有:在visual模式下选中几行,然后输入 2> ,
# 则选中的行会全部缩进两个tab
# 555,偶一开始还是用 :xx,xx s/^//t/t/,好傻啊!
:vmap st ”zdi<?= <C-R>z ?><ESC> : 在visual模式下选中的文字前后分别加上<?= 和 ?>
—————————————-
# 文件浏览
:Ex : 开启目录浏览器,注意首字母E是大写的
:Sex : 在一个分割的窗口中开启目录浏览器
:ls : 显示当前buffer的情况
:cd .. : 进入父目录
:args : 显示目前打开的文件
:lcd %:p:h : 更改到当前文件所在的目录
# 译释:lcd是紧紧改变当前窗口的工作路径,% 是代表当前文件的文件名,
# 加上 :p扩展成全名(就是带了路径),加上 :h析取出路径
:autocmd BufEnter * lcd %:p:h : 自动更改到当前文件所在的目录
# 译释:autocmd指定一个自动命令,BufEnter指定一个事件,* 指定事件的对象,
# lcd %:p:h 指定一个动作
# hehe,好像和写记叙文差不多
—————————————-
# 缓冲区(buffer)浏览器 (第三方的一个最流行的脚本)
# 需要下载 bufexplorer.vim ,http://www.vim.org/script.php?script_id=42 上就有
/be : 在缓冲区浏览器中打开缓冲区列表
/bs : 以分割窗口的形式打开缓冲区浏览器
—————————————-
# 大小写转换
guu : 行小写
gUU : 行大写
g~ : 行翻转(当然指大小写啦)
# 译释: g 是大小写转换命令(greate),u/U/是三种转换形式(小写/大写/翻转),
# 最后一个重复则表示该转换是对于一行而言的
guw : 字大写(狭义字) 译注:建议对比iw
gUw : 字小写(狭义字)
g~w : 字翻转(狭义字)
# 译释:最后一个w 表示该转换是对于一个字而言的,由于是在normal模式下,
# 所以这个w 表示一个狭义字<cword>
vEU : 字大写(广义字)
vE~ : 字翻转(广义字)
# 译释:vE 这个指令组合会进入visual模式,然后选择一个广义字<CWORD>
ggguG : 把整个文章全部小写(ft!bt!)
gf : 取当前光标处的广义字作为文件名,然后试图打开它!
# 译释:为什么是广义字呢?因为这样可以方便的取到路径啊,像/var/www/html/index.htm
ga : 显示光标处字符的ascii,hex,oct,…晕菜的一堆转换
ggVGg? : 用rot13编码整个文件(晕!)
# 译释:gg到文件首行首字符,V进入Visual-Line模式,G到文件末行首字符,
# 这样就选中了整篇文章,然后g?就是用rot13编码整个文件啦
#
# 【关于rot13——谁让英文是偶数个字母啊】
# ROT13 是一种简单的编码,它把字母分成前后两组,每组13个,编码和解码
# 的算法相同,仅仅交换字母的这两个部分,即:[a..m] –> [n..z] 和 [n..z]
# –> [a..m] 。 ROT13 用简易的手段使得信件不能直接被识别和阅
# 读,也不会被搜索匹配程序用通常的方法直接找到。经常用于 USENET 中发表一
# 些攻击性或令人不快的言论或有简单保密需要的文章。
# 由于 ROT13 是自逆算法,所以,解码和编码是同一个过程。
<C-A>,<C-X> : 增加,减少 光标处的狭义字所表示的数字
:(,仅仅是分割了这两个命令,不是命令的一部分)
: Win32的用户可能需要重新定义一下Ctrl-A,呵呵
# 译注:good guy, 令人不得不想到perl的数字串
<C-R>=55 : 插入25 (这是一个迷你计算器耶!)
—————————————-
# 好玩的东东
:h 42 : 也可以访问 http://www.google.com/search?q=42
: 第一个结果就是 News. Douglas Adams 1952 - 2001.
: Floor 42 extends its deepest sympathies to
: the family, friends, and fans of Douglas Adams.
:h holy-grail
:h!
—————————————-
# 标记和移动
‘. : 跳到最后修改的那一行 (超级有用)(ft,怎么又是这个评价)
`. : 不仅跳到最后修改的那一行,还要定位到修改点
<C-O> : 依次沿着你的跳转记录向回跳 (从最近的一次开始)
<C-I> : 依次沿着你的跳转记录向前跳
:ju(mps) : 列出你跳转的足迹
:help jump-motions
:history : 列出历史命令记录
:his c : 命令行命令历史
:his s : 搜索命令历史
q/ : 搜索命令历史的窗口
q: : 命令行命令历史的窗口
:<C-F> : 历史命令记录的窗口
—————————————-
# 缩写和键盘映射(原文中文件举例都用了c:/aaa/x,偶全给他改成/path/file了,哼唧)
:map <f7> :’a,‘bw! /path/file
# 译释:map是映射一个normal模式下的键
# 这里是把F7键映射成把标记a到标记b中间的内容另存为一个文件/path/file
# 标记(mark)的方法:把光标移动到需要标记的地方,输入m,然后输入标记名,例如a
# 引用标记的方法:’a ,即:单引号加标记名
:map <f8> :r /path/file
# 译释:把F8键映射成在当前位置插入文件/path/file的内容
:map <f11> :.w! /path/file2<CR>
# 译释:.(点号)表示当前行
# 所以F11就是把当前行存为/path/file2
# 最后的<CR>表示一个回车
:map <f12> :r /path/file2<CR>
:ab php : 列出php表示的缩写
# 译释:定义一个缩写使用::iab hm hmisty
# 一个有趣的现象是,它列出的会是php和它的前子串开头的缩写
# 例如,有这么几个缩写:
# h => hmisty1 , hm => hmisty2 , hmi => hmisty3, m => hmisty4
# 那么使用 :ab hm会显示这么几个缩写:hm 和 h
# 而不是你想象中的 hm 和 hmi
:map , : 列出以逗号开始的键盘映射
# 译释:一般而言,我们称这些逗号开始的组合键为“逗号命令”
# 不过hmisty更喜欢用;构成“分号命令”
# 而且不是用map,而是用imap
# 因为偶懒么,懒得按<Esc>,所以直接在insert模式下就执行命令了
# 为什么用分号呢?因为我最常用它写程序啊
# perl/C/C++/object pascal/java,都是用分号结束一个语句
# 我们一般很少在分号后面连续写其他字符
# 所以用“分号+其他键”就很少会在输入的时候造成冲突
# 在键盘映射中常用的表示
<CR> : 回车
<ESC> : Esc
<LEADER> : 转义符号 /
<BAR> : 管道符号 |
—————————————-
# 列出寄存器(Registers)
:reg : 显示所有当前的registers
“1p : ”表示引用register,1表示一个名字叫做1的register,
: p就是粘贴(paste)命令
# 译释:“也用来定义register
# 先输入 ”,表示定义register
# 然后输入名字,如0~9,a~z
# 然后执行删除或复制命令,如dd或y,
# 或者是visual模式下的d(删除选中的部分)或y(复制选中的部分)
# 则被删除或复制的部分就被存入了这个命名的register
#
# 观察:一个特殊的register, “” ,里面存储了一个匿名的删除/复制
# 在你执行dd或y的时候,被作用的部分被存到了“”中
# 这些和perl是多么像啊
—————————————-
# Useful trick
“ayy@a : 把当前行作为一个Vim命令来执行
# 译释:”ayy 是定义当前行到register a,然后@a是执行register a中存储的指令
# yy: 复制一行
# 10yy: 复制从此向下的10行
yy@“ : 用上面所提到的那个匿名register
—————————————-
# 从其他程序获取输出 (需要外部程序)
:r!ls.exe : 读取ls的输出到当前位置
!!date : 读取date的输出 (但是会替换当前行的内容)
# 译释:其实你输入了!!后,vim就自动转换到 :.! 等待你继续输入
# 使用外部程序sort进行排序(sort是Unix标准命令,ls,date也是)
:%!sort -u : 使用sort程序排序整个文件(用结果重写文件)
# 译释:%表示整个文件的所有行
# !sort表示执行外部命令sort
# -u是sort的参数,man sort看看,这个参数的意义是合并相同的行
# u就是unique,如果两行内容相同,则结果中只保留一行的说
:‘a,’b!sort -u : 对mark a 到mark b中间的内容进行排序
!1} sort -u : 排序当前段落 (只能在normal模式下使用!!)
# 译释:!表示使用filter,1}表示filter的对象是从当前行开始向后数一段
# 段落指到空行处结束,不包括空行
# 其实你一旦输入 !1},vim就自动计算当前段落应该到那一行(eg.+5),然后生成
# :.,.+5! 等待之后输入sort -u,回车,完成操作
# .表示当前行,.+5当然就是当前行向后数5行
—————————————-
# 多文档操作 (基础)
# 译注:用 :ls! 可以显示出当前所有的buffer
:bn : 跳转到下一个buffer
:bp : 跳转到上一个buffer
:wn : 存盘当前文件并跳转到下一个(又是“超级……”,ft!)
:wp : 存盘当前文件并跳转到上一个
:bd : 把这个文件从buffer列表中做掉
:bun : 卸掉buffer (关闭这个buffer的窗口但是不把它从列表中做掉)
:badd file.c : 把文件file.c添加到buffer列表
:b 3 : 跳到第3个buffer
:b main : 跳到一个名字中包含main的buffer,例如main.c
: (ultra,这个怎么翻译?:()
:sav php.html : 把当前文件存为php.html并打开php.html
:sav! %<.bak : 换一个后缀保存
:e! : 返回到修改之前的文件(修改之后没有存盘)
:w /path/% : 把文件存到一个地儿
:e # : 编辑标记为#的buffer(这个buffer必须含有一个可编辑的文件)
: 用ls命令就能看到哪一个buffer有#
: %a表示当前正在编辑的buffer
: u 表示不能编辑或者已经被做掉的buffer
:e #3 : 编辑编号为3的buffer(这个buffer必须含有一个可编辑的文件)
:rew : 回到第一个可编辑的文件
:brew : 回到第一个buffer
:sp fred.txt : 在一个水平分割的窗口中打开文件fred.txt
# 译注:vs fred.txt可以实现垂直分割
:sball : 把当前所有含有可编辑文件的buffer显示到一个分割窗口中
: (偶该考虑把super翻译成 高级指令 了,ft)
:map <F5> :ls<CR>:e # : 在normal模式下按F5键,则会显示所有含有一个
: 可编辑文件的buffer,然后提示你输入buffer的序号,
: 输入后回车,则编辑这个buffer
# 译注:这是一个键盘绑定
:set hidden : 允许不保存buffer而切换buffer (w/o=without)
—————————————-
# 在分割窗口中快速切换
:map <C-J> <C-W>j<C-W>
# 译注:原文此处有误,前面应该加上冒号
# 这是一个键盘绑定,把Ctrl-J定义成切换到下一个窗口并最大化
:map <C-K> <C-W>k<C-W>
—————————————-
# 命令录制 (**技巧,ft)
#录制到q
… #输入一系列复杂的指令
q #再次按q停止录制
@q #执行q中存储的指令
@@ #重复执行
# 编辑register/录制
”ap #把register a中的内容贴到当前位置
… #现在你可以修改它了
“add#删除之,重新存入register a
@a #执行register a中的指令
—————————————-
# _vimrc基础
:set incsearch : 实时匹配你输入的内容
:set wildignore=.o,.obj,.bak,*.exe : tab键的自动完成现在会忽略这些
:set shiftwidth=4 : 现在自动缩进将是4个字符
# 译注:一个tab位通常是8个字符
# 所以,我们还要设定 :set tabstop=4,这样,所有的缩进都是4字符了
# emacs默认就是4字符缩进吧?
:set vb t_vb=”. : 沉默方式(不要叫beep!)
—————————————-
# 加载windows iexplorer来浏览(我想这只有在windows下用gvim才能用到)
:nmap ,f :update<CR>:silent !start c:/progra~1/intern~1/iexplore.exe file://%:p
# 译释:nmap是做一个normal模式下的键盘绑定
# 这里绑定了一个逗号命令 ,f
# :update是写这个文件,与:w不同,它只有当文件被修改了的时候才写
# :silent别让弹出窗口报告执行结果
# !…后面就是执行windows命令了。呵呵,去问bill gates什么意思吧。
# 不过偶用gvim 6.1试过了,好用!
:nmap ,i :update<CR>: !start c:/progra~1/intern~1/iexplore.exe <cWORD><CR>
—————————————-
# 用VIM编辑ftp文件
:cmap ,r :Nread ftp://209.51.134.122/public_html/index.html
:cmap ,w :Nwrite ftp://209.51.134.122/public_html/index.html
# 译注:原文丢失了开头的冒号
# cmap是命令(command)模式绑定
gvim ftp://209.51.134.122/public_html/index.html
# 这一句就是开始编辑一个ftp远端的文件,ft
—————————————-
# 附加到一个register (就是用大写的register名字啦!)
“a5yy #复制5行到a中
10j #下移10行
”A5yy #再添加5行到a中
—————————————-
[I : 显示光标处的狭义字可以匹配的行(高级指令)
# 译注:# 可以全文查找与光标处的狭义字相匹配的字,
# 这在查找函数原型和实现,或者变量使用的时候很有用
—————————————-
# 常规缩进
:‘a,’b>>
# 译释:把mark a到mark b之间的内容进行两次缩进
# 在visual模式下缩进 (无限可重复)
:vnoremap < <gv
# 译释::vnoremap 重定义了visual模式下 < 符号的含义
# 把它定义成 <gv
# 即:先<向外缩进,然后gv重新选择上一次选择了的区域
# 这样在visual模式下就可以实现连续按<而连续缩进了
:vnoremap > >gv
# 同里,内缩
—————————————-
# 查找(译注:建议先学习正则表达式)
# 译注:查找命令不用进入:命令模式,直接按/就可以了
# 如果没有修饰,可以不要右边的/
# 和smth bbs差不多啦,呵呵
/joe/e : 光标停留在匹配单词最后一个字母处
/joe/e+1 : 光标停留在匹配单词最后一个字母的下一个字母处
/joe/s : 光标停留在匹配单词第一个字母处
/^joe.*fred.*bill/ : ft,标准正则表达式
/^[A-J]/+/ : 找一个以AJ中一个字母重复两次或以上开头的行
/forum/(/_./)*pent : 多行匹配
/fred/_sjoe/i : 中间可以有任何空白,包括换行符/n
# 译注:这个和perl不太一样的哦
/fred/|joe : 匹配FRED或JOE
//<fred/>/i : 匹配fred,fred必须是一个独立的单词,而不是子串
# 译注:这和perl也不太一样,perl是用/b做单词定界符的
//</d/d/d/d/> : 匹配4个数字
/</d/{4}/> : 也是匹配4个数字
# 在visual模式下查找
:vmap g/ y/<C-R>“<CR> : 匹配选中的高亮文字
# 译释:vmap是在visual模式下的键盘映射
# 映射了g/这个命令组合
# y 把选中的高亮文字写入匿名register ”
# / 打开搜索模式
# <C-R> 准备粘贴register
# “ 粘贴了”“中的内容
# <CR> 回车,执行
:vmap <silent> g/ y/<C-R>=escape(@”, ‘///.$^[]’)<CR><CR> : with spec chars
# 译释:@#\(&^*@#%&*#\)@!
# 跨行匹配,/_ 表示允许匹配换行符,或者说,允许匹配新行
# 译注:小心,和perl不一样
/<!–/_p/{-}–> : 匹配多行注释
/fred/s*joe/i : 似乎上面有了,ft
/bugs/(/./)bunny : 中间可以有无数东西
:h /_ : 看看关于 /_ 的帮助
# 查找当前光标位置所在子例程/函数(subroutine/function)的声明
:nmap gx yiw/^/(sub/<bar>function/)/s/+<C-R>“<CR>
# 译释:nmap 做一个normal模式下的键盘绑定
# y 进入复制状态,后面需要一个motion
# 接着就用 iw 指出了这个motion,是inner word
# inner word也是狭义字<cword>,但是和 w 不同
# w 是从光标位置开始向后看
# 而inner word总是把光标移到第一个字母,从而总能得到一个完整的狭义字
# 试一试 gUw 和 gUiw 就知道区别了,呵呵。
# 在多个文档中搜索
:bufdo /searchstr
:argdo /searchstr
—————————————-
# 替换
# 译注:替换命令需要先进入:命令模式
:%s/fred/joe/igc : 一个常见的替换命令,修饰符igc和perl中一样意思
:%s//r//g : 删除DOS方式的回车^M
:%s= \(== : </font></span><span style="font-size: 9pt; font-family: 宋体;">删除行尾空白</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:'a,'bg/fred/s/dick/joe/igc : </font></span><span style="font-size: 9pt; font-family: 宋体;">非常有用!(</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">ft,</font></span><span style="font-size: 9pt; font-family: 宋体;">又来了!)</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">译释:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">'a,'b</font></span><span style="font-size: 9pt; font-family: 宋体;">指定一个范围:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">mark a ~ mark b<br /># g//</font></span><span style="font-size: 9pt; font-family: 宋体;">用一个正则表达式指出了进行操作的行必须可以被</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">fred</font></span><span style="font-size: 9pt; font-family: 宋体;">匹配</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">看后面,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">g//</font></span><span style="font-size: 9pt; font-family: 宋体;">是一个全局显示命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># s/dick/joe/igc</font></span><span style="font-size: 9pt; font-family: 宋体;">则对于这些满足条件的行进行替换</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">列复制</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">译注:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">@#%&^#*^%#\)!
:%s= [^ ]/+\(=&&= : </font></span><span style="font-size: 9pt; font-family: 宋体;">复制最后一列</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s= /f/+\)=&&= : 一样的功能
:%s= /S/+\(=&& : ft,</font></span><span style="font-size: 9pt; font-family: 宋体;">还是一样</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">反向引用,或称记忆</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:s//(.*/):/(.*/)//2 : /1/ : </font></span><span style="font-size: 9pt; font-family: 宋体;">颠倒用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">:</font></span><span style="font-size: 9pt; font-family: 宋体;">分割的两个字段</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/^/(.*/)/n/1//1\)/ : 删除重复行
# 非贪婪匹配,/{-}
:%s/^./{-}pdf/new.pdf/ : 只是删除第一个pdf
# 跨越可能的多行
:%s/<!–/_./{-}–>// : 又是删除多行注释(咦?为什么要说“又”呢?)
:help //{-} : 看看关于 非贪婪数量符 的帮助
:s/fred/<c-r>a/g : 替换fred成register a中的内容,呵呵
# 写在一行里的复杂命令
:%s//f/+/.gif/>//r&/r/g | v//.gif\(/d | %s/gif/jpg/<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">译注:就是用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> | </font></span><span style="font-size: 9pt; font-family: 宋体;">管道啦</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">或者</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/suck/|buck/loopy/gc : </font></span><span style="font-size: 9pt; font-family: 宋体;">或者</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(</font></span><span style="font-size: 9pt; font-family: 宋体;">或者需要</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">/</font></span><span style="font-size: 9pt; font-family: 宋体;">,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">ft</font></span><span style="font-size: 9pt; font-family: 宋体;">!,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">|</font></span><span style="font-size: 9pt; font-family: 宋体;">不是或者</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br /># ft, /</font></span><span style="font-size: 9pt; font-family: 宋体;">不就是转义了么!这个和</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">perl</font></span><span style="font-size: 9pt; font-family: 宋体;">真是不同了!</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">VIM</font></span><span style="font-size: 9pt; font-family: 宋体;">函数</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:s/__date__//=strftime("%c")/ : </font></span><span style="font-size: 9pt; font-family: 宋体;">插入时间串</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">处理列,替换所有在第三列中的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">str1<br />:%s:/(/(/w/+/s/+/)/{2}/)str1:/1str2:<br /><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">交换第一列和最后一列</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> (</font></span><span style="font-size: 9pt; font-family: 宋体;">共</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">4</font></span><span style="font-size: 9pt; font-family: 宋体;">列</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />:%s:/(/w/+/)/(.*/s/+/)/(/w/+/)\):/3/2/1:
# filter all form elements into paste register
# 把所有的form元素(就是html里面的form啦)放到register里?
# ft, 头疼,不解释了
:redir @|sil exec ‘g#</(input/|select/|textarea/|//=form/)/>#p’|redir END
:nmap ,z :redir @<Bar>sil exec ‘g@</(input/<Bar>select/<Bar>textarea/<Bar>//=fo
—————————————-
# 全局(global)显示命令,就是用 :g+正则表达式
# 译释: :g/{pattern}/{cmd} 就是全局找到匹配的行
# 然后对这些行执行命令{cmd}
:g//<fred/>/ : 显示所有能够为单词fred所匹配的行
:g/<pattern>/z#.5 : 显示内容,还有行号,呵呵
:g/<pattern>/z#.5|echo ”==========“ : 漂亮的显示,ft!
# 全局命令 (其他)
:g/^/s\(/d : </font></span><span style="font-size: 9pt; font-family: 宋体;">删除所有空行</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:g!/^dd/d : </font></span><span style="font-size: 9pt; font-family: 宋体;">删除不含字串</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">'dd'</font></span><span style="font-size: 9pt; font-family: 宋体;">的行</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:v/^dd/d : </font></span><span style="font-size: 9pt; font-family: 宋体;">同上</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">译释:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">v == g!</font></span><span style="font-size: 9pt; font-family: 宋体;">,就是不匹配!</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:g/fred/,/joe/d : not line based (very powerfull)<br />:v/./.,/./-1join : </font></span><span style="font-size: 9pt; font-family: 宋体;">压缩空行</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:g/^\)/,/./-j : 压缩空行
:g/<input/|<form/p : 或者 要用/|
:g/^/pu _ : 把文中空行扩增一倍 (pu = put)
: 即:原来两行间有一个空行,现在变成2个
:g/^/m0 : 按行翻转文章 (m = move)
:g/fred/t\( : </font></span><span style="font-size: 9pt; font-family: 宋体;">拷贝行,从</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">fred</font></span><span style="font-size: 9pt; font-family: 宋体;">到文件末尾</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(EOF)<br />:%norm jdd : </font></span><span style="font-size: 9pt; font-family: 宋体;">隔行删除</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">译释:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">% </font></span><span style="font-size: 9pt; font-family: 宋体;">指明是对所有行进行操作</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># norm</font></span><span style="font-size: 9pt; font-family: 宋体;">指出后面是</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">normal</font></span><span style="font-size: 9pt; font-family: 宋体;">模式的指令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># j</font></span><span style="font-size: 9pt; font-family: 宋体;">是下移一行,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">dd</font></span><span style="font-size: 9pt; font-family: 宋体;">是删除行</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman"># incrementing numbers<br />:.,\)g/^/d/exe ”norm! /<c-a>“ : 增加在BOL(beginning of line)处的数字
# 译注:.,\( </font></span><span style="font-size: 9pt; font-family: 宋体;">指明命令从当前行执行到最后一行</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">如果没有</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> .,\) 限定范围,那么g//就会对整个文件进行操作
# exe 是执行后面的命令组合
:.,$g/^/d/exe ”norm /<c-p>“ : Win32下必须重定义Ctrl-A
# 保存全局命令的结果 (注意必须使用添加模式)
:g/fred/y A : 添加所有为fred所匹配的行到register a
:’a,‘b g/^Error/ . w >> errors.txt
# 复制每一行,然后在复制出来的每一行两侧加上一个 print ’复制出来的内容‘
:g/./yank|put|-1s/’/”/g|s/./Print ‘&’/
—————————————-
# 全局命令和替换命令联姻 (强大的编辑能力)
:‘a,’bg/fred/s/joe/susan/gic : 可以使用反向引用来匹配
:g/fred/,/joe/s/fred/joe/gic : non-line based (ultra)
—————————————-
# 先找fred,然后找joe,然后#\(^\)%^#\(%^@%^%&%^*!<br />:/fred/;/joe/-2,/sid/+3s/sally/alley/gIC<br />----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">重定向到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register * </font></span><span style="font-size: 9pt; font-family: 宋体;">和</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">粘贴</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register *<br />:redir @* : </font></span><span style="font-size: 9pt; font-family: 宋体;">重定向命令的输出结果(最下方命令行上的结果)</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"> : </font></span><span style="font-size: 9pt; font-family: 宋体;">到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register * (ft,* </font></span><span style="font-size: 9pt; font-family: 宋体;">代表</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">0~1,a~z,..)<br />:redir END : </font></span><span style="font-size: 9pt; font-family: 宋体;">结束重定向</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">处理粘贴</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">"*yy : </font></span><span style="font-size: 9pt; font-family: 宋体;">上面讲过了,就是复制到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register *</font></span><span style="font-size: 9pt; font-family: 宋体;">中</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">"*p : </font></span><span style="font-size: 9pt; font-family: 宋体;">然后贴出来</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br />:redir >> out.txt : </font></span><span style="font-size: 9pt; font-family: 宋体;">重定向到一个文件</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">重新格式化文本</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">gq<CR><br />gqap (a</font></span><span style="font-size: 9pt; font-family: 宋体;">是</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">motion p</font></span><span style="font-size: 9pt; font-family: 宋体;">是段落</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(visual</font></span><span style="font-size: 9pt; font-family: 宋体;">模式</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">))<br />ggVGgq </font></span><span style="font-size: 9pt; font-family: 宋体;">重新格式化整个文章</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">对多个文档实施命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:argdo %s/foo/bar/ : </font></span><span style="font-size: 9pt; font-family: 宋体;">对所有</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">:args</font></span><span style="font-size: 9pt; font-family: 宋体;">列表中的文档执行命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:bufdo %s/foo/bar/<br />:windo %s/foo/bar/<br />:argdo exe '%!sort'|w! : </font></span><span style="font-size: 9pt; font-family: 宋体;">使用外部命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">命令行的一些好玩的东东</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">gvim -h : </font></span><span style="font-size: 9pt; font-family: 宋体;">启动的时候启动帮助</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(Win32)<br />vi -h </font></span><span style="font-size: 9pt; font-family: 宋体;">或</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> vim -h : </font></span><span style="font-size: 9pt; font-family: 宋体;">这个是</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">unix</font></span><span style="font-size: 9pt; font-family: 宋体;">下用</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">ls | gvim - : </font></span><span style="font-size: 9pt; font-family: 宋体;">编辑一个数据流!</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">gvim -o file1 file2 : </font></span><span style="font-size: 9pt; font-family: 宋体;">以分割窗口打开两个文件</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">指出打开之后执行的命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">gvim.exe -c "/main" joe.c : </font></span><span style="font-size: 9pt; font-family: 宋体;">打开</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">joe.c</font></span><span style="font-size: 9pt; font-family: 宋体;">,然后跳转到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">'main'<br /><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">对一个文件执行多个命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">vim -c "%s/ABC/DEF/ge | update" file1.c<br /><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">对一组文件执行多个命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">vim -c "argdo %s/ABC/DEF/ge | update" *.c<br /><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">自动编辑文件</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> (</font></span><span style="font-size: 9pt; font-family: 宋体;">编辑命令序列</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">Ex commands</font></span><span style="font-size: 9pt; font-family: 宋体;">已经包含在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">convert.vim</font></span><span style="font-size: 9pt; font-family: 宋体;">中了</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />vim -s "convert.vim" file.c<br /><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">不要加载</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">.vimrc</font></span><span style="font-size: 9pt; font-family: 宋体;">和任何</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">plugins (</font></span><span style="font-size: 9pt; font-family: 宋体;">启动一个干净的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">VIM)<br />gvim -u NONE -U NONE -N<br />----------------------------------------<br /># GVIM </font></span><span style="font-size: 9pt; font-family: 宋体;">不同的地方</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">gvim -d file1 file2 : vimdiff (</font></span><span style="font-size: 9pt; font-family: 宋体;">比较不同</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />dp : </font></span><span style="font-size: 9pt; font-family: 宋体;">把光标处的不同放到另一个文件</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">do : </font></span><span style="font-size: 9pt; font-family: 宋体;">在光标处从另一个文件取得不同</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># Vim</font></span><span style="font-size: 9pt; font-family: 宋体;">陷阱</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">vim</font></span><span style="font-size: 9pt; font-family: 宋体;">的正则表达式中,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> + </font></span><span style="font-size: 9pt; font-family: 宋体;">和</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> | </font></span><span style="font-size: 9pt; font-family: 宋体;">都必须加转义符</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> /<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">小心,这和</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">perl</font></span><span style="font-size: 9pt; font-family: 宋体;">不一样!</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/fred/+/ : </font></span><span style="font-size: 9pt; font-family: 宋体;">匹配</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">fred</font></span><span style="font-size: 9pt; font-family: 宋体;">或</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">freddy</font></span><span style="font-size: 9pt; font-family: 宋体;">但是不匹配</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">free<br />----------------------------------------<br /># /v </font></span><span style="font-size: 9pt; font-family: 宋体;">,或叫做</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">very magic (</font></span><span style="font-size: 9pt; font-family: 宋体;">通常都是这么叫</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)</font></span><span style="font-size: 9pt; font-family: 宋体;">可以取消转义符</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/codes/(/n/|/s/)*where : </font></span><span style="font-size: 9pt; font-family: 宋体;">普通的正则表达式</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">//vcodes(/n|/s)*where : very magic</font></span><span style="font-size: 9pt; font-family: 宋体;">,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">| </font></span><span style="font-size: 9pt; font-family: 宋体;">不用加</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> / </font></span><span style="font-size: 9pt; font-family: 宋体;">了!</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">把东西送到命令行</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">/</font></span><span style="font-size: 9pt; font-family: 宋体;">搜索行</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> (SUPER:</font></span><span style="font-size: 9pt; font-family: 宋体;">偶不再翻译这种叹词了</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br /><C-R><C-W> : </font></span><span style="font-size: 9pt; font-family: 宋体;">送一个狭义词</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"><C-R><C-A> : </font></span><span style="font-size: 9pt; font-family: 宋体;">送一个广义词</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"><C-R>- : </font></span><span style="font-size: 9pt; font-family: 宋体;">送一个小型删除寄存器</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register<br /><C-R>[0-9a-z] : </font></span><span style="font-size: 9pt; font-family: 宋体;">送一个命名寄存器</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register<br /><C-R>% : </font></span><span style="font-size: 9pt; font-family: 宋体;">送文件名过去</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> (#</font></span><span style="font-size: 9pt; font-family: 宋体;">也行</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">操作寄存器</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:let @a=@_ : </font></span><span style="font-size: 9pt; font-family: 宋体;">清除</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register a<br />:let @*=@a : </font></span><span style="font-size: 9pt; font-family: 宋体;">寄存器赋值</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:map <f11> "qyy:let @q=@q."zzz"<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">译注:猜猜这个无聊的绑定是什么意思?</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">关于帮助的帮助</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:h quickref : </font></span><span style="font-size: 9pt; font-family: 宋体;">翻到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">VIM Quick Reference</font></span><span style="font-size: 9pt; font-family: 宋体;">页</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(</font></span><span style="font-size: 9pt; font-family: 宋体;">有用!</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />:h tips : Vim</font></span><span style="font-size: 9pt; font-family: 宋体;">自己的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">tips<br />:h visual<C-D><tab> : </font></span><span style="font-size: 9pt; font-family: 宋体;">得到一个关于</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">visual</font></span><span style="font-size: 9pt; font-family: 宋体;">关键字的帮助列表</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"> : </font></span><span style="font-size: 9pt; font-family: 宋体;">然后用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">tab</font></span><span style="font-size: 9pt; font-family: 宋体;">键去选择</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:h ctrl<C-D> : </font></span><span style="font-size: 9pt; font-family: 宋体;">显示所有关于</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">Ctrl</font></span><span style="font-size: 9pt; font-family: 宋体;">的帮助</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:h :r : :ex</font></span><span style="font-size: 9pt; font-family: 宋体;">冒号命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:h CTRL-R : </font></span><span style="font-size: 9pt; font-family: 宋体;">普通模式命令</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:h /r : /r</font></span><span style="font-size: 9pt; font-family: 宋体;">在正则表达式中是什么意思呢?</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:h i_CTRL-R : insert</font></span><span style="font-size: 9pt; font-family: 宋体;">模式下的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">Ctrl-R<br />:h c_CTRL-R : </font></span><span style="font-size: 9pt; font-family: 宋体;">命令行</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(command-line)</font></span><span style="font-size: 9pt; font-family: 宋体;">模式下的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">Ctrl-R<br />:h v_CTRL-V : visual</font></span><span style="font-size: 9pt; font-family: 宋体;">模式下的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">Ctrl-V<br />:h tutor : VIM </font></span><span style="font-size: 9pt; font-family: 宋体;">指南</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">gvim -h : </font></span><span style="font-size: 9pt; font-family: 宋体;">关于</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> VIM </font></span><span style="font-size: 9pt; font-family: 宋体;">命令的帮助</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">vi/vim -h<br /><C-S>T : Control Shift T go backwards in help<br /> : </font></span><span style="font-size: 9pt; font-family: 宋体;">偶不清楚有什么用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">:(<br />----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">选项设置在哪里?</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:scriptnames : </font></span><span style="font-size: 9pt; font-family: 宋体;">列出所有加载的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> plugins, _vimrcs<br />:verbose set history : </font></span><span style="font-size: 9pt; font-family: 宋体;">显示</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">history</font></span><span style="font-size: 9pt; font-family: 宋体;">的值并指出设置文件的位置</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">制作你自己的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">VIM</font></span><span style="font-size: 9pt; font-family: 宋体;">帮助</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:helptags /vim/vim61/doc : </font></span><span style="font-size: 9pt; font-family: 宋体;">重建</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> /doc </font></span><span style="font-size: 9pt; font-family: 宋体;">中所有的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> *.txt </font></span><span style="font-size: 9pt; font-family: 宋体;">帮助文件</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:help add-local-help<br />----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">用外部程序来运行程序</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> (</font></span><span style="font-size: 9pt; font-family: 宋体;">例如</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> perl :)<br />map <f2> :w<CR>:!perl -c %<CR><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">译释:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">:w<CR></font></span><span style="font-size: 9pt; font-family: 宋体;">写文件</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># :!perl -c %<CR></font></span><span style="font-size: 9pt; font-family: 宋体;">用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">perl</font></span><span style="font-size: 9pt; font-family: 宋体;">来运行当前文件</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">当前文件必须有文件名!</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">插入</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">DOS</font></span><span style="font-size: 9pt; font-family: 宋体;">换行符</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/nubian/<C-V><C-M>&/g : Ctrl-V</font></span><span style="font-size: 9pt; font-family: 宋体;">是一种转义,它说要解释</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"><C-M><br />:%s/nubian/<C-Q><C-M>&/g : </font></span><span style="font-size: 9pt; font-family: 宋体;">对于</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">Win32</font></span><span style="font-size: 9pt; font-family: 宋体;">应该这样</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/nubian/^M&/g : </font></span><span style="font-size: 9pt; font-family: 宋体;">你看到的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">^M</font></span><span style="font-size: 9pt; font-family: 宋体;">是一个字符</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/nubian//r&/g : </font></span><span style="font-size: 9pt; font-family: 宋体;">更好的形式</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">把最后一个命令贴到当前位置</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">i<c-r>:<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">把最后一个搜索指令贴到当前位置</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">i<c-r>/<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">译释:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">i</font></span><span style="font-size: 9pt; font-family: 宋体;">是进入</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">insert</font></span><span style="font-size: 9pt; font-family: 宋体;">模式,</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># Ctrl-r</font></span><span style="font-size: 9pt; font-family: 宋体;">是开启插入模式下</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register</font></span><span style="font-size: 9pt; font-family: 宋体;">的引用</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># :</font></span><span style="font-size: 9pt; font-family: 宋体;">和</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">/</font></span><span style="font-size: 9pt; font-family: 宋体;">分别引用了两个</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">register</font></span><span style="font-size: 9pt; font-family: 宋体;">的内容</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">更多的完成功能</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"><C-X><C-F> :</font></span><span style="font-size: 9pt; font-family: 宋体;">插入当前目录下的一个文件名到当前位置</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">insert</font></span><span style="font-size: 9pt; font-family: 宋体;">模式下使用</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">然后用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> Ctrl-P/Ctrl-N </font></span><span style="font-size: 9pt; font-family: 宋体;">翻页</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">替换一个</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">visual</font></span><span style="font-size: 9pt; font-family: 宋体;">区域</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"># </font></span><span style="font-size: 9pt; font-family: 宋体;">选择一个区域,然后输入</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> :s/Emacs/Vim/ </font></span><span style="font-size: 9pt; font-family: 宋体;">等等,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">vim</font></span><span style="font-size: 9pt; font-family: 宋体;">会自动进入</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">:</font></span><span style="font-size: 9pt; font-family: 宋体;">模式</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:'<,'>s/Emacs/Vim/g : </font></span><span style="font-size: 9pt; font-family: 宋体;">前面的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">'<,'></font></span><span style="font-size: 9pt; font-family: 宋体;">是</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">vim</font></span><span style="font-size: 9pt; font-family: 宋体;">自动添加的</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">在文件中插入行号</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(</font></span><span style="font-size: 9pt; font-family: 宋体;">不是显示行号,是插入!</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />:g/^/exec "s/^/".strpart(line(".")." ", 0, 4)<br />----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">VIM</font></span><span style="font-size: 9pt; font-family: 宋体;">的方式来编号行</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman">:set number :</font></span><span style="font-size: 9pt; font-family: 宋体;">显示行号</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:set nonu :</font></span><span style="font-size: 9pt; font-family: 宋体;">取消显示</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/^//=strpart(line('.')." ",0,&ts)<br /><br />#</font></span><span style="font-size: 9pt; font-family: 宋体;">从任意行开始编号</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(</font></span><span style="font-size: 9pt; font-family: 宋体;">需要</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">perl</font></span><span style="font-size: 9pt; font-family: 宋体;">,嘿嘿</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)<br />:'a,'b!perl -pne 'BEGIN{\)a=223} substr(\(_,2,0)=\)a++‘
#似乎有点小问题,你试试看:)
mnYP`n^Aq : 记录到q 然后用 @q 重复
#似乎不能工作,你试试看:)
# 递增已存在数字到文件末
:.,\(g/^/d/exe "normal! /<c-a>"<br /><br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">高级递增,看:</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">http://vim.sourceforge.net/tip_view.php?tip_id=150<br />----------------------------------------<br /># </font></span><span style="font-size: 9pt; font-family: 宋体;">高级递增</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> ("</font></span><span style="font-size: 9pt; font-family: 宋体;">真的很有用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">"</font></span><span style="font-size: 9pt; font-family: 宋体;">,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">ft)<br />" </font></span><span style="font-size: 9pt; font-family: 宋体;">把下面几句放到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> _vimrc #vimrc</font></span><span style="font-size: 9pt; font-family: 宋体;">脚本用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> " </font></span><span style="font-size: 9pt; font-family: 宋体;">做行注释符</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">let g:I=0<br />function! INC(increment)<br />let g:I =g:I + a:increment<br />return g:I<br />endfunction<br />" </font></span><span style="font-size: 9pt; font-family: 宋体;">例如从</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">mark a </font></span><span style="font-size: 9pt; font-family: 宋体;">到</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">mark b </font></span><span style="font-size: 9pt; font-family: 宋体;">递增,从</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">223</font></span><span style="font-size: 9pt; font-family: 宋体;">开始,步长为</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">5<br />:let I=223<br />:'a,'bs/\)//=INC(5)/
“ (原文:create a map for INC)
” 但是cab是清楚命令行缩写啊?怎么回事?
cab viminc :let I=223 /| ’a,‘bs/$//=INC(5)/
—————————————-
# 加密(小心使用,不要忘了密码)
:X : 然后vim会提示你输入密码
:h :X
—————————————-
# 模式行(modeline)
# 第二版新加,感谢tcpip
vim:noai:ts=2:sw=4:readonly: #让文档只读
# 译释:这一行必须以vim:开头,而且只能在文档的前5行或后5行之内
# 后面是需要执行的命令,依次是:
# noai noautoindent
# ts=2 tabstop=2
# sw=4 shiftwidth=4
# readonly readonly
:h modeline #看看关于modeline的帮助先!
—————————————-
# Creating your own GUI Toolbar entry
# 对于text模式下的vim没用,不翻了
amenu Modeline.Insert/ a/ VIM/ modeline <Esc><Esc>ggOvim:ff=unix ts=4 ss=4<CR>v
—————————————-
# 一个保存当前光标下的狭义字到一个文件的函数
function! SaveWord() “这里用!是强制覆盖以前的定义
normal yiw
exe ’:!echo ‘.@0.’ >> word.txt‘
endfunction
map ,p :call SaveWord() #使用该函数的一个例子
—————————————-
# 删除重复行的函数
function!
Del 讯享网
()
if getline(”.“) == getline(line(”.“) - 1)
norm dd
endif
endfunction
:g/^/ call Del() #使用该函数的一个例子
—————————————-
# 双字节编码 (non alpha-numerics)
:digraphs : 显示编码表
:h dig : 帮助
i<C-K>e’ : 输入 é
i<C-V>233 : 输入 é (Unix)
i<C-Q>233 : 输入 é (Win32)
ga : 查看字符的hex值
—————————————-
# 文件名自动完成 (例如 mainc.c)
:e main<tab> : tab 键完成
gf : 打开光标处广义字命名的文件 (normal模式)
main_<C-X><C-F> : 文件名自动完成(insert模式)
—————————————-
# Vim复杂使用
# 交换两个单词
:%s//</(on/|off/)/>//=strpart(”offon“, 3 (”off“ == submatch(0)), 3)/g
—————————————-
# 把text文件转换成html文件(oh,ft)
:runtime! syntax/2html.vim : 转换 txt 成 html
:h 2html : 看看帮助
—————————————-
# VIM 有一个内部自带的 grep 命令
:grep some_keyword .c : 得到一个包含some_keyword的c文件名列表
:cn : 去下一个出现的位置
—————————————-
# 强制无后缀文件的语法着色方式 .pl
:set syntax=perl
# 取消语法着色
:set syntax off
# 改变色彩主题 (在~vim/vim??/colors中的任何文件)
:colorscheme blue
—————————————-
:set noma (non modifiable) : 防止修改
:set ro (Read Only) : 只读保护
—————————————-
# Sessions (打开一系列文件)
gvim file1.c file2.c lib/lib.h lib/lib2.h :在”session“中加载一系列文件
:mksession : 生成一个Session文件 (默认是Session.vim)
:q
gvim -S Session.vim : 重新读取一个session,也就读取了所有文件,ft
—————————————-
# 标记(tags) (跳转到subroutines/functions)
taglist.vim : 最流行的插件
:Tlist : 显示Tags (functions的列表)
<C-]> : 跳转到光标处的function
: 这个键 Ctrl-] 和vim帮助中是一样的
—————————————-
# Just Another Vim Hacker JAVH
# Juat Another Perl Hacker JAPH,嘿嘿
vim -c ”:%s/^/WhfgTNabgureRIvzSUnpxre/|:%s/[R-T]/ /Ig|:normal ggVGg?“
# 译释:呵呵,谁来解释一下吧!
# 其实不过是在启动vim的时候执行了一个命令
# 先写入了 Just Another Vim Hacker 的rot13编码
# 然后再解码
—————————————-
终于翻完了,呵呵。好累啊!
前言
工欲善其事,必先利其器。一个programmer必然要先有一个好的editor。
vim就是一个十分强大的编辑器。它的强大之处,在于其个性化和可定制。
学习vim,就像学习Linux,学习perl,你发现你可以让它来适应自己,
你发现你只需要学一点点就可以工作了;而当你继续学习下去,你会
惊奇的发现它的“新”功能能够极大的提高你的工作效率;就是这样,
你学习的兴趣将始终超过学习的难度,因此促使你一直钻研下去……
另外一点,你总是可以找到vim,至少是vi,可是你可能不会在你的公司
的Sun机器上找到记事本,也可能不会在AIX上找到emacs——除非你是root,
你可以自己装一个:)
我无意把vim和其他编辑器比较,我只知道Larry Wall用vim。
言归正传,个性化vim最基本也是最重要的就是编写vimrc文件(不要说
你不知道vimrc在哪里,它就在你的home目录下,名字叫做.vimrc)。
1. 注释
写程序之前,第一件事情不是了解语法,而是知道如何写注释。
vimrc脚本的注释是使用引号(”)作行注释。
2. 变量
(1) 标量变量
可以是数字或字符串,基本与perl相同。
命名方式为:作用域:变量名,常用的有如下几种:
b:name —— 只对当前buffer有效的变量
w:name —— 只对当前窗口有效的变量
g:name —— 全局变量
v:name —— vim预定义变量
a:name —— 函数的参变量
注意:引用标量变量的时候请包含作用域和冒号
(2) 一类有特殊含义的变量
命名方式:Fun Character(这个词请参看Programming Perl)加上变量名
共有三类:
\(NAME —— </font></span><span style="font-size: 9pt; font-family: 宋体;">环境变量(一般变量名都是大写)</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"> &name —— </font></span><span style="font-size: 9pt; font-family: 宋体;">选项(</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">vim</font></span><span style="font-size: 9pt; font-family: 宋体;">处理某些事情的时候的默认设置)</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"> @r —— register</font></span><span style="font-size: 9pt; font-family: 宋体;">(寄存器,不是汇编的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">EAX,EBX</font></span><span style="font-size: 9pt; font-family: 宋体;">,看第</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">2</font></span><span style="font-size: 9pt; font-family: 宋体;">部分</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">vim tips</font></span><span style="font-size: 9pt; font-family: 宋体;">)</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">常见环境变量例子:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">\)VIMRUNTIME —— vim运行路径
常见选项例子:&ic —— ignorecase
注:使用set命令可以改变选项设置,例如:
:set ignorecase
使用一个set命令可以看到当前所有的选项及其设置。
(3) 变量赋值
:let 变量名=值
注意:最前面的冒号不仅是为了表示这是一个冒号命令,而且是必须的。
释放变量::unlet! 变量名
(4) 运算符(和perl基本一样)
数学运算:+ - / % .
逻辑运算:== != > >= < <= ?:
正则匹配运算符= !
3. 控制结构
(1) if 条件
语句块
elseif 条件
语句块
else
语句块
endif
注意:条件表达式不需要小括号,语句块不需要大括号
(2) while 条件
语句块
[break/continue]
endwhile
4. 函数:
定义:
function 函数名(参数)
函数体
endfunc
调用:
在脚本语句中使用 call 函数名(参数)
在vim命令中使用 :call 函数名(参数)
注:在函数体中使用参数需要在参数变量名称前加上a:,例如参数名为
keyword,
则函数体中使用a:keyword来引用
注:常用系统函数 参见【附】。
5. 执行命令,键盘绑定,命令行命令和自动命令
(1) 执行命令
exec “命令” —— 用于在vim脚本中执行一系列vim命令
:!外部命令 —— 这是一个vim命令行命令,功能是调用外部程序
(2) 键盘绑定 :help map-overview
vim最大的特点在于可以把所有的操作能够用一个命令字符串表达出来,
因此这带来了编写脚本的最大的便利。键盘绑定就是一个例子,这个功能允许
把一个命令字符串绑定到一个按键/按键组合。
一般格式:映射命令 按键组合 命令组合
例子:nmap c ^i#<Esc>j
解释:映射normal模式下的按键c为:^i#<Esc>j,就是在该行开头加上#号
,然后下移一行
常用映射命令:
map :全模式映射
nmap :normal模式映射
vmap :visual模式映射
imap :insert模式映射
(3) 命令行命令
vim支持在启动的时候使用-c开关执行命令字符串,例如:
\( cat n<br /> #!/bin/sh<br /> vim -c "set filetype=\)PERL“ -c ”s.\(.#!/usr/bin/\)PERL -w/r/r.“ -c
”:nohlsearch“ \(1<br /> </font></span><span style="font-size: 9pt; font-family: 宋体;">设置文件类型</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">写入</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">#!/usr/bin/perl -w <br /> </font></span><span style="font-size: 9pt; font-family: 宋体;">取消匹配加亮</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman"> \) n myperlfile
(4) 自动命令
一般格式:autocmd 事件 文件类型 命令
例子:au BufNewFile,BufRead .pl setf perl
解释:当事件 BufNewFile 和 BufRead 发生在 .pl 文件上的时候,
执行命令:setf perl
========================================================================
【附】常用系统函数 :help function-list
函数 返回值 解释
(1)字符串操作
char2nr( {expr}) Number {expr}中字符的ASCII值
nr2char( {expr}) String {expr}中ASCII值对应的字符
escape( {string}, {chars}) String 使用‘/’对{string}中的字符
{chars}进行转义
strtrans( {expr}) String 把字符串转换成可打印字符串
tolower( {expr}) String 将字符串{expr}小写
toupper( {expr}) String 将字符串{expr}大写
match( {expr}, {pat}[, {start}])
Number {pat}在{expr}中的匹配位置
matchend( {expr}, {pat}[, {start})
Number {pat}在{expr}中的最后匹配位
置
matchstr( {expr}, {pat}[, {start}])
String {pat}在{expr}中的匹配,成功
返回{pat},失败返回”“
stridx( {haystack}, {needle}) Number {needle}在{haystack}中的始
索引位置
strridx( {haystack}, {needle}) Number {needle}在{haystack}中的终
索引位置
strlen( {expr}) Number 字符串{expr}的长度
substitute( {expr}, {pat}, {sub}, {flags})
String 用{sub}替换{expr}中的模式
{pat}
submatch( {nr}) String ”:substitute“的特殊匹配
strpart( {src}, {start}[, {len}])
String 子串函数,在{src}中从
{start}开始取出{len}个字符
expand( {expr}) String 扩展{expr}中的字符串
type( {name}) Number 变量{name}的类型
(2)操作当前缓冲区中的文本
byte2line( {byte}) Number 字节数为{byte}的行
line2byte( {lnum}) Number 行号为{lnum}的行的字节数
col( {expr}) Number {expr}的列号:. 光标所在列
$ 末列 ”x 标记x所在位置
virtcol( {expr}) Number {expr}的屏幕列号(screen
column)
line( {expr}) Number {expr}的行号
wincol() Number {expr}的窗口列号(screen
column)
winline() Number {expr}的窗口行号(screen
column)
getline( {expr}) Number {expr}的行号
setline( {lnum}, {line}) Number 把{line}写入{lnum}行
append( {lnum}, {string}) Number 在{lnum}行下一行加入
{string}
indent( {lnum}) Number 缩进行{lnum}
cindent( {lnum}) Number C格式缩进行{lnum}
lispindent( {lnum}) Number Lisp格式缩进行{lnum}
nextnonblank( {lnum}) Number 行号 >= {lnum} 的非空白行的
行号
prevnonblank( {lnum}) Number 行号 <= {lnum} 的非空白行的
行号
search( {pattern} [, {flags}]) Number 查找{pattern}
searchpair( {start}, {middle}, {end} [, {flags} [, {skip}]])
Number 查找 始/末对 的另一个末端
(3)系统函数和文件操作
browse( {save}, {title}, {initdir}, {default})
String 启动一个文件请求
glob( {expr}]) String 展开一个fileglob {expr}
globpath( {path}, {expr}) String 对{path}中的所有目录调用
glob({expr})
resolve( {filename}) String 得到符号链接的指向
fnamemodify( {fname}, {mods}) String 更改文件名
executable( {expr}) Number 如果{expr}存在且可执行,则
真
filereadable( {file}) Number 如果{file}可读,则真
isdirectory( {directory}) Number 如果{directory}存在,则真
getcwd() String 当前工作目录
getfsize( {fname}) Number 文件字节数
getftime( {fname}) Number 文件最终修改时间
localtime() Number 当前时间
strftime( {format}[, {time}]) String 格式化的时间
tempname() String 生成一个临时文件名
delete( {fname}) Number 删除文件{fname}
rename( {from}, {to}) Number 重命名{from}成{to}
system( {expr}) String 执行shell命令{expr}
hostname() String 机器名
(4)缓冲区,窗口,参数列表
argc() Number 参数列表中的文件个数
argidx() Number 参数列表中当前索引值
argv( {nr}) String 参数列表中第{nr}个
bufexists( {var}) Number 如果{var}存在则真
buflisted( {expr}) Number 如果{expr}被列出则真
bufloaded( {expr}) Number 如果{expr}被加载则真
bufname( {expr}) String 缓冲{expr}的名字
bufnr( {expr}) Number 缓冲{expr}的缓冲序号
winnr() Number 当前窗口的窗口序号
bufwinnr( {expr}) Number 指定缓冲的窗口序号
winbufnr( {nr}) Number 指定窗口的缓冲序号
getbufvar( {expr}, {varname}) 得到缓冲{expr}的特殊变量
{varname}的值
setbufvar( {expr}, {varname}, {val}) 设置缓冲{expr}的特殊变量
{varname}的值为{val}
getwinvar( {nr}, {varname}) 得到窗口{nr}的特殊变量
{varname}的值
setwinvar( {nr}, {varname}, {val}) 设置窗口{nr}的特殊变量
{varname}的值为{val}
(5)折叠(Folding)
foldclosed( {lnum}) Number 如果{lnum}行的折叠闭合了,
返回第一行
foldlevel( {lnum}) Number {lnum}行的折叠级数
foldtext( ) String 生成一个闭合折叠的表示
(6)语法加亮
hlexists( {name}) Number 如果命名为{name}的高亮组存
在,则真
hlID( {name}) Number 名字为{name}的高亮组的语法
ID
synID( {line}, {col}, {trans}) Number {line}行{col}列的语法ID
synIDattr( {synID}, {what} [, {mode}])
String 返回语法ID {synID}的{what}
属性
synIDtrans( {synID}) Number 翻译的语法ID {synID}
(7)历史
histadd( {history},{item}) String 向历史中增加一项
histdel( {history} [, {item}]) String 在历史中删除一项
histget( {history} [, {index}]) String 在历史中取出索引{index}的项
histnr( {history}) Number 历史项最大索引
(8)交互
confirm( {msg} [, {choices} [, {default} [, {type}]]])
Number 返回用户选择的项序号
getchar( [expr]) Number 获得一个输入字符
getcharmod( ) Number 修改最后一个输入的字符
input( {prompt} [, {text}]) String 获得用户输入
inputsecret( {prompt} [, {text}]) String 获得用户输入,但是不回显
inputdialog( {prompt} [, {text}]) String 产生一个GUI对话框以获得用
户输入
(9)Vim服务器
serverlist() String 返回一个可用的服务器列表
remote_send( {server}, {string} [, {idvar}])
String 送出key序列
remote_expr( {server}, {string} [, {idvar}])
String 送出表达式
server2client( {serverid}, {string})
Number 送出回复串
remote_peek( {serverid} [, {retvar}])
Number 检查回复串
remote_read( {serverid}) String 读取回复串
foreground( ) Number 把vim窗口带到前台
remote_foreground( {server}) Number 把vim服务器带到前台
(10)变量检查
mode() String 返回当前编辑方式
visualmode() String 返回最后一次visual模式的使
用
hasmapto( {what} [, {mode}]) Number 如果存在{what}的映射则真
mapcheck( {name}[, {mode}]) String 检查所有被{name}匹配的映射
名称
maparg( {name}[, {mode}]) String 在模式{mode}下的映射{name}
的rhs值
exists( {var}) Number 如果{var}存在则真
has( {feature}) Number 如果特性{feature}被支持则真
cscope_connection( [{num} , {dbpath} [, {prepend}]])
Number 检查是否存在cscope连接
did_filetype( {name}) Number 设置文件类型的自动命令
{name}存在则真
eventhandler( ) Number 如果在一个事件处理中则真
getwinposx() Number vim窗口在GUI模式下的X轴坐标
象素数
getwinposy() Number vim窗口在GUI模式下的Y轴坐标
象素数
winheight( {nr}) Number 窗口{nr}的高度
winwidth( {nr}) Number 窗口{nr}的宽度
libcall( {lib}, {func}, {arg}) String 使用参数{arg}调用在{lib}中
的函数{func}
libcallnr( {lib}, {func}, {arg})
Number 和上一个函数相同,用于返回
整数的函数
写一点vim常用的技巧,
主要是其他讲vim的文章不太讲的。
1. %
%用来匹配块,
如果你的光标在类似([{}])
或者#ifdef #else #endif上
%将把光标跳转到相应的匹配符号上去,
%还可以用来指定命令范围,
如果你想把一个
{
..
…
}的块全部删除。
可以先把光标移到{ 再敲d%
类似的,
如果你想把一个块全部往里缩进一个tab
可以把光标移到 { 敲>%
2. =
=是用来缩进的假设你已经在.vimrc里
设置了你的缩进格式,
你就可以用=来缩进你的代码了
=%就是缩进一个块。
3.正则表达式
正则表达式大家都清楚,
我主要讲个一般人不太用,
但很有用的表达,
例如你想把所有的“…”形式的串替换成‘…’的形式
但引号里的内容不变
你就可以用
%s/“/(./)”/‘/1’/来做
上面这个正则表达式“/(.*/)”里 /用来表示()是元字符
第一个在括号里的被匹配的串就可以被/1来代表, 以后依次是/2 /3。
顺便说一句,我到现在还不
知道怎么限制vim里正则表达匹配的贪婪算法。
————————————
里面说的非贪婪匹配是 /{-},
也就是 %s/“/(./{-}/)”/‘/1’/g
/ 太多了可以用 /v,
%s//v“(.{-})”/‘/1’/g
详细
:h //{-
:h /v
另外 和 perl 正则表达式的区别在 :h perl-patterns
1.
fx
x 表示任何一个字符。
这是最快的在一行种移动的方法了。然后用
; (分号)
继续移动。
反向移动好像是 t ,但是我记性不好,
总是记不住,于是
0fx
用 0 先回到行首,在 f
2.
任何一个操作命令在加一个移动命令。实现对某个范围的操作。
例如
dfx
表示删除到下一个出现 x 的地方, x 可以使任意字符。
操作命令有 d (cut), y(copy) , p(paste), v (select)
移动命令有 hjkl, f, /, gg, G
3.
任何命令组合都可以先按一些 数字健 表示重复操作。
如:
d123j
删除下面123行。
4. 宏纪录的功能
例如,把文件中所有奇数行和偶数行合并。按如下序列
gg
J
q
@q
5. C-p
在 insert mode 下
写程序的时候,任何超长的变量名字都不成问题。
如果有一个this_is_very_long_variable_name
可以
this<C-P>
90%的可能性自动就把名字补全了。
于是,我写程序的时候,变量的名字其的越来越长,输入越来越快。:)
6. C-x C-p
在写程序 abc.h 的时候
写道 #include “abc.h” 的时候
其实可以
#include “a<C-x><C-p>”
90% 的情况,可以自动补全文件名称。
6.从外部命令输入
:r !ls
可以读取当前目录的文件列表。
如果你对 bash 很熟悉的话,这个功能非常好用
例如
输入
case 1
case 2
….
case 1000:
的方法是
:r !for ((i=0;i<100;i++));do echo “case \(i" ; done<br /><br />7. </font></span><span style="font-size: 9pt; font-family: 宋体;">利用外部命令处理文字。</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">我在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> ~/.vimrc </font></span><span style="font-size: 9pt; font-family: 宋体;">中写了一行。</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">map = ggVG:!indent -nut -st -kr 2>/dev/null<CR>G<br /><br /></font></span><span style="font-size: 9pt; font-family: 宋体;">我按一个</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> = </font></span><span style="font-size: 9pt; font-family: 宋体;">,就可以利用外部命令</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> indent </font></span><span style="font-size: 9pt; font-family: 宋体;">美化</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">我的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> c </font></span><span style="font-size: 9pt; font-family: 宋体;">程序。</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">我认为,</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">还可以用外部命令排序</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">例如</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> v </font></span><span style="font-size: 9pt; font-family: 宋体;">选定要排序的区域</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">然后按一个叹号。</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:'<,'>!sort <br /><br />8<br /></font></span><span style="font-size: 9pt; font-family: 宋体;">我在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> ~/.vimrc </font></span><span style="font-size: 9pt; font-family: 宋体;">中写了</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">map <Left> :bp<CR><br />map <Right> :bn<CR><br />map <F4> :bd<CR><br /><br /></font></span><span style="font-size: 9pt; font-family: 宋体;">就可以用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">左右方向健来切换</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> buffer<br />F4 </font></span><span style="font-size: 9pt; font-family: 宋体;">关闭</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> buffer </font></span><span style="font-size: 9pt; font-family: 宋体;">了。</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman">9<br /></font></span><span style="font-size: 9pt; font-family: 宋体;">我在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> ~/.vimrc </font></span><span style="font-size: 9pt; font-family: 宋体;">中写了</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">runtime ftplugin/man.vim<br /><br /></font></span><span style="font-size: 9pt; font-family: 宋体;">就可以在把光标停在一个函数上,然后用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> <br />/k<br /></font></span><span style="font-size: 9pt; font-family: 宋体;">查看在线帮助了。</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">用</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:Man getuid<br /></font></span><span style="font-size: 9pt; font-family: 宋体;">查看</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> getuid </font></span><span style="font-size: 9pt; font-family: 宋体;">函数的手册了。</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman">10.<br />:make <br /></font></span><span style="font-size: 9pt; font-family: 宋体;">可以用外部命令</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> make </font></span><span style="font-size: 9pt; font-family: 宋体;">编译工程。</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:cw <br /></font></span><span style="font-size: 9pt; font-family: 宋体;">查看出错信息,</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:cn<br />:cp<br /></font></span><span style="font-size: 9pt; font-family: 宋体;">在出错信息之间跳转。</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /><font face="Times New Roman">11.<br />]] , [[ , [] , ][ </font></span><span style="font-size: 9pt; font-family: 宋体;">命令可以在函数之间移动。</span><span lang="EN-US" style="font-size: 9pt;"> </span></p> <p style="margin: 0cm 0cm 0pt;" class="MsoNormal"><span style="font-size: 9pt; font-family: 宋体;">我先说几个</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">标记文本</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">mchar</font></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">用字母</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">char</font></span><span style="font-size: 9pt; font-family: 宋体;">标记当前光标的位置</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">`char </font></span><span style="font-size: 9pt; font-family: 宋体;"> 移至</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">char</font></span><span style="font-size: 9pt; font-family: 宋体;">所标记处</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">'char</font></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">移至</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">char</font></span><span style="font-size: 9pt; font-family: 宋体;">标记所在行的开头处</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">"</font></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">移至当前行上一次所在位置(在光标移动之后)</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">――</font></span><span style="font-size: 9pt; font-family: 宋体;">一个双引号</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;"> </span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">''</font></span><span style="font-size: 9pt; font-family: 宋体;"> 移至当前行上第一次所在位置的行的开头处</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">(</font></span><span style="font-size: 9pt; font-family: 宋体;">在光标移动之后</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">)―<br /> ―</font></span><span style="font-size: 9pt; font-family: 宋体;">两个单引号</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">清理掉</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">DOS</font></span><span style="font-size: 9pt; font-family: 宋体;">文本中的</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">^M<br /> </font></span><span style="font-size: 9pt; font-family: 宋体;">可以在:</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">1,\)s/^M//g,其中^是用CTRL+v上去的M是回车形成的
—————-
编辑数个文件(利用vi filename(s))进入vi后)
:args 显示编辑名
单中的各个文件名
:n ?
3;读入编辑名单中的下一个文件
:rew 读入
编辑名单中的第一个文件
:e# 读入
编辑名单内的前一个文件
:e file 读入另一个文件进
vi(此文件可不在编辑名单内),若原文件经修改还没有存档,则
应先以: w 存档。
:e! file 强迫读入另一个文
件进入vi,原文件不作存档动作。
—————-
>>
<<
移动整行的命令。类似于delphi的ctrl+shift+I/ctrl+shift+U
在调整大段代码时很方便。
设置在.exrc中
sw=4
—————-
在vim中ctrl-v开始列编辑。
:help 可以看vim的使用手册。很丰富的。
—————-
快速块复制
用m char标识某行(如 mb)
移动到复制的起始行,y‘b,就可以复制整块
将整块复制到制定的‘寄存器’ ”qy’b
将寄存器中的数据插入到某位置 “qp
在寄存器中保存的块在整个vi过程中有效(包括用e 重新编辑其他文件)
—————-
shift +G 跳到文件尾
?char 从后往前查找字符串
/ char 从前往后查找字符串
—————-
% - 移至匹配的括号
xp - 交换两个字符
y[cursor movement] - 光标移多少复制多少,如yw, y2w, y2l
. - repeat last command
q<reg> - 开始录制宏,存入<register>Register中。
@<reg> - 回放<reg>中的宏
—————-
偶来说几个(vim):
”*p, 把文本拷贝到系统剪贴板, 可以在别的程序窗口中粘贴
计算器^^:
(例):在插入模式下输入 35*45=CTRL_R=35*45回车,结果为35*45=1575
(输入CTRL_R=后光标会跳到底行,应继续输完,你也可以在此输入
任意四则运算)
快速复制上一行(或下一行):
(插入模式下)输完一行后回车,在下面一行按住CTRL_Y不放,直到复制完上一行^^
CTRL_E为复制下一行;
快速插入已输入过的单词:
(例):如果已经在文本中输入过hello, 再次输hello的时候,在输到he的时候
按CTRL_P就可补全hello, CTRL_N为向后搜索。
格式化程序段:
假如你拿到别人的c代码,里面没有代码缩进怎么办?
首先确保你的vim cindent选项打开了(:set cindent) ,
然后在命令模式下按 gg=G 就全部缩进好了。
—————-
保存退出 :x
#vi +10 program.c
直接转到文件第十行
—————-
交换两个字符位置
xp
上下两行调换
ddp
把文件内容反转
:g/^/m0/
—————-
:X
然后系统会提示输入密码
输入密码后存盘退出
下次编辑时请使用
vi -x filename
系统会提示你输入密码
—————-
:set nu 在每行行首加上行号
:set nonu 和上面的相反
—————-
:!command 暂时退出vi并执行shell指
令,执行完毕后再回到vi。
:sh 暂时
退出vi到系统下,结束时按Ctrl + d则回到vi。
vi filename 进入vi并读入指定名称的文件(
新、旧文件均可)。
vi +n filename 进入vi并且由文件的第几行开始。
vi +filename 进入vi并且由文件的最后一行开始。
vi + /word filename 进入vi并且由文件的word这个字开始。
vi filename(s) 进入vi并且将各指定文件列入名单内,第一个
文件先读入。
—————-
vi -r filename 恢复被毁坏的文件
—————-
在查找、替换命令 使用以下正则表达式元字符,功能强大。
也可用在:g/命令中
& 代表最近匹配串
~ 代表最近替换串
. 任一字符
^ 行首 或 表示 非
\( </font></span><span style="font-size: 9pt; font-family: 宋体;">行末</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/< </font></span><span style="font-size: 9pt; font-family: 宋体;">词首</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/> </font></span><span style="font-size: 9pt; font-family: 宋体;">词尾</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">* 0</font></span><span style="font-size: 9pt; font-family: 宋体;">次或多次</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/( /) </font></span><span style="font-size: 9pt; font-family: 宋体;">分节指定与其中正则式匹配的部分,在替换时候可以用</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> /1 /2 /3 ... </font></span><span style="font-size: 9pt; font-family: 宋体;">引用匹配部</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">分</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">[] </font></span><span style="font-size: 9pt; font-family: 宋体;">表示选择</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">- </font></span><span style="font-size: 9pt; font-family: 宋体;">表示范围</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">,例如</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> [0-9]</font></span><span style="font-size: 9pt; font-family: 宋体;">代表数字,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">[a-z]</font></span><span style="font-size: 9pt; font-family: 宋体;">代表小写字母</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> [^0-9a-zA-Z] </font></span><span style="font-size: 9pt; font-family: 宋体;">代表非数字和大小</span><span lang="EN-US" style="font-size: 9pt;"><br /></span><span style="font-size: 9pt; font-family: 宋体;">写字母</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/{m,n/} </font></span><span style="font-size: 9pt; font-family: 宋体;">前面部分的从</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> m </font></span><span style="font-size: 9pt; font-family: 宋体;">次</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">至</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> n </font></span><span style="font-size: 9pt; font-family: 宋体;">次出现,</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">m n </font></span><span style="font-size: 9pt; font-family: 宋体;">为数值</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/{m/} </font></span><span style="font-size: 9pt; font-family: 宋体;">精确</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">m</font></span><span style="font-size: 9pt; font-family: 宋体;">次出现</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">/{m,/} </font></span><span style="font-size: 9pt; font-family: 宋体;">大于等于</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">m</font></span><span style="font-size: 9pt; font-family: 宋体;">次出现</span><span lang="EN-US" style="font-size: 9pt;"><br /><br /></span><span style="font-size: 9pt; font-family: 宋体;">以下举几例子,欢迎大家提出问题来共同探讨。</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">1.</font></span><span style="font-size: 9pt; font-family: 宋体;">在</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">20</font></span><span style="font-size: 9pt; font-family: 宋体;">列后插入串</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s/^./{20/}/&insert something here/g<br /><br />2.</font></span><span style="font-size: 9pt; font-family: 宋体;">把</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman">C++</font></span><span style="font-size: 9pt; font-family: 宋体;">语言里</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> //</font></span><span style="font-size: 9pt; font-family: 宋体;">注释</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> </font></span><span style="font-size: 9pt; font-family: 宋体;">修改为</span><span lang="EN-US" style="font-size: 9pt;"><font face="Times New Roman"> /* */ </font></span><span style="font-size: 9pt; font-family: 宋体;">格式</span><span lang="EN-US" style="font-size: 9pt;"><br /><font face="Times New Roman">:%s//(.*/)\)/1////g
3.在建存储过程的sql文本里,在每个create procedure procname()
前加上drop procedure procname ; [ ]里输入的是一个空格和TAB键。
:%s/^[ ][cC][rR][eE][Aa][tT][eE][ ][pP][Rr][oO][cC][eE][dD][uU][rR][eE][ ]
/([^(]/)/drop procedure /1;Ctrl_VCtrl_Mcreate procedure /1/g
—————-
数字加减, CTRL-A, CTRL-X
—————-
vim里自动缩进一段
把光标移动到某个花括号,
按 =% 缩进整段。
把整段不按格式往外缩一个tab
>%
缩两个
>>%
往里缩
<%
注意%匹配很多东西,
如果你想从
#ifdef
缩到
#endif
也可如此
向下查找光标下(或附近)的 <word> 。向上找用 # 。 g 查找则不限制 whole word 。
2. C-R (magic insert)
在 insert 模式下, C-R (register) 插入 register 里的内容,一个有趣的 reg 是 “=”.
假设你想输入 123K 的具体字节数,不用打开计算器,试试这个 “<C-R>=1024*123<CR>” ,
“” 就出来了!
另外在命令行里 C-R C-W 和 C-R C-A 是必用的技巧,它们将光标下的 <word> 和 <WORD>
考到命令行里,省了你无数的 typing 。
3. C-X (auto complete)
在 insert 模式下, C-X C-P/N/L/F 等自动完成前面的词、行、文件名等,是编程时必用的
命令。其中 C-P 和 C-N 可以不用 C-X 。
4. [p & ]p (smart paste)
paste 同时自动根据目标行的缩进调整来源行的缩进。在 copy 代码段的时候非常有用。
5. C-O (fast out, fast in)
在 insert 模式下,用 C-O 后可以执行一个 normal 命令,然后立即返回 insert 模式,省去了
用 ESC 的麻烦。
6. [I (fast grep )
[I 显示文件中包含光标下 <word> 的所有行。我常用来浏览某个 id 在程序中的引用情况。
还有很多相关的命令: :h include-search

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/62014.html