Writing WordPress posts in Vim

design PostADay software thoughts vim writing

Because I have been keeping a journal electronically for so long, I standardized on plain text files. After all, just about everything can read plain text (txt) files.

At one of my first programming jobs, I was then required to use Vim, and once I got the hang of it, it became my prefered way to deal with plain text whenever I could. And, once I found VimWiki, and moved my text based journal over to that, I have been using it ever since.

Recently, I found out that the company that hosts some websites for me has WP-CLI (the WordPress Command Line Interface) installed by default. And, that started me wondering if there were a simple way to go from a local journal entry that I quite liked, to a draft of a post in WordPress while avoiding the usual copy/paste route. This is what I figured out…

The Code

First, I added this to my .vimrc file

"Make new WordPress draft from VimWiki entry

function! PublishPost()
   :terminal ++close scp -Cpr % yoursite.com:www/new-post.txt
   :terminal ++close wget -qO- https://yoursite.com/vim.php?wp
endfunction

map <silent><leader>P <esc>:call PublishPost()<cr> 

The function PublishPost() uses [SCP (Secure Copy Protocol)](https://en.wikipedia.org/wiki/Secure_copy_protocol) to copy the currently open file (using Vim notation, the % symbol) and copy it to the server as new-post.txt.

Once that completes successfully, a php file on the server is pinged with a $\_GET variable named wp.

This function is called when I hit LEADER+SHIFT+P (for Publish Post).

Now, onto the vim.php file…
```

```
```

This checks if $\_GET has a variable named “wp” set (Yes, you should set it to a specific value to increase security 🙂 ).

It then runs WP-CLI with a simple post create command without any other modifiers.

Finally, it deletes the new-post.txt file from the server.

Was this worth the trouble?
---------------------------

At first, I wondered that too. After all, this is just replacing the following workflow:

1. Select All in a Journal Entry
2. Copy Journal Entry
3. Open WordPress
4. Open New Post
5. Paste Journal Entry
6. Save Draft of Entry
7. Close WordPress

But, what I am finding, is that having a single command to issue when I feel that I have written something worth sharing, that creates the draft, hidden in the background, ready for me to deal with the next time that I log into the website, seems to short circuit my inner critic.

This way, I have divided the work of creation (which happens distraction-free within Vim) and the work of editing (which happens later on within WordPress). The two thought processes are quite different from each other. And, I find that dividing the two the best I can, helps me to do both of them better.

Previous Post Next Post