VIM TIP31

To find and replace one or more occurences of a given text pattern with a new text string, use the s[ubstitute] command. 为了查找替换文中出现过的一个或多个文本串,使用s命令 There are a variety of options, but these are what you most probably want: 有很多可选的变量,看看什么是你最需要的: :%s/foo/bar/g find each occurance of 'foo' and replace it with 'bar' without asking for confirmation 不确认替换每一个foo为bar :%s/foo/bar/gc find each occurance of 'foo' and replace it with 'bar' asking for confirmation first 首次向用户确认并替换每一个foo为bar :%s//bar/gc find (match exact word only) and replace each occurance of 'foo' with 'bar' 将一个词foo替换为bar :%s/foo/bar/gci find (case insensitive) and replace each occurance of 'foo' with 'bar' 大小写不敏感替换foo为bar :%s/foo/bar/gcI find (case sensitive) and replace each occurance of 'foo' with 'bar' 大小写敏感替换foo为bar

NB: Without the 'g' flag, replacement occurs only for the first occurrence in each line. NB: 没有g选项的话,仅仅替换每行的第一个匹配项 For a full description and some more interesting examples of the substitute command refer to 想知道替换详细的解释和一些有趣的例子请参考 :help substitute

See also: 或者: :help cmdline-ranges :help pattern :help gdefault

Technorati Tags: ,

VIM TIP30

To increase a number under or nearest to the right of the cursor, go to Normal mode and type: 在一般模式下可以这样来递增光标下或最靠近光标右侧的数字: Ctrl-A

To decrease, type: 递减可以使用: Ctrl-X

Using this in a macro simplifies generating number sequences a lot. 使用这样的宏可以简化对数字序列的操作。

Technorati Tags: ,

VIM TIP28

VimTip 28: add a line-number to every line without cat or awk alike utilities.
不使用cat或awk等在添加每行都添加行号

With Unix-like environment, you can use cat or awk to generate a line number easily, because vim has a friendly interface with shell, so everything work in vim as well as it does in shell.
在类Unix环境下,你可以轻松的使用cat或awk产生行号,因为vim与shell有友好的接口,所以同样可以在vim中执行shell中的操作。
:%!call -n
or
:%!awk '{print NR,$0}'

But, if you use vim in MS-DOS, of win9x, win2000, you loss these tookit.here is a very simple way to archive this only by vim:
但是,如果你在MS的系统下,你将无法使用这些工具。下边是仅靠vim来实现上述功能的方法:
fu! LineIt()
exe ":s/^/".line(".")."/"
endf

Well, a sequence composed with alphabet is as easy as above:
那么,压缩为一行包括同样简单:
exe "s/^/".nr2char(line("."))."/"


Technorati Tags: ,

VIM TIP29


VimTip 29: reverse all the line with only 7 keystroke in vim
使用7个键反转所有行

:g/^/m0
well,
1. : bring you to command-line mode(also known as ex-mode) from normal-mode(also known as command mode).
1. : 使你从一般模式进入命令行
2. g means you'll take an action through the whole file, generally perform a search, v' also perform a search but it match the line not match the canonical expression.<br>2.<b>g</b> 表明作用域将是整个文件,一般使用在搜索中。'<b>v</b>'也在搜索中使用但是不能使用正则表达式。<br>3.<b> /</b> begins the regular express<br>3.<b>/</b>开始正则表达式<br>4.<b> ^ </b>is a special character respect the start of a line.<br>4.<b> ^</b>使表示行首的元字符<br>5. the second<b> /</b> ends the regular express and indicate that the remains is action to do.<br>5. 第二个<b>/</b>在正则表达式的末尾,并表明有后续操作<br>6. <b>m</b> means move,tandco' for copy, `d' for delete
6. m 表示移动,'t' 和'co'表示复制,d为删除
7. 0 is the destination line.
7. 0 是目标行号
you can use
你可以使用
:g/regexp/t$
to filter all lines and pick the match line together and copy them to the end of the buffer or
为了过滤所有的行并全部取出匹配行到buffer中
:g/regexp/y A
to put them into a register(not eax, ebx...)
将过滤出的匹配项放入寄存器(非汇编中的eax、ebx)


Technorati Tags: ,

VIM TIP27

VimTip 27: Convert hex to dec


when you check the output of objdump, you'll confused by the $0xFFFFFFc operand, this function translate the hexcamal to decimal.
当查看objdump的输出时,你将被$0xFFFFFFc之类的符号所迷惑,下边这个函数将把十六进制转为十进制。
function! Hex2Dec()
let lstr = getline(".")
let hexstr = matchstr(lstr, '0x[a-f0-9]+')
while hexstr != ""
let hexstr = hexstr + 0
exe 's#0x[a-f0-9]+#'.hexstr."#"
let lstr = substitute(lstr, '0x[a-f0-9]+', hexstr, "")
let hexstr = matchstr(lstr, '0x[a-f0-9]+')
endwhile
endfunction
usage:
5,8call Hex2Dec()


