码迷,mamicode.com
首页 > 系统相关 > 详细

vim批量替换

时间:2017-09-02 20:44:46      阅读:2218      评论:0      收藏:0      [点我收藏+]

标签:lan   cape   ctr   复制   x86_64   join   rtu   linu   word   

(文章都是从别的地方摘抄并总结的,如有侵权,请联系管理员)
  • vim编辑器---批量注释与反注释   

在使用vim编写代码的时候,经常需要用到批量注释与反注释一段代码。下面简要介绍其操作。

方法一 块选择模式

 

插入注释:

用v进入virtual模式

用上下键选中需要注释的行数

按CTL+v(win下面ctrl+q)进入列模式

按大些“I”进入插入模式,输入注释符“#”或者是"//",然后立刻按下ESC(两下)

取消注释:

 

Ctrl + v 进入块选择模式,选中你要删除的行首的注释符号,注意// 要选中两个,选好之后按d即可删除注释

 

方法二 替换命令

批量注释:
使用下面命令在指定的行首添加注释:
:起始行号,结束行号s/^/注释符/g
取消注释:
:起始行号,结束行号s/^注释符//g
  • vim批量替换:
    :%s/source_pattern/target_pattern/g即可完成

如想把所有的username换成login_name,那么:%s/username/login_name/g就可以了。

 

 

 

Connecting to 192.168.127.130:23...

Connection established.

Escape character is ‘^@]‘.

Ubuntu 16.04.2 LTS

ubuntu64-casa login: casa

Password:

 

Login incorrect

ubuntu64-casa login: casa

Password:

Last login: Tue Mar 21 09:29:56 CST 2017 from 192.168.127.1 on pts/1

Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-62-generic x86_64)

 

* Documentation: https://help.ubuntu.com

* Management: https://landscape.canonical.com

* Support: https://ubuntu.com/advantage

 

26 packages can be updated.

25 updates are security updates.

 

 

Signature not found in user keyring

Perhaps try the interactive ‘ecryptfs-mount-private‘

[Oh My Zsh] Would you like to check for updates? [Y/n]: Y

Updating Oh My Zsh

error: Cannot pull with rebase: You have unstaged changes.

There was an error updating. Try again later?

?. ~ git:(master) ?.

?. ~ git:(master) ?.ls

python

?. ~ git:(master) ?.cd python

?. python git:(master) ?.ls

03062.pcap 1_0310.py 1.py ftp1.py ftp2.py ftp.py master.zip paramiko.log pydiction ssh.py startup.py

?. python git:(master) ?.vim startup.py

?. python git:(master) ?.vim startup.py

 

1 #startup.py

2 #!/usr/bin/python

3 # python startup file

4

5 import sys

6 import readline

7 import rlcompleter

8 import atexit

9 import os

10 # tab completion

11 readline.parse_and_bind(‘tab: complete‘)

12 # history file

13 histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)

14 try:

15 readline.read_history_file(histfile)

16 except IOError:

17 pass

18 atexit.register(readline.write_history_file, histfile)

19

20 del os, histfile, readline, rlcompleter

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

:%s/import/immport/g

 

 

Connecting to 192.168.127.130:23...

Connection established.

Escape character is ‘^@]‘.

Ubuntu 16.04.2 LTS

ubuntu64-casa login: casa

Password:

 

Login incorrect

ubuntu64-casa login: casa

Password:

Last login: Tue Mar 21 09:29:56 CST 2017 from 192.168.127.1 on pts/1

Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-62-generic x86_64)

 

* Documentation: https://help.ubuntu.com

* Management: https://landscape.canonical.com

* Support: https://ubuntu.com/advantage

 

26 packages can be updated.

25 updates are security updates.

 

 

Signature not found in user keyring

Perhaps try the interactive ‘ecryptfs-mount-private‘

[Oh My Zsh] Would you like to check for updates? [Y/n]: Y

Updating Oh My Zsh

error: Cannot pull with rebase: You have unstaged changes.

There was an error updating. Try again later?

?. ~ git:(master) ?.

?. ~ git:(master) ?.ls

python

?. ~ git:(master) ?.cd python

?. python git:(master) ?.ls

03062.pcap 1_0310.py 1.py ftp1.py ftp2.py ftp.py master.zip paramiko.log pydiction ssh.py startup.py

?. python git:(master) ?.vim startup.py

?. python git:(master) ?.vim startup.py

 

1 #startup.py

2 #!/usr/bin/python

3 # python startup file

4

5 immport sys

6 immport readline

7 immport rlcompleter

8 immport atexit

9 immport os

10 # tab completion

11 readline.parse_and_bind(‘tab: complete‘)

12 # history file

13 histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)

14 try:

15 readline.read_history_file(histfile)

16 except IOError:

17 pass

18 atexit.register(readline.write_history_file, histfile)

19

20 del os, histfile, readline, rlcompleter

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

-- VISUAL -- 16 5,1 All

  • VI删除与复制多行
 
一、多行
dd
删除一行
ndd 
删除以当前行开始的n行
dw
删除以当前字符开始的一个字符
ndw
删除以当前字符开始的n个字符
d$、D
删除以当前字符开始的一行字符
d)
删除到下一句的开始
d}
删除到下一段的开始
d回车
删除2行
 
 
二、复制多行
任务:将第9行至第15行的数据,复制到第16行
方法1:(强烈推荐)
:9,15 copy 16  或 :9,15 co 16
由此可有:
:9,15 move 16  或 :9,15 m 16 将第9行到第15行的文本内容到第16行的后面  
 
方法2:
光标移动到结束行,ma
光标移动到起始行,输入y‘a
光标移动到需要复制的行,输入p,行前复制则输入大写P
 
方法3:
把光标移到第9行 shift + v
再把光标移动到第15行  ctrl + c
再把光标死去到第16行  p MySQL 
 
方法4:
光标移动到起始行,输入ma
光标移动到结束行,输入mb
光标移动到粘贴行,输入mc
然后输入:‘a,‘b, co ‘c   把co换成m就是剪切
若要删除多行,则输入:‘a,‘b de
 
vi设置自动缩进:set smartindent
vi设置显示行号:set number 或 set nu

vim批量替换

标签:lan   cape   ctr   复制   x86_64   join   rtu   linu   word   

原文地址:http://www.cnblogs.com/dundun/p/7467616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!