DEV Community

Cover image for # Hello [DEV] World; I'm Bashing in, but All in One Bite!
Marx Marvelous
Marx Marvelous

Posted on

# Hello [DEV] World; I'm Bashing in, but All in One Bite!

Here I share a modest addition I made to a tool I found that makes it more useful for me in my desktop Manjaro Linux.

Borrow and improve, right?

First, I was looking for a bash script that would allow me to use a function in my bash terminal, calling the name as an alias to create a post in Hugo for the static website blog I am in the process of designing and creating. While I have no complaints about the normal way it is done, it just seems kinda clunky, no?

hugo new posts/

Instead of two words, why not one? And instead of a path each time, can't we DRY that? The .md extension too, while we are at it? Since hyphens are common in markdown posts, can't we automagically make those sandwich where needed too? I think so.

What I had in mind was something like hugpost my new post And I would, assuming I am in the base of my Hugo instance, expect it to create a new markdown file, in this case, in the /content directory of the that instance and open vscode in said directory with the file I just created in one short line. Why not?

I found a post in this blog post that nearly gets me there. And it is very helpful too inasmuch as the anonymous author bothers to explain the bash syntax and credits and links to their source of inspiration. Still, I wanted to be able to not just create the new post, but also push pipe it into the present working directory and pipe it again to open it in my editor of choice: vscode.

Credit where it is due, here is their script from their post, Create New Hugo Post Faster With Bash :

function hugonp() {
    hugo new posts/$(echo $str | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z
Enter fullscreen mode Exit fullscreen mode

Aside: And how about that bitesize mode they use in the blogpost too, eh? Cool tool there, I think.

Here is my ever so modest extended version, using 2 |'s, two calls (one with a switch), and Voila!:

# example:  $:>> hugpost my new post
function hugpost() {
    hugo new post/$(echo $str | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z | pwd | code -n .
Enter fullscreen mode Exit fullscreen mode

KA-Chow! Modest indeed. But it works, least on my laptop.

For those unfamiliar with bash scripting, the above would, typically, be put in the .bashrc file found as a hidden file in the base your home directory, depending on which version of Linux you are using. Once there I can simply type the following in the terminal:

hugpost my new post
Enter fullscreen mode Exit fullscreen mode

Teaser- My next post will be about my new favorite amazing learning tool: Lurnby

Stay tuned...

Top comments (0)