vim批量操作

博客介绍了使用vim编写代码时批量注释与反注释的操作方法,包括块选择模式和替换命令。还展示了在Ubuntu系统中使用git、python的操作过程,如进入python目录、编辑startup.py文件等,同时提及了vim批量替换的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(文章都是从别的地方摘抄并总结的,如有侵权,请联系管理员)
  • 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

转载于:https://www.cnblogs.com/dundun/p/7467616.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值