Easier Reindenting Wed, 24 Apr 2013
With a visual selection you can use
< to indent left by 1 tab (or spaces, depending on your settings) and
> to indent right by 1 tab. And of course we can prepend these with a motion to indent more than one tab at a time, such as
But what if you’re not sure how many you want to indent by? You can guess a number and then alter it after that, but that’s not efficient. You can use
gv to select the previous visual selection, so what I tend to do is make a selection, and use
>. If I need to indent again, I can do
gv> to indent the same selection 1 more to the right.
From this, I decided to map
> to do this for me automatically:
" reselect visual block after indent/outdent vnoremap < <gv vnoremap > >gv
This means that when I run
>, the selection I indented is selected for me again, so I can then keep hitting
> however many times I need to get the code indented just right. This is one of my favourite snippets in my .vimrc.
Note: it’s been pointed out to me that instead of this I could just use the dot command (
.), which repeats the last change. Normally I do, but in this instance I found it much easier to add the mapping above.