Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On keystroke insert line of code in (mac)vim, for pdb

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

like image 315
Sjoerd Avatar asked Jul 01 '11 12:07

Sjoerd


3 Answers

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>
like image 81
Magnun Leno Avatar answered Nov 02 '22 20:11

Magnun Leno


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

like image 24
Spencer Rathbun Avatar answered Nov 02 '22 20:11

Spencer Rathbun


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
like image 5
Bitterzoet Avatar answered Nov 02 '22 21:11

Bitterzoet