Vim

Vim 剪貼板如何與系統剪貼板交互?

“+y 把選中內容拷貝到”+號剪貼板,即系統剪貼板。

“+p 把系統剪貼板的內容粘貼到vim。

Vim 中的剪貼板歷史可以使用 :reg 進行羅列,並配合以上操作進行粘貼。

如何連接兩行文字?

在 Vim 中你可以把兩行連起來這意味着刪除兩行間的換行符。"J" 命令用於完成這個
功能。
以下面兩行爲例:

A young intelligent
turtle

把光標移到第一行,然後按 "J":

A young intelligent turtle

如何進行列塊選擇?

使用 Ctrl + v 進入列塊選擇模式。

如何快速查找和替換?

快速進行查找可以在命令模式下,使用 /{regex},Vim 中的正則有點特殊(詳細規則可以閱讀文末附件,最明顯的就是括號也需要轉義),舉個例子,有文字材料如下:

java.lang.NullPointerException
...
java.lang.ClassNotFoundException

要求查找到所有 Exception,那麼可以使用

/\.\a*Exception

再配合 n 繼續查找。

如何進行重做?

使用 Ctrl + r 進行重做。

如何記錄和回放我的可重複性操作?

要點:使用 q{register} 和 @{register}

"." 命令重複前一個修改操作。但如果你需要作一些更復雜的操作它就不行了。這時,記
錄命令就變得很有效。這需要三個步驟:

  1. "q{register}" 命令啓動一次擊鍵記錄,結果保存到 {register} 指定的寄存器中。
    寄存器名可以用 a 到 z 中任一個字母表示。

  2. 輸入你的命令。

  3. 鍵入 q (後面不用跟任何字符) 命令結束記錄。

現在,你可以用 "@{register}" 命令執行這個宏。
現在看看你可以怎麼用這些命令。假設你有如下文件名列表:

stdio.h
fcntl.h
unistd.h
stdlib.h

而你想把它變成這樣:

include "stdio.h"
include "fcntl.h"
include "unistd.h"
include "stdlib.h"

先移動到第一行,接着執行如下命令:

qa 啓動記錄,並使用寄存器 a
^ 移到行首
i#include " 在行首輸入 #include "
$ 移到行末
a" 在行末加上雙引號 (")
j 移到下一行
q 結束記錄

現在,你已經完成一次複雜的修改了。你可以通過重複三次 "@a" 完成餘下的修改。
"@a" 命令可以通過計數前綴修飾,使操作重複指定的次數。在本例中,你可以輸入:

3@a

如何進行查找替換?

:s(substitute)命令用來查找和替換字符串。語法如下:

:{作用範圍}s/{目標}/{替換}/{替換標誌}

例如:%s/foo/bar/g會在全局範圍(%)查找foo並替換爲bar,所有出現都會被替換(g)。

Vim 中正則的 magic

# 設置magic 
:set magic 
# 取消magic 
:set nomagic 
# 查看幫助
:h magic

幫助文檔如下:

3. Magic                                                        */magic*

Some characters in the pattern are taken literally.  They match with the same
character in the text.  When preceded with a backslash however, these
characters get a special meaning.

Other characters have a special meaning without a backslash.  They need to be
preceded with a backslash to match literally.

If a character is taken literally or not depends on the 'magic' option and the
items mentioned next.
                                                        */\m* */\M*
Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
ignoring the actual value of the 'magic' option.
Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.
                                                        */\v* */\V*
Use of "\v" means that in the pattern after it all ASCII characters except
'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning.  "very magic"

Use of "\V" means that in the pattern after it only the backslash and the
terminating character (/ or ?) has a special meaning.  "very nomagic"
Examples:
after:    \v       \m       \M       \V         matches ~
                'magic' 'nomagic'
          $        $        $        \$         matches end-of-line
          .        .        \.       \.         matches any character
          *        *        \*       \*         any number of the previous atom
          ~        ~        \~       \~         latest substitute string
          ()       \(\)     \(\)     \(\)       grouping into an atom
          |        \|       \|       \|         separating alternatives
          \a       \a       \a       \a         alphabetic character
          \\       \\       \\       \\         literal backslash
          \.       \.       .        .          literal dot
          \{       {        {        {          literal '{'
          a        a        a        a          literal 'a'

{only Vim supports \m, \M, \v and \V}

刪除指定行

# 刪除1到10行
:1,10d

參考

  1. Vim 用戶使用手冊
  2. 在 Vim 中優雅地查找和替換 - harttle.land
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章