vim查找替换小结

vim FAQ

marz posted @ Aug 11, 2010 06:44:22 AM in tool-vim with tags vim faq , 1511 readers

 

(1) 为什么别人的方法在我这里行不通?

(2) 如何找到上次编辑的文件?

(3) 如何复制粘贴?

(4) 如何在多个文件中进行替换?

(5) 如何把fold一次全部打开?

(6) 如何选择、复制或者查找光标所在的单词?

(7) 如何计数搜索的字符串?

(8) 如何去掉高亮搜索?

(9) 如何解决linuxgvim中文菜单乱码的问题?

(10) 如何对所编辑的文件语法高亮显示?

(11) 如何快速插入多行数字0-99

(12) 如何设置中文编码?

(13) 如何去掉或者插入^M这样的字符?

(14) 如何整体缩进一段代码(插入tab)?

(15) 如何合并两行?

(16) 如何设置缩进距离(tab宽度)?

 

==============================================================================

(1) 为什么别人的方法在我这里行不通?

 

估计这是最难回答的一个问题了。:-)

不过首先应该确定你的VIM版本是否足够新,功能是否完整。使用 :version 命令查看你

VIM版本以及功能。一个常见的情况,如果你用的是RedHat系统,请确定安装了

vim-enhancerpm包,否则将是不完整的VIM

 

(2) 如何找到上次编辑的文件?

 

Ctrl+O (按两次)

 

(3) 如何复制粘贴?

 

复制

