13

I'm looking for the way to insert a line of code with a keystroke like leaderp in Macvim

I want to insert the following line of code:

import pdb; pdb.set_trace()

Probably not an unheard of line of code in python land

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sjoerd
  • 257
  • 2
  • 9

4 Answers4

12

I'd use a simple mapping (without functions) to leader p:

nnoremap <leader>p oimport pdb; pdb.set_trace()<Esc>

When pressing o, this enters insert mode inserts a blank line after the current one (with o) and then types import pdb; pdb.set_trace(), finally it goes back to normal mode (with Esq).


If you want to insert the code before the current line replace o by O:

nnoremap <leader>p Oimport pdb; pdb.set_trace()<Esc>

Or alternatively you could set this for leader shift-p:

nnoremap <leader><S-p> Oimport pdb; pdb.set_trace()<Esc>
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Magnun Leno
  • 2,728
  • 20
  • 29
  • 3
    I used a variation of this answer to insert above the current line and use the same level of indent, regardless of any indent plugins: `nnoremap p yyP^Cimport pdb; pdb.set_trace()^[` (the ^C isn't "control-v C", it's just "^" then "C"). – Will Hardy Apr 10 '13 at 11:23
  • The `^[` at the end didn't expand to ESC for me in gvim on Windows (but instead was just printed to the buffer). Using `` instead solved this problem. – Zakum Jan 19 '14 at 15:54
  • 1
    @Zakum mind the explanation on how to insert the `^[` character on the third paragraph. – Magnun Leno Jan 20 '14 at 15:39
  • Right, didn't notice! Any advantages of using `^[`to ``? – Zakum Jan 21 '14 at 00:05
  • @Zakum not really (as far as I'm concerned). I just find it easier to type :) – Magnun Leno Jan 22 '14 at 10:31
6

Why not try the vimpdb plugin? Alternatively, if your looking for snippet functionality, the combination of the supertab and snipmate plugins works great.

ib.
  • 27,830
  • 11
  • 80
  • 100
Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
  • I love vim, but I wanted an ide feature set. These three plugins, plus project, conqueterm, taglist, and a couple other minor ones give me the best of both worlds. And I have a working ide for specialized "languages" that I have to work with. Eclipse won't do psl. – Spencer Rathbun Jul 01 '11 at 14:43
5

This might not be the best vimscript every but it does wat you want! :-) Just place this in your .vimrc and you can call it with leader p.

map <Leader>p :call InsertLine()<CR>

function! InsertLine()
  let trace = expand("import pdb; pdb.set_trace()")
  execute "normal o".trace
endfunction
Bitterzoet
  • 2,752
  • 20
  • 14
2

Using registers?

write that line somewhere and copy it to register p, then use "pp to print it

import pdb; pdb.set_trace()

"pY

"pp
import pdb; pdb.set_trace()

or use abbreviations

:ab teh the
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130