I’ve got a little keybinding in my .vimrc
file which inserts a debug statement into a file, and then saves it;
So, if I’m editing foo.rb
with my cursor on line 2;
Then if I hit ,db
the file will look like this;
Saving the file triggers any test runner process that might be watching the file for changes.
FYI the ; 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.
This is great for ruby and javascript, but for other languages like elixir, I need to insert a different statement.
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 ~/.vim/scripts/debug.vim
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.