Last active
October 15, 2016 22:19
-
-
Save eremzeit/9be3c2d4ff97895e50fe3620132b9f91 to your computer and use it in GitHub Desktop.
A quick hack for for debugging JS in Vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" If you frequently find yourself printing values to console in JS for | |
" debugging, add the line below to your .vimrc file. It sets you up | |
" so that if you do <Leader>d then whatever you had yanked to the | |
" default buffer will be put into a console.log statement. | |
" Trust me. It's hacky (and could be better written to handle quotes), | |
" but saves a ton of time. | |
" | |
" (For more info on leader keys, see: http://learnvimscriptthehardway.stevelosh.com/chapters/06.html) | |
" | |
" Example: | |
" If you had yanked `this.myVariable` and then type <Leader>d, it would | |
" paste the following into the next line: | |
" | |
" console.log('this.myVariable: ', this.myVariable) | |
" | |
autocmd BufEnter *.js :map <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your mapping should be buffer-local to prevent it from leaking to other non-JavaScript buffers:
:map
is too greedy, you should restrict your mapping to a single mode:Since you don't use another mapping in your mapping you should make it non-recursive to improve your setup's predictability:
The
BufEnter
event will happen a lot more than necessary. Use a more appropriate event:If you re-source your
vimrc
often, your autocommand is going to be repeated many times for no good reason. You should include it in a properly cleaned-up autocommand group:But Vim already does all that for you so there's no need to pollute your
vimrc
with unnecessary logic. Create~/.vim/after/ftplugin/javascript.vim
if it doesn't exist and put that line in it (without the colon because it's totally unecessary:Since we are in normal mode, the first
<Esc>
serves no purpose whatsoever:$a
is an anti-pattern. UseA
instead:All those
<Esc>p
are wasteful. You can use<C-r>"
in insert mode to insert the content of the unnamed register:Since we are at it, let's add a visual mode mapping so that we don't even need to yank:
Neat.
Bonus, let's bypass insert mode completely: