Insert predefined text on keyboard shortcut

后端 未结 6 956
悲&欢浪女
悲&欢浪女 2021-02-07 04:27

I often insert binding.pry to my ruby files when I debug them. As I use Vim I\'d love to automate it to avoid retyping it every time. How could I do it?

Th

6条回答
  •  忘了有多久
    2021-02-07 05:11

    You can define a shortcut for this purpose with the following key strokes

    1. :vsplit $MYVIMRC
    2. i
    3. nnoremap bi obinding.pry
    4. :w
    5. :source $MYVIMRC

    Explaination

    1. Open your vimrc in a new vertical splitted window
    2. Switch to insert mode
    3. Define a mapping for the shortcut 'bi'.
    4. Leave insert mode
    5. Save the changes
    6. Source your vimrc to make the shortcut available

    Now, while you in normal mode, the keystrokes (one key after each other) will insert 'binding.pry' in a new line under the current line.

    Explaination for step 3: nnoremap is the command for mapping keystroke(s) to do some action. 'bi' is the keystroke combination. You can adjust this to your needs. And the rest is a normal edit sequenz on VIM:

    • o - Switch to insert mode under the current line
    • binding.pry - is the text which will be writen
    • - Push escape to leave insert mode.

提交回复
热议问题