DEV Community

Cover image for Creating a simple CLI note app using a shell alias
Mike Buyco
Mike Buyco

Posted on • Updated on

Creating a simple CLI note app using a shell alias

Overview

I've been using the terminal for my work as a software engineer for several years now and really love the idea of doing most of my tasks in the terminal. I mainly use vim as my IDE and have spent lots of time configuring it to my work style and to improve my productivity over the years.

Don't get me wrong, I love tools like VSCode (especially the web version) and I started out using text editors like Notepad++ and Sublime Text 2. But when I learned how to use vim and configure it to become a full-fledged IDE for everyday use, I fell in love with it. Back then I was really looking into how I can write code with minimal overhead as possible.

Some might say, I'm an old school kind of guy and I'd happily take it as a compliment!

Enough with the backstory, let's see how we can take note-taking capabilities into the terminal...

Prerequisites

  • Vim 8+ or Neovim 0.5+
  • Unix shell

Creating an alias

If you're using bash, add this line to your .bashrc file:

# Quickly create a note file for today
NOTES_FILENAME="notes-$(date +%Y%m%d).txt"
alias notes="mkdir ~/notes &> /dev/null; touch ~/notes/$NOTES_FILENAME; cd ~/notes; vim ~/notes/$NOTES_FILENAME"
Enter fullscreen mode Exit fullscreen mode

Let's explain the script above...

First off, we create a NOTES_FILENAME variable with the current date formatted into the filename. So for example, today is October 10, 2022, then the file notes-20221010.txt would be created.

Second, we pass that variable to the alias notes= declaration. To learn more about aliases, check out this article.

The alias notes= declaration is like a shortcut for a shell command. So instead of running the actual commands, you save it into a command named notes. In this case, this command does 4 things:

  1. It creates a folder named notes, into the current user's home directory (~/notes)
  2. It creates a file with the name based on the NOTES_FILENAME variable we created earlier.
  3. It cds into the ~/notes directory.
  4. Runs the vim text editor for the file we created.

Notes environment

Now we can access our notes by just running the notes command in the terminal.

If you have configured vim to have a fuzzy search for project files or live grepping action, you should be able to search through your notes. And given the date format we mentioned earlier, you can search by file for a specific date. Simple enough yeah? LGTM!

Conclusion

The steps we did above is a very simple example of how we can quickly create notes in the terminal and focus on writing what's on our mind fast without leaving the terminal. If you're like me and you mostly work on the terminal and use vim as your text editor, this is a very handy tool, simple and practical.

Let me know your thoughts about this. Cheers!

Image description

Top comments (0)