jsborjesson Yet another dev blog

Snippet reminders

There are some pieces of code, or a specific format of writing something that you need frequently, but rarely enough to have to look them up, or at least think for a few extra seconds to remember. A good way to mitigate this interruption is to encode this knowledge in a snippet - even if they are short and easy to type, but just hard to remember.

Here are a few examples I use, with a link to the definition underneath. I use UltiSnips but they should be trivial to adapt to any other snippet system.

alias argument order

alias_method :{new}, :{old}

Source

Before this, I could never remember which argument came first.

shebang

#!/usr/bin/env {bash}

Source

I could never seem to get the first #, !, / in the right order, and then I had to remember which path env was in - no more!

cron training wheels

This one is a bit bigger, the cron syntax is very succinct but unless you use it often there's a good chance to forget - therefore I have a snippet to insert some labels in a comment for me:

  *   *   *   *   * {command}
# |   |   |   |   |
# |   |   |   |   +----- day of week (0 - 7) (Sunday is 0 or 7)
# |   |   |   +------- month (1 - 12)
# |   |   +--------- day of month (1 - 31)
# |   +----------- hour (0 - 23)
# +------------- min (0 - 59)}

Source

The few times I use cron, this is a lifesaver - I also have the entire help-comment as a placeholder so in case I just want it there while typing it out, I can just do tab-backspace to get rid of it when I'm finished.

Vim spell checking for dummies

:set spell will mark misspelled words in red, but this is not Microsoft Word. Here are a few tricks I use to work effectively with the Vim spell checker:

  • If you have vim-unimpaired installed you can toggle spell checking on and off with cos

  • Usually though, whether you want it enabled depends on the file type you are editing. You can put setlocal spell in the appropriate vim/after/ftplugin/{filetype}.vim, probably at least markdown and gitcommit.

  • ]s will take you to the next misspelled word.

  • z= will bring up a list of suggestions for correcting the word under the cursor

crontab: temp file must be edited in place

If you try to add a cron job with crontab -e using Vim, without any configuration, you will probably get this error message:

crontab: temp file must be edited in place

Since Vim is one of, if not the most commonly used $EDITOR, it is a bit surprising that this does not work out of the box. It has to do with the way Vim handles backup files, turning that feature off (only for crontab files) will fix the issue.

TL;DR: put this in your .vim/after/ftplugin/crontab.vim:

setlocal nobackup nowritebackup

Vim folding for dummies

Vim has a really powerful mechanism for folding, it has lots of different commands and settings you can tweak to make it behave just the way you want it - but sometimes you just want something out of your way.

  • zf - folds the region selected by a motion or from visual mode (to fold a paragraph for example do zfip or vipzf)
  • zo - opens the fold under the cursor
  • zR - opens every fold

For light, occasional use of folds, this is all you need. No settings required.

Quick help in vimrc

Normally, K will show you the man-page of the word under the cursor. It's a nifty feature, but man is not always the most relevant command.

You can bind it to open the :help in Vim, when you are in a VimScript file, this is very handy when you are working on your vimrc and need a quick explanation of what a setting does.

Put this in .vim/after/ftplugin/vim.vim:

nnoremap <buffer> K :help <C-r><C-w><CR>
xnoremap <buffer> K "xy:help <C-r>x<CR>

Update

2020-08-13

This should be done by setting keywordprg=:help, which is already the default in NeoVim.