I’ve got a little keybinding in my
.vimrc file which inserts a debug statement into a file, and then saves it;
map <Leader>db Odebugger; 1<CR><ESC>:w<CR>
So, if I’m editing
foo.rb with my cursor on line 2;
1 def hello 2 puts "Hello, world!" 3 end
Then if I hit
,db the file will look like this;
1 def hello 2 debugger; 1 3 puts "Hello, world!" 4 end
Saving the file triggers any test runner process that might be watching the file for changes.
; 1 is there in case I want to insert a debug statement at the end of a block – in that case, it gives the debugger something to stop on.
In the past, I’ve done this by creating a custom
.vimrc file for each project, in which I remap that key combination to whatever is appropriate. But, that gets pretty tedious when you have a lot of projects.
I want to define my editor config once, but have it insert whatever statement is appropriate, according to the language of the file I’m editing.
Here’s how I’ve done it.
Create a file
Then, in my .vimrc;
That’s it. The key sequence calls the
InsertDebugStatement function, which uses the current value of
filetype to insert whatever debug statement is required, and then save the file.