Technorati Tags: ,

VIM TIP26

VimTip 26: Getting rid of ^M - mixing dos and unix
清除混乱的dos/unix格式换行

If you work in a mixed environment you will often open files that have ^M's in them. An example would be this:
如果你在混合环境下工作的话,你将常常碰到打开的文件中含^M的情况。如下例所示:
------------------------------------------------------------------
import java.util.Hashtable; ^M
import java.util.Properties; ^Mimport java.io.IOException;
import org.xml.sax.AttributeList; ^M
import org.xml.sax.HandlerBase; ^Mimport org.xml.sax.SAXException;

/**^M
* XMLHandler: This class parses the elements contained^M
* within a XML message and builds a Hashtable^M

[snip]
------------------------------------------------------------------

Notice that some programs are not consistent in the way they insert the line breaks so you end up with some lines that have both a carrage return and a ^M and some lines that have a ^M and no carrage return (and so blend into one). There are two steps to clean this up.
值得注意的是一些程序没有以本方法识别出换行符,所以在行尾有的是回车符、有的用^M换行(有的两个混合)。这里有这么两种办法来清理文件
1. replace all extraneous ^M:
1.替换所有的无用^M
:%s/^M$//g

BE SURE YOU MAKE the ^M USING "CTRL-V CTRL-M" NOT BY TYPING "CARROT M"! This expression will replace all the ^M's that have carriage returns after them with nothing. (The dollar ties the search to the end of a line)
确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有回车符前的 ^M为空($是为了保证^M出现在行尾)
2. replace all ^M's that need to have carriage returns:
2.替换所有的^M
:%s/^M//g

Once again: BE SURE YOU MAKE the ^M USING "CTRL-V CTRL-M" NOT BY TYPING "CARROT M"! This expression will replace all the ^M's that didn't have carriage returns after them with a carriage return.
再次注意:确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有没有回车符的^M为回车符。
Voila! Clean file. Map this to something if you do it frequently.
瞧!干净多了。如果常常这样操作的话映射成命令。
:help ffs - for more info on file formats
:help ffs -有更多的信息
thanks to jonathan merz, douglas potts, and benji fisher
多谢了: jonathan merz douglas potts benji fisher


Technorati Tags: ,

VIM TIP25

VimTip 25: color highlighting on telnet (esp w/ SecureCRT)
在telnet中色彩高亮

The following settings in .vimrc will enable color highlighting when using SecureCRT and may work on other telnet packages. The terminal type should be selected as ANSI and color enabled.
当使用SecureCRT或其他的telnet包时在.vimrc中进行如下配置可以色彩高亮。终端必须选择ANSI并且激活色彩选项。

if !has("gui_running")
set t_Co=8
set t_Sf=^[[3%p1%dm
set t_Sb=^[[4%p1%dm
endif




Technorati Tags: ,

VIM TIP22

I frequently hold the shift key for too long when typing, for instance :wq, and end up with :Wq. Vim then whines "Not an editor command: Wq" 我时常在输入的时候按住shift键,比如:wq当以:Wq录入时,Vim会提示“无:Wq命令”的错误 In my .vimrc, I have taught vim my common typos: 在我的.vimrc配置中,我讲设置如下等价录入 command! Q quit command! W write command! Wq wq this one won't work, because :X is already a built-in command command! X xit 如果不生效的话,那是应为:X已然是内置的命令!X xit了。

Technorati Tags: ,

VIM TIP24

 Here are some pointers to the vim documentation.  Notice that the mechanism is different in vim 6.0 and vim 5.x.
这有一些vim文档的指导。注意其中vim6.0和5.x版本的构架区别。
  1. I want .foo files to be highlighted like HTML files. 1.我希望.foo 文件按照HTML文件进行语法高亮。 :help new-filetype http://www.vim.org/html/autocmd.html#new-filetype

  2. I want to define a syntax file for *.bar files. Read the above and also

  3. 我希望定义一个*.bar语法高亮文件。请参考上边文件 :help mysyntaxfile http://www.vim.org/html/syntax.html#mysyntaxfile

  4. I want to make a few changes to the existing syntax highlighting. Depending on the x in 5.x, either read the above and page down a few screens, or you may be able to skip right to

  5. 我希望对已有的语法高亮文件进行一些改变。在5.x环境下的x,阅读以上的文件和下文,或者直接跳到 :help mysyntaxfile-add http://www.vim.org/html/syntax.html#mysyntaxfile-add

  6. I want to change some of the colors from their defaults. Again, read 4.我想改变默认语法高亮的颜色配置。请读:

:help mysyntaxfile http://www.vim.org/html/syntax.html#mysyntaxfile

Technorati Tags: ,