Quickly Edit Posts

edit✏️

πŸŽ„This post has fully grown


Recently I automated starting new posts with a python script. Today I want to work on the next part that is editing those posts quickly.

article cover for Automating my Post Starter

Automating my Post Starter

One thing we all dread is mundane work of getting started, and all the hoops it takes to get going. This year I want to

read more waylonwalker.com

Check out this post about setting up my posts with python 🐍

Enter Bash

For the process of editing a post I just need to open the file in vim quickly. I dont need much in the way of parsing and setting up the frontmatter. I think this is a simple job for a bash script and fzf.

  1. change to the root of my blog
  2. fuzzy find the post
  3. open it with vim
  4. change back to the directory I was in

bash function

For this I am going to go with a bash function. This is partly due to being able to track where I was and get back. Also the line with nvim will run fzf everytime you source your ~/.alias file which is not what we want.

Lets setup the boilerplate. Its going to create a function called ep "edit post", track our current directory, create a sub function _ep. Then call that function and cd back to where we were no matter if _ep fails or succeeds.

boilerplate

ep () {
    _dir=$(pwd)
    _ep () {
        # open file here
    }
    _ep && cd $_dir || cd $_dir
}
article cover for Creating Reusable Bash Scripts

Creating Reusable Bash Scripts

Bash is a language that is quite useful for automation no matter what language you write in. Bash can do so many powerful system-level tasks. Even if you are on windows these days you are likely to come across bash inside a cloud VM, Continuous Integration, or even inside of docker.

read more waylonwalker.com

check out this post for more information about writing reusable bash scripts.

FZF

Let's focus in on that _ep function here that is going to do the bulk of the work to edit the post.

cd to where I want to edit from

cd ~/git/waylonwalkerv2/

Next I need to find all markdown pages within my posts directory. There is probably a better way to filter with the find command directly, but I am not worried about perf here and I knew how to do it without google.

find all markdown

find ~/git/waylonwalkerv2/src/pages/ | grep .md$

Now that we can list all potential posts, sending the selected post back to neovim is as easy as piping those files into fzf inside of a command substitution that is called with neovim.

putting together the edit command

$EDITOR $(find ~/git/waylonwalkerv2/src/pages/ | grep .md$ | fzf)

Final Script

final ep function

ep () {
    _dir=$(pwd)
    _ep () {
        cd ~/git/waylonwalkerv2/
        $EDITOR $(find ~/git/waylonwalkerv2/src/pages/ | grep .md$ | fzf)
    }
    _ep && cd $_dir || cd $_dir
}


πŸ‘€ see an issue, edit this post on GitHub

If you found value in this post
and want to send a tip.

Buy Me A Coffee



tweet about this post and it will show up here.

    loading
←An IndieWeb Webring πŸ•ΈπŸ’β†’

.