19

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
mrzasa
  • 21,673
  • 11
  • 52
  • 88

6 Answers6

26

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>
alhelal
  • 800
  • 9
  • 21
Fredrik Pihl
  • 41,002
  • 6
  • 73
  • 121
12

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

Rob Bednark
  • 19,968
  • 18
  • 67
  • 100
Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275
5

Another mapping that will do is:

nnoremap <silent> gb :let a='binding.pry'\|put=a<cr>
MAGA
  • 6,370
  • 3
  • 14
  • 35
4

Based on Fredrik's idea, you can define and store a macro in your .vimrc, say g:

let @g = "Obinding.pry^["

Note that to type the escape character you hit CTRL-V then ESC.

You can then do @g to perform the macro.


In general, if you want to save a macro, one easy way would be to record the macro, say in register q, then do "qp (where q is the macro name) to paste the macro. Then surround it with

let @x = "..."

where x is the macro name you want it to always have and put it in the .vimrc file.

Community
  • 1
  • 1
Shahbaz
  • 42,684
  • 17
  • 103
  • 166
  • 2
    For more information see [Saving vim macros](http://stackoverflow.com/questions/2024443/saving-vim-macros) – timss Jun 13 '13 at 12:08
  • For the escape character, this worked for me: In Vim Insert Mode hit `ctrl+q` then `ESC` – csebryam Sep 26 '18 at 19:42
  • here is what I'm using: `let @b="obinding.pry^[0"` Use lowercase 'o' to insert below current line. Use 0 (zero) to go to the beginning of the line. – csebryam Sep 26 '18 at 19:48
1

A great quandary I found myself in. To solve this I placed the following mappings in my .vimrc:

imap <C-b> binding.pry 
nnoremap <leader>bp O<% binding.pry %><esc>

The first allows me to use to insert a binding.pry when already in insert mode.

The second lets me use my leader+bp to place a binding.pry above the current line.

Shadoath
  • 752
  • 13
  • 25
1

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

  1. :vsplit $MYVIMRC
  2. i
  3. nnoremap bi obinding.pry<cr><esc>
  4. <esc>
  5. :w
  6. :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 <b><i> (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<cr> - is the text which will be writen
  • <esc> - Push escape to leave insert mode.