方法一:先ma标记当前位置为a,移动到新的位置,再y`a或者y''a复制从标记位置a

当前位置的内容。`''的区别:`以字符为单位,''以行为单位。这个方法应该是所有vi

都适用的。(如果不对请指正。)

方法二:按vVCtrl+V进入visual模式,移动光标选择一段内容,按y。三者的区别:

v以字符为单位,V以行为单位,Ctrl+V是块选择。这个方法只在VIM下适用。

 

粘贴

pP。区别:前者粘贴到光标后面,后者粘贴到光标前面。

 

(4) 如何在多个文件中进行替换?

 

一般可以用sed或者vim来做。比如要对当前目录下的*.c文件进行替换:

方法一:

for i in *.c; do

  sed -i -e ''s/oldvalue/newvalue/g'' $i

done

方法二:

vim *.c

:argdo %s/oldvalue/newvalue/g

 

(5) 如何把fold一次全部打开?

 

zR 或者 :set foldlevel=999

 

(6) 如何选择、复制或者查找光标所在的单词?

 

VIM中用iw或者aw表示一个单词,两者稍有区别。

选择光标所在的单词:viw v进入visual模式,然后iw

复制光标所在的单词:yiw

 

查找的话就是两个很常用的明令: *

 

(7) 如何计数搜索的字符串?

 

如果需要统计缓冲中一个模式(pattern)出现的频率,需要把 ''report'' 置为0,然后

用替换命令把模式替换成它自己。Vim 报告的已替换的数目就是模式出现的次数。例:

        :set report=0

        :%s/./&/g               characters

        :%s/i+/&/g            words

        :%s/^                   lines

        :%s/the/&/g             "the" anywhere

        :%s/<the>/&/g         "the" as a word

你可能希望复位 ''hlsearch'' 或者使用 ":nohlsearch"

 

(8) 如何去掉高亮搜索?

 

方法一::nohlsearch

方法二:/awertgvcxz (查找一个肯定不存在的字符串)

设置了高亮搜索(:set hlsearch)以后,可以通过 :set nohlsearch 永久地关闭高亮

显示,也可以通过 :nohlsearch 暂时关闭高亮显示,下次搜索时将自动恢复高亮。

 

(9) 如何解决linuxgvim中文菜单乱码问题?

 

vim6.3为例

:source $VIMRUNTIME/delmenu.vim

:source $VIMRUNTIME/menu.vim

 

也可以将以上内容加入.vimrc

 

source一个menu.vim或者啥也不source都是乱码

另外,也有网友报告说没有遇到此类问题

 

注:这里的source指的是vim自己的source命令,和bash shellsource无关

 

(10) 如何对所编辑的文件语法高亮显示?

 

需要解释的是,VI有很多变种,它本身并没有这种功能,提供这种功能

往往是其各种衍生版本,比如说vim。下面就vim中如何语法高亮简单解释一下。

 

首先,确定一下你所用的操作系统,如果是windows,那么默认的配置文件

就可以工作了。如果还不行,可以用鼠标从syntax菜单下选择一下适合你的

文件类型(新版本的gvim默认把文件类型选择关闭了,需要点开,然后按照

开头字母选择类型)

如果是linux,那么确认一下是否是redhat。如果是redhat,那么需要安装

vim-enhancedrpm包。这些都做了以后,可以用:syntax on来打开语法高亮。

必要时可以用:set filetype=...来设定你的文件类型。

如果使用图形界面,比如gvim,那么syntax onset filetype的操作可以

用鼠标在菜单中选择,同windows下。

如果是在控制台中使用vim,还应当确定你的终端设置支持颜色。

 

(11) 如何快速插入多行数字0-99

 

在命令行中输入以下代码,或者先把代码保存到一个外部文件,然后source这个文件:

let i = 0

while i < 100

  put = i

  let i = i + 1

  endw

如果位数不足需要在前面补零,可以用Ctrl-V块选择,然后I插入0

 

(12) 如何设置中文编码?

 

.vimrc中作如下设置,可以实现界面以及显示字体采用gb2312,遇到utf8文件时

自动做转换:

set encoding=prc

set fileencoding=prc

set fileencodings=utf8,prc

需要注意的是,按照上面的设置,如果你打开一个空的(0字节)或者是纯7bits的文件,

这时VIM将认为是utf8编码的文件,如果你往里面输入中文,存盘的时候也将是utf8

因此你可能需要在存盘之前 :set fileencoding=prc 把编码改回来。

另外,对于纯7bits的文件,由于认成了utf8,每次打开存盘的时候都要做一次转换,

会比较慢,所以如果你很长一段时间不会接触utf8的话,可能暂时把 fileencodings

一行注释掉比较好。

[警告]:转换可能会造成信息丢失!如果你需要编辑的是重要的系统文件,最好把

fileencodings 一行暂时注释掉。

 

(13) 如何去掉或者插入^M这样的字符?

 

VIM的插入模式或者命令行中用<C-V>输入^M这样的特殊字符,例如输入

:s/<C-V><C-M>//g

实际显示是

:s/^M//g

就可以去掉文件中所有的^M。同理,要在文件中输入^M只要按<C-V><C-M>即可。

 

这个问题往往发生在WINDOWS/DOS系统和UNIX系统之间交换文件时,因为这两类系统

对于文本的“换行”概念解释不同。所以,还有一个解决方法就是对这类文本进行

转换。vim内部就可以做这件事情。首先打开文本,然后

set fileformat=unix

再从新保存该文件覆盖原文件就可以去除:^M

 

(14) 如何整体缩进一段代码(插入tab)?

 

选中一段代码, >  2<  3> ……

 

(15) 如何合并两行?

 

合并两行的键是 J

另外,如果你设置了

set backspace=2

你就可以用backspace或者delete很方便的删到上一行或者下一行。

 

(16) 如何设置缩进距离(tab宽度)?

 

set tabstop=8

set shiftwidth=8

 

从 <http://www.fish888.com/vim-FAQ-t95403> 插入

meidir said:
Jan 14, 2023 01:05:29 AM

very nice post, i definitely enjoy this amazing site, persist in it 카지노사이트

meidir said:
Feb 11, 2023 12:57:36 AM

When I originally commented I clicked the -Notify me when new comments are added- checkbox and after this whenever a comment is added I get four emails with the same comment. Could there be however you possibly can get rid of me from that service? Thanks! SaaS Lawyer

meidir said:
Feb 13, 2023 04:02:28 PM

Hi, i think that i saw you visited my weblog so i came to “return the favor”.I am trying to find things to improve my site!I suppose its ok to use a few of your ideas!! 여우알바

 

 

==============================

 

 

I cannot thank you fully for the posts on your web page. I know you set a lot of time and effort into them and really hope you know how deeply I enjoy it. I hope I’m able to do something identical man or woman someday. 二手mac

meidir said:
Feb 22, 2023 11:52:29 PM

This is such a great post, and was thinking much the same myself. It’s certainly an opinion I agree with. 여우알바

meidir said:
Oct 05, 2023 11:22:12 PM

I’d perpetually want to be update on new content on this web site , saved to my bookmarks ! . 花藝學校

meidir said:
Oct 13, 2023 02:40:05 AM

A powerful share, I just given this onto a colleague who was doing slightly analysis on this. And he in reality bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the deal with! However yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love studying extra on this topic. If possible, as you turn into experience, would you mind updating your weblog with more particulars? It is extremely useful for me. Massive thumb up for this blog post! 二手電腦回收

 

 

=====================

 

 

An array of wild hair caution equipment in hair apply, tweezers, hydrogen stick reviews frizzy hair scissors, frizzy hair sawing scissors, sheers, specialist sheers, frizzy hair sheers, frizzy hair hair comb, bobby pin, head piece, eyelash curler, hair hair brush, plus shower limitation accessories can be purchased. macbook回收

 

 

=====================

 

 

Youre so cool! I dont suppose Ive read anything similar to this before. So nice to get somebody by incorporating original applying for grants this subject. realy appreciate starting this up. this fabulous website is one thing that is needed on the web, somebody if we do originality. valuable project for bringing new stuff on the internet! 課外活動

 

 

======================

 

 

That is the appropriate blog for anybody who needs to find out about this topic. You understand a lot its almost onerous to argue with you (not that I really would want aHa). You positively put a new spin on a subject thats been written about for years. Great stuff, just great! 到校興趣班

meidir said:
Oct 13, 2023 10:41:36 PM

Substantially, the post is really the sweetest on that precious topic. I fit in with your conclusions and will eagerly look forward to your coming updates. Just saying thanks will not just be enough, for the amazing lucidity in your writing. I will at once grab your rss feed to stay abreast of any kind of updates. Genuine work and also much success in your business efforts! heart


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter