Intellij Live template for console.log(‘’) that will improve your daily development life!

We’ve all been there. Our code is not behaving like we hoped it would and it is not obvious why. The easiest and fastest way to get a clue for what’s going on is to simply put a print statement in the code. It’s not nice, we should use a proper debugger… but we do it anyway.

The following live-template will add the console.log(‘’) statement to your code, automatically insert the class name, function name, and line number in the log message, offers code completion for the message you want to log and places the cursor where you need it.

According to Larry Wall, the original author of the Perl programming language:

One of the three great virtues of a programmer is laziness.¹

So let’s automate the boring stuff!

There’s a video showing the set up and usage of the live template further down. You can pause and skip parts of the video or just follow the instructions below.

Steps to follow:

live template preferences:

  • Go to Preferences( ⌘ + , )
  • select Editor / Live Templates (you can type ‘live templates’ to find it)
  • select Javascript
  • press the ‘+’ sign at the top right corner of the window( ⌘ + N )
  • select ‘Live Template’( press 1 or Enter)

edit template:

  • enter the abbreviation you want to assign to the template. We will use ‘cl’ in this case.
  • enter the description for the template (e.g. “inserts console.log(‘’);” ).
  • Paste the following template code:
console.log('Class: $CLASS$, Function: $FUNCTION$, Line $LINE$ $PARAM_TEXT$($EXPECTED$): '
, $PARAM$);$END$
  • click on ‘Define’ below the template textfield and select the contexts where this template should apply e.g. Javascript, Typescript etc.

edit variables:

Feel free to add or leave out template variables that you (don’t) need.

  • click ‘Edit variables’
  • select ‘jsClassName()’, ‘jsMethodName()’, ‘lineNumber()’ as Expression to CLASS, FUNCTION, LINE respectively
  • add ‘PARAM’ as a default Value of PARAM_TEXT
  • check ‘Skip if defined’ for these variables
  • order variables so that PARAM comes before PARAM_TEXT and EXPECTED

Now go ahead type ‘cl’ and press Enter(or Tab if that is your default). Tada! there is our template already with line number, class and function name and the cursor is waiting for you to type the second parameter which will instantly appear in the string of the log message. By pressing Enter the cursor will jump to the next undefined parameter, type something, press Enter again and you are at the end of the line.

And there you have your log statement:


$PARAM$ is the placeholder for the expression that you want to log.

$PARAM_TEXT$ will be replaced with this expression due to the default value binding.

$CLASS$, $FUNCTION$, $LINE$ will be replaced with the className, methodName and lineNumber as specified.

$END$ indicates the position where the cursor will be placed after every variable has been set.

The order of the variables is important since is defines the order where the cursor jumps to next after pressing Enter . Special in this case is that the cursor will skip PARAM_TEXT since it will defined through PARAM before.

The mechanics used here can be applied for many other use cases like a custom logger or other general language features. Some other useful expressions that can be assigned to template variables are complete() and completeSmart() which lets you use autocomplete within strings.

I’d love to see your favourite use cases and examples in the comment section.

As usual feedback is welcome!

¹: not quite the actual quote from Larry Wall. see: