1. 程式人生 > >vim批量替換

vim批量替換

uri pattern git vim編輯器 p s 模式 插入模式 多行 username

(文章都是從別的地方摘抄並總結的,如有侵權,請聯系管理員)
  • 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批量替換