Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

4.15. vim

4.15.1. vim 初始化

		
cat >> ~/.vimrc <<EOF
set ts=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
EOF
		
		

4.15.2. 查找与替换

s%/aaa/bbb/g
		
Starting Nmap 5.21 ( http://nmap.org ) at 2012-02-02 17:03 CST
NSE: Script Scanning completed.
Nmap scan report for 10.10.1.1
Host is up (0.0072s latency).
The 1 scanned port on 10.10.1.1 is filtered

Nmap scan report for 10.10.1.2
Host is up (0.0064s latency).
The 1 scanned port on 10.10.1.2 is closed

Nmap scan report for 10.10.1.3
Host is up (0.0071s latency).
The 1 scanned port on 10.10.1.3 is closed

Nmap scan report for 10.10.1.4
Host is up (0.0072s latency).
PORT     STATE SERVICE
3306/tcp open  mysql
| mysql-info: Protocol: 10
| Version: 5.1.54-log
| Thread ID: 37337702
| Some Capabilities: Long Passwords, Connect with DB, Compress, ODBC, Transactions, Secure Connection
| Status: Autocommit
|_Salt: y0!QV;ekiN)"kx;\=Y+g

Nmap scan report for 10.10.1.5
Host is up (0.0081s latency).
PORT     STATE SERVICE
3306/tcp open  mysql
| mysql-info: Protocol: 10
| Version: 5.1.48-community-log
| Thread ID: 6655211
| Some Capabilities: Long Passwords, Connect with DB, Compress, ODBC, Transactions, Secure Connection
| Status: Autocommit
|_Salt: i3ap1?+UL^q>$5~=UqYJ

Nmap scan report for 10.10.1.6
Host is up (0.0073s latency).
The 1 scanned port on 10.10.1.6 is closed

Nmap scan report for www.example.com (10.10.1.7)
Host is up (0.0074s latency).
The 1 scanned port on www.example.com (10.10.1.7) is closed
		</screen>
		<para>删除closed上面2行</para>
		<screen>
:%s:.*\n.*\n.*closed$::g
:%s/\n\n\n//g			
		
		

4.15.3. 删除操作

删除指定行

		
:158,158d		
		
		

4.15.4. 插入文件

当前光标处插入文件

		
:r /etc/passwd
		
		

第十行处插入文件

		
:10 r /etc/passwd
		
		

4.15.5. 批处理

test script

		
vim test.txt <<end > /dev/null 2>&1
:%s/neo/neo chen/g
:%s/hello/hello world/g
:wq
end
		
		

test.txt

		
begin
neo
test
hello
world
end
		
		

test result

		
$ ./test
$ cat test.txt
begin
neo chen
test
hello world
world
end
neo@netkiller:/tmp$
		
		
vi 批处理
			
for i in file_list 
do 
vi $i <<-! 
:g/xxxx/s//XXXX/g 
:wq 
! 
done
			
			

4.15.6. line()

加入行号

		
:g/^/ s//\=line('.').' '/
		
		

4.15.7. set fileformat

加入行号

		
vim    set fileformat
执行 set fileformat  会返回当前文件的 format 类型 如:fileformat=dos
也可执行 set line	
		
		

4.15.8. 空格与TAB转换

ts 是tabstop的缩写,设TAB宽度为4个空格。

softtabstop 表示在编辑模式的时候按退格键的时候退回缩进的长度,当使用 expandtab 时特别有用。

shiftwidth 表示每一级缩进的长度,一般设置成跟 softtabstop 一样。

expandtab表示缩进用空格来表示,noexpandtab 则是用制表符表示一个缩进。

autoindent自动缩进

空格|tab 长度设置

		
set ts=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
		
		

上面配置可以添加到 vim 配置文件中:/etc/virc 和 /etc/vimrc

TAB替换为空格

		
:set ts=4
:set expandtab
:%retab!		
		
		

空格替换为TAB

		
:set ts=4
:set noexpandtab
:%retab!