Robert Basic's blog

Archives for March, 2016

GitHub flavoured code fences in Hugo

by Robert Basic on March 28, 2016.

This was an undocumented feature until today, so I missed it when I was converting my site to Hugo last week. It is also possible to highlight code examples with GitHub flavoured code fences, or, in other words, with triple backticks ```.

I like this a lot because it makes highlighting code in posts easier. Typing the {{< highlight >}} shortcode is just awkward and I always end up forgetting either the angle brackets or add too much or too little currly brackets. Backticks are much nicer.

The code fences are not enabled by default, though. We need to set PygmentsCodeFences to true in Hugo’s configuration file and that’s about it. Everything else about syntax highlighting stays the same.

Change to backticks in old posts

I used these two simple sed one-liners to change all the highlighting shortcodes to the code fences:

find -name "*.md" -print0 | xargs -0 sed -i 's/{{< highlight \([a-z]*\) >}}/``` \1/g'
find -name "*.md" -print0 | xargs -0 sed -i 's/{{< \/highlight >}}/```/g'

Now I don’t even need a custom Vim function to insert the highlighting shortcode. Sweet.

Tags: highlight, hugo, syntax.
Categories: Development, Programming, Software.

Need help on your PHP projects? Let's talk!

Vim Hugo helper

by Robert Basic on March 25, 2016.

I think I just wrote my first Vim plugin. OK, it’s more a bunch of Vim functions slapped together than an actual plugin, but gotta start somewhere, right?

Last week I converted this blog to a static web site and I’m using Hugo as the static website engine. Writing posts is a lot easier now, plus it’s written in Go, which I started learning a few weeks ago.

Vim Hugo helper is a plugin, collection of Vim functions that help me while writing posts. It will hopefully speed up a few recurring tasks such as drafting and undrafting posts, inserting code highlight blocks, etc. I guess I’ll be adding more to it in the future, but for now it has a total of 5 functions.

Front matter reorder

Hugo posts have a front matter which is basically meta data for the content. Hugo will create the front matter out of an archetype which is a template of sorts for the front matter. One thing I don’t like is that when a new post is created, the meta data is sorted alphabetically so the HugoHelperFrontMatterReorder function reorders it in the way that I do like.

Drafting/undrafting posts

The HugoHelperDraft and HugoHelperUndraft functions simply draft and undraft posts by setting the draft meta data in the front matter to either true or false.

Code highlight blocks

The HugoHelperHighlight(‘language’) function inserts the highlight shortcode that comes built-in with Hugo and sets the language of the highlight block.

Setting the date of the post

Finally, the HugoHelperDateIsNow function sets the date meta data of the post to the current date and time.

It’s not much, definitely has room for improvements, but I used it even when writing this very post, so I guess this helper is helpful.

Tags: hugo, plugin, vim.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!

Find duplicate lines from a CSV

by Robert Basic on March 21, 2016.

Finding duplicate lines from a CSV file is something I have to do from time to time, yet not on a regular enough basis to remember it all. Plus, I’m trying to blog more often.

cut -d, -f1 file.csv | tr -d '"' | sort | uniq -dc

cut to split the lines at the commas and select the first field, then tr to delete any double quotes that encloses the field, then sort and finally with uniq to show only the duplicated lines and to prefix every line with the count of occurrences.

Tags: linux, shell.
Categories: Development.

Need help on your PHP projects? Let's talk!

Tags for PHP in Vim

by Robert Basic on March 09, 2016.

One thing I was missing for a long time in Vim is to be able to “jump to definition” in an easy and painless way.

The other thing I wanted to improve is to be able to tell easily where am I actually in the code base; to see the current class and method name of wherever the cursor was.

With a bit of googling and poking around, I finally came up with a perfect combo of 5 plugins (yep, five!) that enables me to do both, and a little bit of extra.

Tags made easy

Gutentags  is a brilliant Vim plugin that makes it so easy to have tags. Just install the plugin and boom! Tags! It will figure out things on it’s own and just generate the tags in the background. I use it daily for a fairly large code base and I never had any problems with the tags, or with Vim being unresponsive while the tags are being generated.

The only two settings I have set for it is what to exclude and where to store the tag files to not pollute the current project with them.

let g:gutentags_exclude = ['*.css', '*.html', '*.js']
let g:gutentags_cache_dir = '~/.vim/gutentags'

That’s it.

Jump to definition

Pair gutentags with CtrlP and it’s CtrlPTag method and we get jump to definition.

map <silent> <leader>jd :CtrlPTag<cr><c-\>w

Move to the method name we’re interested in, hit <leader>jd and it will jump to it’s definition. Tip: <C->w means “insert word under cursor”.

Where the hell am I?

My second requirement for displaying the current class and method name was a bit more difficult to fulfill. It takes the tagbar, tagbar-phpctags and lightline plugins as well as the phpctags tag generator to accomplish that.

Let the tagbar plugin know where the phpctags bin is:

let g:tagbar_phpctags_bin='~/.vim/phpctags'

This will make the tagbar plugin use phpctags to generate tags for the current file. To finally display the current tag in lightline, I wrote a simple tagbar component for it:

'tagbar': '%{tagbar#currenttag("[%s]", "", "f")}'

My complete lightline settings at the time of writing this can be found here.

Tags: gutentags, lightline, php, tagbar, tags, vim.
Categories: Development, Programming, Software.

Need help on your PHP projects? Let's talk!