Skip to content

Instantly share code, notes, and snippets.

@eremzeit
Last active October 15, 2016 22:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eremzeit/9be3c2d4ff97895e50fe3620132b9f91 to your computer and use it in GitHub Desktop.
Save eremzeit/9be3c2d4ff97895e50fe3620132b9f91 to your computer and use it in GitHub Desktop.
A quick hack for for debugging JS in Vim
" 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>
@romainl
Copy link

romainl commented Oct 15, 2016

  1. Your mapping should be buffer-local to prevent it from leaking to other non-JavaScript buffers:

    autocmd BufEnter *.js :map <buffer> <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    
  2. :map is too greedy, you should restrict your mapping to a single mode:

    autocmd BufEnter *.js :nmap <buffer> <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    
  3. Since you don't use another mapping in your mapping you should make it non-recursive to improve your setup's predictability:

    autocmd BufEnter *.js :nnoremap <buffer> <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    
  4. The BufEnter event will happen a lot more than necessary. Use a more appropriate event:

    autocmd FileType *.js :nnoremap <buffer> <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    
  5. 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:

    augroup JavaScriptStuff
        autocmd!
        autocmd FileType *.js :nnoremap <buffer> <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    augroup END
    
  6. 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:

    nnoremap <buffer> <Leader>d <Esc>oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    
  7. Since we are in normal mode, the first <Esc> serves no purpose whatsoever:

    nnoremap <buffer> <Leader>d oconsole.log('<Esc>p$a:', <Esc>p$a)<Esc>
    
  8. $a is an anti-pattern. Use A instead:

    nnoremap <buffer> <Leader>d oconsole.log('<Esc>pA:', <Esc>pA)<Esc>
    
  9. All those <Esc>p are wasteful. You can use <C-r>" in insert mode to insert the content of the unnamed register:

    nnoremap <buffer> <Leader>d oconsole.log('<C-r>":', <C-r>")<Esc>
    
  10. Since we are at it, let's add a visual mode mapping so that we don't even need to yank:

    xnoremap <buffer> <Leader>d yoconsole.log('<C-r>":', <C-r>")<Esc>
    

Neat.

Bonus, let's bypass insert mode completely:

nnoremap <buffer> <Leader>d :put=printf(\"console.log('%s:', %s);\", @\", @\")<CR>
xnoremap <buffer> <F5> y'>:put=printf(\"console.log('%s:', %s);\", @\", @\")<CR>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment