熟悉幾個命令,脫離鼠標依賴,VIM技能升級


如果你剛開始使用VIM,運行過vimtutor幾次,是否會覺得好VIM實在太太太笨拙,簡直不能理解爲何它還有那麼多簇擁,甚至visual studio還提供vim的插件。那其實是因爲你還沒有擺脫鼠標的依賴,對vim強大的命令而言,你所掌握的還是僅僅是最皮毛的而已。


如果你是初學者,可以先閱讀 VIM 初學與進階的操練及個人體驗


如下介紹的幾個命令(命令組合)將囊括90%的工作需求,你會發現,脫離了鼠標,其實也不是那麼笨拙的一件事,而你的操作,似乎更快了有沒有?


第一步:牢記光標移動命令。

1.1 假設光標當前位置在i(nt)。如何使得光標移動到|所指示的位置?

|   |     |    ||              |       |    |

    //int length = strlen(other.m_data);

1.2 光標達到行末。如何使得光標反向移動到|所指示的位置?

    //int length = strlen(other.m_data);

                  |            |


命令提示:

0   ^    2w    et=            f.      g_    $

|   |     |    ||              |       |    |

    //int length = strlen(other.m_data);

                  |            |

                  T=           F. 

0        : 到行頭

^        : 到行頭第一個非blank字符

$        : 到行尾

g_      : 到行尾最後一個非blank字符

w        : 按單詞移動,到下一個單詞開頭  (提示: N<command> 重複命令N次)

e        : 按單詞移動,到下一個單詞結尾

f<char>: 到下一個<char>字符處

t<char>: 到下一個<char>字符前一個字符處

F/T 與f/t相同,只是反向操作。


1.3 假設光標在TODO註釋行,怎樣到第一行?最後一行?第N行?

      你在其他位置,怎樣快速跳到TODO處, 下一個TODO,上一個TODO?

String & String::operate =(const String &other) {

    //check if it is self-assignment

    if(this == &other)

        return *this;

    //TODO: other things for copy

    //TODO: next

}



命令提示:

gg    : 到第一行

G      :到最後一行

NG    :到第N行       (提示: :set nu 顯示行號    :set nu! 不顯示行號)

/pattern: 搜索pattern的字符串 (如果有多個匹配 n 到下一個 N 到上一個)


重要提示:

    很多命令可以和光標移動連用,實現對一塊區域的一起操作。

    <start_position><command><end_position>

    例如,

    0y$: 到行頭,開始拷貝到本行最後一個字符。

    ye:   從當前位置拷貝到本單詞最後一個字符。

    dt):  從當前位置刪除所有內容直到遇到)。


第二步:熟悉區域選擇。


2.1 <action>a<object> 和 <action>i<object> 區域操作

    action 可以是任何命令,例如d(刪除),y(拷貝),v(以可視模式選擇)

    object 可以是w(單詞),s(句子),p(段落),或者特別字符如:),}等。   

                Lesson 2.1 Operation on block


     * <operation>a<word> <operation>i<word> : operation on a block, a word/sentence/paragraph/ or anything between ",',),},]


     1. Move the cursor to below line inside  helloworld.

     2. Try  the commands one by one and see what's the highlighted text.

        vaw     viw     (w stands for word)

        vas     vis     (sentence)

        vap     vip     (paragraph)

        va"     vi"     ("")

        va]     vi]     ([])

     3. Move the cursor back into helloword, using d(elete) and y(ank) replace v in <operation> and see what happens.


--->    function( map["helloworld"] ){

--->    //this is a comment

--->    }


2.2 假設你有如下function,你打算全部註釋掉,怎樣操作?

      你打算全部加上一個縮進,怎樣操作?

function( map["helloworld"] ){

    var a = 10;

    //this is a comment

    return;

}


命令提示:

        ^ <Ctrl-v> <Ctrl-d> I// [ESC] [ESC]

        1. Move your curser to the beginning of the function. ^

        2. Ctrl+v    : start block selection.

        3. Ctrl+d    : move your cursor to select the whole function.

        4. I// [ESC] : Insert '//', enter ESC to make it affects to each line

       ^ <Ctrl-v> <Ctrl-d> 


第三步:自動提示 Ctrl +p 或者 Ctrl + n


第四步:分屏 :split:vsplit


                Lesson 4 Split the window


        1. :split or   :vsplit      : split the window                   ^

        2. Ctrl+w <dir>            : dir is direction, can be hjkl or <- | | ->

                                                                           v

                                     to select working window

        3. Ctrl+w q                : quit the split mode


        4. Ctrl+w_ or  Ctrl+w|     : maximize the window

        5. Ctrl+w+ or  Ctrl+w-     : increase or decrease the size of the window


怎麼樣,以上命令都熟悉了後,是不是GUI編輯器裏你所常用的90%的操作都可輕鬆在vim裏面輕易實現?

(以上可以看作advancedvim的解釋,advancedvim是一份模仿vimtutor所創建的進階練習教程,可以在這裏下載





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章