Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert predefined text on keyboard shortcut

Tags:

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?

The exact sequence I'd like to map is:

  1. Insert new line.
  2. Insert binding.pry to the newly created line.
  3. Return to normal mode.

EDIT: binding.pry is text I want to paste, not a file.

Before insert:

a = 1
b = 2

After insert:

a = 1
binding.pry
b = 2
like image 556
mrzasa Avatar asked Jun 13 '13 11:06

mrzasa


People also ask

What is the keyboard shortcut for inserting text?

Insert a Text boxPress and release ALT, N, and then press X. Press the arrow keys to select the Text box that you want, and then press ENTER. Type the text that you want. When you are finished typing and want to switch back to editing text in your document, press ESC.

What is the use of Ctrl M?

Ctrl+M in Word and other word processors Ctrl+M is used to indent a paragraph in Microsoft Word and other word-processing software. The indent keeps growing if you repeatedly use this keyboard shortcut. To indent the paragraph by three units, for instance, hit M three times while holding down Ctrl.


2 Answers

Record a macro (untested)

qq               " record macro to register q 
o                " insert empty line below cursor
esc              " exit insert-mode
:r /path/to/binding.pry   " insert content of file
esc              " cmd-mode
q                " end recording

To execute macro, do

@q

Or add the following to your .vimrc file

update

To insert the string binding.pry the mapping becomes:

map ,p obinding.pry<ESC>
like image 183
Fredrik Pihl Avatar answered Oct 18 '22 18:10

Fredrik Pihl


Easiest is an abbreviation that is triggered from insert mode:

:ia debug <CR>binding.pry

Now, when you type debug, the text binding.pry is inserted on a new line.

Documentation: :help abbreviations

like image 38
Ingo Karkat Avatar answered Oct 18 '22 18:10

Ingo Karkat