Custom VIM Modifications for Steemit

in #vim7 years ago

Custom VIM Modifications for Steemit

syntax file

I like to use Vim for, well, everything. I edit my posts in Vim before I submit them through the Steemit website. I made a few customizations that I think are worth sharing with the handful of other Vim users out there.


The two features I'm sharing today are:

  1. Extra syntax highlights for hashtags and user references
  2. Automatically commit edits to a Git repository

Extra Highlights for @users and #hashtags

I wanted stuff like hash tags and user call outs to be highlighted in addition to supporting all the markdown syntax formatting stuff. So I created a new file type that uses the .steem extension. This new file type makes use of all the Markdown style highlighting and adds a couple of more options.

illustrating the call outs

~/.vim/syntax/steem.vim

I created a directory mkdir ~/.vim/syntax/ and in there I added a steem.vim file that contains the following:

" Vim syntax file
" Language:     Markdown
" Maintainer:  Nobody... sorry.
" Filenames: *.steem

if exists("b:current_syntax")
  finish
endif

if !exists('main_syntax')
  let main_syntax = 'steem'
endif

runtime! syntax/markdown.vim

" Make any @foo style elements stand out because they're probably intentional
" references to users/people.
highlight link CallOut MoreMsg
syn match CallOut /@[a-zA-Z0-9.-]\+/

" Make any #foo style elements stand out because they're probably intentional
" references to hash tags.
highlight link HashTag Label
syn match HashTag /#[a-zA-Z0-9.-]\+/

let b:current_syntax = "steem"
if main_syntax ==# 'steem'
  unlet main_syntax
endif

Here are the components and what they do.

" Vim syntax file
" Language:     Markdown
" Maintainer:   Nobody... sorry.
" Filenames:    *.steem

This is a comment block that tells anyone reading the file what it's for.

if exists("b:current_syntax")
  finish
endif

This is a bit of boilerplate that was copied from another .vim syntax file. I assume it makes the script more modular, for instance, if the script were going to be used by another script but only to provide highlighting for a region instead of an entire file. Like when you have a code block for one language in a file that's another language.

if !exists('main_syntax')
  let main_syntax = 'steem'
endif

A little more boiler plate that sets the syntax name, but only if their isn't some other syntax name already in place.

runtime! syntax/markdown.vim

This makes it honor any Markdown syntax highlighting that was installed with the platform. It's why I wont have to implement all of the Markdown formatting in my own syntax file, just the extra stuff I want to add.

" Make any @foo style elements stand out because they're probably intentional
" references to users/people.
highlight link CallOut MoreMsg
syn match CallOut /@[a-zA-Z0-9.-]\+/

Here I define a highlight group that's called CallOut but instead of defining it to have explicit colors, I tell it to just use the existing colors for the group named MoreMsg. Then I define a regular expression that will match against any sub string that begins with an at sign and is followed by at least one or more of any a to z in upper or lower case, number from zero to nine, optionally containing dots or dashes. I don't actually know what constitutes valid user names on Steem and this is just a guess, but it's probably pretty close to being correct if it's not exactly correct.

" Make any #foo style elements stand out because they're probably intentional
" references to hash tags.
highlight link HashTag Label
syn match HashTag /#[a-zA-Z0-9.-]\+/

Much like the previous block, this defines something called HashTag that will use the same syntax highlighting as the Label group. And then the HashTag group will match anything that begins with a hash tag.

let b:current_syntax = "steem"
if main_syntax ==# 'steem'
  unlet main_syntax
endif

This block is more boilerplate, it sets the buffer's language to Steem and un-sets the assigned main_syntax, but I don't know the full ramifications of this.

steemit/.vimrc

So that these syntax changes are applied to any .steem file, I put the set exrc option in my ~/.vimrc file so that any project directories containing a .vimrc file will have their code executed. Then I create a .vimrc file in my Steemit posts directory and I put the following in there:

