Aliasing g

This isn’t so much of a Vim tip (sorry), but a terminal tip that has made a lot of difference to my workflow recently, so much so that I’ve bent the rules slightly and posted it today.

This one is courtesy of Ben Orenstein’s dotfiles, from which I have picked up a lot of good things, including this shell function:

function g {
   if [[ $# 0 ]]; then
     git $@
   else
     git status
   fi
}

This does a really simple thing, and remaps g on your command line. If you call g on its own, you’ll call git status, but if you pass it an argument, it will pass that through to git.

For example:

g =git status
g diff =git diff
g commit =git commit

Of course, you should also set up some aliases for common commands to save yourself more typing.

comments powered by Disqus… Click Here

Making `j` and `k` work on visual lines

Sorry for the hiatus – conferences have kept me busy. Posts will return to mostly daily rate from now on.

Four of the most important lines in my vimrc file are:

nnoremap k gk
nnoremap j gj
nnoremap gk k
nnoremap gj j

You see by default, k and j go up and down physical lines, not visual lines. What this means is that if you’ve one line but it wraps and spans more than one on your screen, j or k will jump up or down to the next physical line, and skip the wrapped lines of the current line you’re on. This is a bit confusing, so hopefully this helps:

1 this is the first line
2 this is the second line but imagine
3 it has been wrapped {C}around because
4 you're on a really small screen
5 and it wont fit on one line

If you imagine your cursor is where the {C} is and you press k, you wont end up on line 2 as you might expect, but on line 1, because k has gone to the next physical line, which is line 1. It doesn’t respect line wraps. However, gk does respect line wraps, so if you’re on line 3 and hit gk, you will end up on line 2. Personally I prefer this behaviour, so these four mappings swap k to work like gk and gk to work like the default k behaviour, and then obviously the same for j.

nnoremap k gk
nnoremap j gj
nnoremap gk k
nnoremap gj j

Thanks to Yubin Kim for his collection of Vim tips, which is where I stole that mapping from.… Click Here