vim tips

1) 计算文本中字符串个数

:%s/XXX/gn,n意为只做计数,不做替换

2) 空格与换行的互转

空格转换行

:%s/ +/\r/g,其中“ +”表示一个或多个空格

换行转空格

:%s/\n/ /g

那么同样是换行,为什么有\r和\n的区别呢?因为查找和替换时\r与\n的含义是不同的。
在查找时


\r      matches                                     */\r*
\b      matches                                     */\b*
\n      matches an end-of-line                          */\n*
        When matching in a string instead of buffer text a literal newline
        character is matched.

在替换时

 <CR>        split line in two at this point
              (Type the <CR> as CTRL-V <Enter>)                  *s<CR>*
  \r          idem                                               *s/\r*
  \<CR>       insert a carriage-return (CTRL-M)
              (Type the <CR> as CTRL-V <Enter>)                  *s/\<CR>*
  \n          insert a <NL> (<NUL> in the file)
              (does NOT break the line)                          *s/\n*

posted @ 2012-09-21 15:50  千里快哉  阅读(169)  评论(0编辑  收藏  举报