Despite having used ‘vi’ and its variants as my primary editor for quite a while now (ever since it was the only editor that ran well on the university’s old Sun 3s), I’ve never really bothered to learn many of the ‘advanced’ tips and tricks. Insert, delete, copy, and paste always seemed to be good enough.
I thus only just recently discovered how to indent and unindent without having to manually insert or delete spaces repeatedly. I really should have earlier since it comes up quite often while coding, but there’s always that short-term laziness…
Anyway, in ‘vim’ at least, the useful controls are:
“>>” – Indent the current line. Use a numeric prefix to apply it to a certain number of consecutive lines.
“<<” – Unindent the current line
“>%” – Indent the entire current code block. This is the really useful one, since quite often you’re cutting and pasting a block of code into a place at a different indentation level, and you want to shift the whole block. The cursor has to be positioned at the start or end of the block, like with the basic “%” command.
“<%” – Unindent the entire current coding block.
And you can also select a region with the mouse or Shift-V and use “>” and “<” alone to indent or unindent the selected block.
Somewhat related to this is the “=” command, which modifies the indentation to match up with how the current syntax mode would have indented it based on its own built-in rules and environment settings.
“==” – Automatically adjust the indentation of the current line.
“=%” – Automatically adjust the indentation of everything within the current code block, including other blocks within this one.
And again, selecting a region and using “=” alone will apply automatic indentation to the entire region.
Do you know if there is a way to set the length of the indention? I have my tab set to 4 characters, but the indent moves the line over 8 characters.
That’s probably controlled by the ‘shiftwidth’ environment setting. I think. I know I’ve got it set up to use a 3 space tab consistently at the office, but can’t check my .vimrc from there right now…
You’re right. Thanks.