Create a new file at the right place with prefilled content thanks to Jetbrains IDEs

| 3 min read

It’s been a while since I felt that pain...
I had to manually create a file for every blog post in the correct directory, repeating information based on the title in snake case, using the current date, with the same structure that I copied from one old post to the new one and manually edited.

That’s just too long, or at least super interesting and error-prone.

I wanted to have a template for my blog post article. My articles are written in an index.md stored in directories including the current dates and using the article’s title in snake case.

For instance, the article you’re currently reading is stored in /articles/2024-09-13-create-a-new-file-at-the-right-place-with-prefilled-content-thanks-to-jetbrains-ides/index.md

Creating the directory and file, setting up some information about the permalink, and linking to the social image—all this after converting the article title to snake case—could probably be easier.

For my Burritalks project, I created a small CLI in node that helps me set up pages faster. I could do the same here, but a question popped into my mind:

Could Jetbrains IDE automate part of this?

Of course!

Here is what you can get:

I tested that on Webstorm, but I’m pretty sure it would work on all their other IDEs. You can try that on Intellij, Rider, PhpStorm, PyCharm, Goland, ...

Here is how it works:

In your IDE settings, go to Editor > File and Code Templates.

Create a new template by clicking on + and giving it a name. In my case, I need to create a new blog post article so I went with Article.

Automating the content of the article

The File template system makes use of Apache Velocity template, and comes with a set of prepared variables.

In my case, I needed ${YEAR}, ${MONTH}, and ${DAY} for the first part of my article path.

You can also add custom variables. If the IDE finds a variable it doesn’t know when the template is applied, it will ask for a value. For example, I defined a $TITLE variable to get the title of the article.

Even, better, you can run some functions on your variable. A good thing when you want to turn the title to snake-case !

I defined a new variable, $TODASH, based on the $TITLE one, which makes it lowercase and replaces spaces with a hyphen: #set( $TODASH = $TITLE.toLowerCase().replaceAll(" ", "-")).

My complete template looks like so:

#set( $TODASH = $TITLE.toLowerCase().replaceAll(" ", "-"))
---

title: ${TITLE}
description: ""
permalink: /articles/${TODASH}/
socialImage: /articles/${YEAR}-${MONTH}-${DAY}-${TODASH}/social.png
language: en
tags:
-
---

This is already super cool and saves some time, but I still needed to create the directory with the date and a snake-case version of the title.

It turns out that can be automated as well!

Automating the directory creation

The File Name input runs the template systems, which means we can use Apache Velocity variables and functions there too.

Do you speak French and want to stop hating your tests ?

I've created a course to help developers to improve their automated tests.

I share ideas and technics to improve slow, flaky, failing for unexpected reason and hard to understand tests until they become tests we take joy to work with !

But you'll need to understand French...

Here is the file name I defined :

src/articles/${YEAR}-${MONTH}-${DAY}-$TITLE.toLowerCase().replaceAll(" ", "-")/index

See how it uses the same ideas as in the body template

The extension, .md, must be set in the Extension input to avoid doubling it.

Now, I just need to add a new "Article", type the name, and I get the index.md file right at the right place and part of the front matter data already setup.

And it’s time to start typing!

One reason is to publish a new article; let’s keep them coming!

That trick may also help you save time in your daily workflow. I always enjoy teaching people how to make better use of their IDEs. I’m pretty sure that if we spend a day together, you’ll learn a few things that will improve your way of working, especially if you want to become an expert at using refactoring tools. If you’re curious about that, let’s talk!