Tags
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.
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.
- change to the root of my blog
- fuzzy find the post
- open it with vim
- 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 }
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 }