Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a line of text/code on keypress in Sublime

Tags:

sublimetext

I want to be able to insert import ipdb; pdb.set_trace() on the line below where I am on a key press. For obvious reasons ;-)

like image 697
Sjoerd Avatar asked Dec 15 '11 15:12

Sjoerd


2 Answers

Two fairly easy ways to do this:

Create a snippet... go to Tools>Developer>New snippet, bung this in:

<snippet>
    <content><![CDATA[import ipdb; pdb.set_trace()]]></content>
    <tabTrigger>p</tabTrigger>
</snippet>

save it.. should start working straight away.

  • Ctrl + Enter (puts you onto a new next line),
  • p + Tab (inserts your snippet).

4 key strokes.

OR

You could use the macro tool:

  • Ctrl+Alt+Q (start recording macro)
  • Ctrl+Enter (puts you onto a new next line)
  • type: import ipdb; pdb.set_trace()
  • Ctrl+Alt+Q (end recording macro)

Now save your_macro.sublime-macro (Tools>Save macro), and add this key binding to your user key bindings:

{ "keys": ["p"], "command": "run_macro_file", 
"args": {"file": "Packages/User/your_macro.sublime-macro"} }

now 'p' will insert your text, on the next line..

1 key stroke... but you must be careful to write code that never needs the letter p... ;)

like image 151
fraxel Avatar answered Oct 23 '22 12:10

fraxel


On Mac OS X I have to save it with extension:

.sublime-snippet

to get it working.

like image 3
andilabs Avatar answered Oct 23 '22 14:10

andilabs