Skip to content

Instantly share code, notes, and snippets.

@nervetattoo
Created September 6, 2012 08:13
Show Gist options
  • Save nervetattoo/3652878 to your computer and use it in GitHub Desktop.
Save nervetattoo/3652878 to your computer and use it in GitHub Desktop.
console.log in vim
" Console log from insert mode; Puts focus inside parentheses
imap cll console.log();<Esc>==f(a
" Console log from visual mode on next line, puts visual selection inside parentheses
vmap cll yocll<Esc>p
" Console log from normal mode, inserted on next line with word your on inside parentheses
nmap cll yiwocll<Esc>p
@rob137
Copy link

rob137 commented Jul 8, 2020

Thank you!

@cmdcolin
Copy link

cmdcolin commented Jul 14, 2020

Here is a modified console log vim inserter

imap cll console.log({});<Esc>==f{a  

this makes it so that you have

 console.log({})

With the cursor inside the curlies, because then if you type

console.log({myvariable})

Then the console log contains

{myvariable:3}

That way you don't have to do things like

console.log('myvariable', myvariable)

@7iibob
Copy link

7iibob commented Oct 30, 2020

nnoremap gll oconsole.log("LINE: =line('.')","")F"i

Then hit gll to produce console.log("LINE: line","").

@darkmasta
Copy link

darkmasta commented Apr 1, 2021

How to run this in VSCode VIm Plugin? @nervetattoo

@delbetu
Copy link

delbetu commented Oct 14, 2021

nnoremap gll oconsole.log("LINE: =line('.')","")F"i

Then hit gll to produce console.log("LINE: line","").

This didn't work for me (in neovim) the =line('.') doesn't get evaluated.

@cmdcolin
Copy link

cmdcolin commented Jun 3, 2022

one maybe more "modern" alternative to this gist (which i used happily for years already, but wanted to try out something new) is to use "snippet" plugins like https://github.com/hrsh7th/vim-vsnip

Procedure:
Install vim-vsnip plugin
Run :VsnipOpen
Select type global (or filetype specific)
This opens a blank file where you can put your snippets
Put this in the snip file

{
  "console.log": {
    "prefix": "cl",
    "body": "console.log(${0})"
  }
}

console.log is the name of the snippet. you can add more to the file for custom usages. Then while you are typing in insert mode

cl<Tab>

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