set textwidth=0
autocmd BufRead,BufNewFile *.steem setfiletype steem

The first line disables word wrapping because Steemit is broken with interpreting word wraps correctly, see here. The second line causes any file with the extension of .steem to use my custom steem.vim syntax settings.

Automatically commit edits to a Git repository

I make a lot of edits to a file before I'm ready to post. A LOT of edits. But when I'm doing this, I don't always do it correctly. Sometimes I decide after I've removed something that I actually want to put it back in. So I've created a Git repository out of my steemit posts directory. This was pretty easy, I just did git init . inside the directory and it's a Git repo. Every time I save a file, I have vim automatically commit it to the repository. That way, when I decide later on that I want to put something back in, I can just compare the current version of the file with previous versions. I can also just view the differences over time to see if my changes overall have improved the article or made it worse.

Anyway, I accomplish this with another autocmd in the .vimrc file in my steemit directory. Here's the command:

autocmd BufWritePost * execute '!git add % && git commit -m "saving % changes"'

This will execute two shell commands upon saving a file, the first adds the file to staging, and the second commits the file with the comment saving <filename> changes.

For example, here's the history for this file as it stands now:

commit 6ac42fb08e211c220125bc77b84bf6b259f8efec
Author: not-a-bird <not-a-bird@>
Date:   Fri Nov 10 11:08:55 2017 -0500

    saving vim-custom-steem-syntax.steem changes

commit 10bc3c48630b68bb2ecde0f674a444014edb906a
Author: not-a-bird <not-a-bird@>
Date:   Fri Nov 10 11:06:47 2017 -0500

    saving vim-custom-steem-syntax.steem changes

commit 51f4eab43425e2b0b0936a249d901e350924c78b
Author: not-a-bird <not-a-bird@>
Date:   Fri Nov 10 11:05:14 2017 -0500

    saving vim-custom-steem-syntax.steem changes

And I can easily compare the current version of the file to the previous.

For example:

diff

Other Customizations

That's pretty much it, really. I haven't though of anything else to do with the Steem syntax. If anyone else out there has made any customizations for editing their Steem posts, I'd be glad to hear about them.

Sort:  

I enjoyed finding and reading this post. I am curious about one thing though. At the end you mentioned comparing versions of the file. The graphic looks like you might be using vimdiff but I was curious how exactly you accomplish this. I am assuming that you are comparing the same file from different commits?

You assume correctly.

In the case of the above, the following command would generate that output:

git difftool f7c2fe7ca6f58576fcf0b586ebee4edaea580259..659bd91052ffd3bdc223eabfcff775eb51ed3c8f -- vim-custom-steem-syntax.steem

At the time that I was editing the file, the most recent change was most likely f7c2fe7ca6f58576fcf0b586ebee4edaea580259 and so I could have pulled up the comparison with just one hash as the target, 6ac42fb08e211c220125bc77b84bf6b259f8efec, in which case it would look like this:

git difftool 6ac42fb08e211c220125bc77b84bf6b259f8efec -- vim-custom-steem-syntax.steem

But that's a lot of typing, so I probably only did the first few characters of the hash and let it prompt me for each file to view, or:

git difftool 6ac42f

This works because my diff tool of choice is set to vimdiff. Otherwise you'd have to type the slightly longer:

git difftool --tool=vimdiff 6ac42f

Or, if you like guis:

git difftool --tool=gvimdiff 6ac42f

Note that I couldn't just do git difftool because the changes were committed on save, so I had to pass some hash in to get diff output, otherwise there wouldn't be a difference as the content would already be committed.

Now, if you are wondering why it looks like the commit message is in the diff, that's actually because the article text contained a quote of a previous commit message, that's not the revision history of the current diff, but rather the file content being diffed.

Спасибо спасибо очень в тему и облегчает понимание

Nice post. 👍

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.030
BTC 59519.43
ETH 2458.33
USDT 1.00
SBD 2.47