# Quickly Edit Posts


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.

<table><td>
<a class="onelinelink" href="https://waylonwalker.com/automating-my-post-starter/">
<img style="float: right;" align='right' src="https://waylonwalker.com/static/fd0bd45e6a1500a02b7f796ae7294550/630fb/quickly-edit-posts-xmas2020.png" alt="article cover Quickly Edit Posts">
<div class="right">
    <h2>Quickly Edit Posts</h2>
    <p class="description">
Recently I automated starting new posts with a python script. Today I want to work on the next part that is editing tho
    </p>
    <p class="url">
    <span class="read-more">read more</span>  waylonwalker.com
    </p>
</div>
</a>
</td></table>

> 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
1. fuzzy find the post
1. open it with vim
1. 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 every time you source your `~/.alias` file which is not what we want.

Let's set up the **boilerplate**.  It's 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.

_<small><mark>boilerplate</mark></small>_
``` bash
ep () {
    _dir=$(pwd)
    _ep () {
        # open file here
    }
    _ep && cd $_dir || cd $_dir
}
```

<table><td>
<a class="onelinelink" href="https://waylonwalker.com/reusable-bash/">
<img style="float: right;" align='right' src="https://waylonwalker.com/static/96bd1b466e9e00834d0d7eda8af04b97/630fb/reusable-bash-xmas2020.png" alt="article cover for Creating Reusable Bash Scripts">
<div class="right">
    <h2>Creating Reusable Bash Scripts</h2>
    <p class="description">
    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.
    </p>
    <p class="url">
    <span class="read-more">read more</span>  waylonwalker.com
    </p>
</div>
</a>
</td></table>

> 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.

_<small><mark>cd to where I want to edit from</mark></small>_
``` bash
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.

_<small><mark>find all markdown</mark></small>_
``` bash
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.


_<small><mark>putting together the edit command</mark></small>_
``` bash
$EDITOR $(find ~/git/waylonwalkerv2/src/pages/ | grep .md$ | fzf)
```

## Final Script

_<small><mark>final ep function</mark></small>_
``` bash
ep () {
    _dir=$(pwd)
    _ep () {
        cd ~/git/waylonwalkerv2/
        $EDITOR $(find ~/git/waylonwalkerv2/src/pages/ | grep .md$ | fzf)
    }
    _ep && cd $_dir || cd $_dir
}
```
