gvim 快速編輯例子

1 準備工做vim

首先, 咱們打開 VIM, 輸入一段文本,  用於今天的演示: this

this is a test

2 查找替換

按幾下 ESC 進入 Normal 模式, 輸入如下命令: :%s/ /\r/g 。 回車後獲得的效果以下: spa

this
is
a
test

解說: 這條命令的做用是,  將文章中全部的空格替換爲回車 。 code

3 行的拼接

    按幾下 ESC 進入 Normal 模式, 而後輸入這段命令: ggVG gg   表示跳到文本開頭 , V  表示進入行選擇模式 , G   表示選擇到文章末尾 。 經過這 3 條命令, 總共 4  個按鍵, 咱們選中了整篇文章 。 orm

而後, 按下冒號 :  進入命令模式 ,  狀態欄上出現: :'<,'> 字樣,  在它後面輸入 j , 而後回車, 能夠看到, 整篇文章又被拼接起來了 , 整個操做包括回車只按了 7  次鍵:  io

this is a test

4 複製粘貼與重複動做

按幾下 ESC 確認當前處在 Normal 模式下, 而後按 yy , 便可將當前行復制到默認寄存器中  (至關於剪貼板) 。 而後按下 12p , VIM  將執行粘貼動做 12 次, 屏幕上出現了 13  行這樣的字符: class

this is a test
this is a test
this is a test
this is a test
this is a test
t his is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test  

解說: 在 VIM 中, 複製和粘貼操做至關快捷。 另外,  VIM  中大部分命令均可以經過在命令前加數字重複若干遍 。 test

5 列操做

     把每一行的開頭第一個字母改成大寫。  vimrc

按幾下 ESC 確認當前處在 Normal 模式下, 而後按  gg  跳到第一行, 按下 Ctrl + v  進入列選擇模式  (若是你按下 Ctrl + v 沒能進入列選擇模式: 編寫本身的 _vimrc(在安裝跟目錄)! 若是你不會寫, 請放一個空文件在那兒),  而後按 G , 跳到文章最後一行, 此時你應該看到, 文本的第一列被選中了, 並且只選中了第一列。 按下 U  鍵,  能夠看到, 每行的第一個字母都變爲大寫 了。 提示:  選中文本後按 u   能夠將文本變爲小寫 ,  選中文本後按 ~   能夠翻轉原有的大小寫 。  im

This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test

而後, 咱們在每行的前面 加上一個星號 。 按下  gg  跳到第一行, 按 Ctrl + v 進入列選擇模式,  再按 G , 選中全文的第一列, 而後按 I , 進入列插入狀態 , 輸入星號  *再按下 ESC , 你會看到,  全部行以前都出現了一個星號:  

*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test  

解說:  另外,  列選擇後按 x 刪除 被選中的塊, 能夠批量地解除註釋。

6 宏的錄製

  接下來, 咱們要將文本的偶數行修改成:  This is another test 。  因爲全部的偶數行都要進行一樣的操做,  所以咱們把這個操做錄製下來,  而後重複播放若干遍, 就能很快地完成這項工做了。

      首先, 按幾下 ESC 確認處在 Normal 模式下, 再按下  gg  跳到第一行, 準備開始操做。  咱們首先按下 q  鍵,  而後再按一個其它字母 ,  將這個宏錄製 到該字母對應的寄存器 下。  例如咱們這裏使用 m  寄存器, 則按  qm 。 此時 VIM 狀態欄出現「recording」 字樣,  代表已經進入了錄製狀態 。

      而後, 咱們把第二行的 a  修改成  another 。 首先按 j  進入第二行 ,  而後按 $  跳到行末 , 再按兩下 b   往前跳兩個單詞 , 此時光標停在字母 a  上。  而後咱們輸入caw 刪除 a  並進入插入狀態, 而後輸入 another  , 按  ESC 回到 Normal 狀態, 按 j  進入下一行,  整個操做步驟就完成了。 最後, 咱們再按一下 q , 結束該宏的錄製。

      接下來咱們播放這個宏, 完成整個操做步驟。  在鍵盤上輸入 1000@m , 表示將 m  寄存器裏的宏播放 1000 次, 立刻能夠看到,  文章中全部偶數行的 a  都變成了  another

*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test  

解說: 雖然咱們指定播放 1000 次 , 但事實上,  執行到第 6 次的時候, 光標挪到了屏幕最下方,  因而執行過程就自動中止了。 所以,  在批量操做的時候, 咱們能夠指定足夠大的數字,  而不用擔憂出現問題。  

另外, 修改 a  的時候,  咱們跳到行末後再使用 b  命令以單詞爲單位跳轉, 而沒使用 h  一個字母一個字母往回挪, 咱們使用 caw  修改整個單詞 , 而不使用 s  命令刪除單個字母並進入 Insert 模式 。  這些細節能夠保證錄製獲得的宏更具備通常性。

7 點命令

      接下來, 咱們在每行的末尾加上一個感嘆號  !

因爲每行長度不一樣,  咱們沒法使用塊選擇方式 批量尾部添加感嘆號。  使用宏錄製的方式是能夠作到這點的,  但操做稍嫌繁瑣了一些。 使用點命令,  能夠很是方便地作到這一點。

先按幾下 ESC 確認當前出於 Normal 模式, 而後使用  gg  跳到第一行, 按 A   進行行尾插入 , 輸入 ! , 而後按下 ESC 回到  Normal 狀態, 第一行的感嘆號就添加完畢了。

而後, 咱們按 j  進入第二行 , 再按 . , 能夠看到,  第二行尾部也出現了感嘆號。 反覆按 j.j.j. ,  直到每一行尾部都添加了感嘆行爲止。

*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!  

解說: 點命令 的做用是, 重複最近一次所作的編輯操做 。  因爲在第一行裏作的操做是行尾添加,  所以在第二行重複這個動做的時候,  也會在行尾添加一樣的字符。  點命令功能不如宏強大, 但它使用起來比宏簡便,  所以也有着普遍的用途。

相關文章
相關標籤/搜索