Tuesday, September 20, 2016

Three Minutes Daily Vim Tip: Indentation

Say you want to indent a block of code inside curly brackets. There are a couple of ways to do this. Before we run the commands, make sure to place your cursor inside the block of curly brackets.

>i{
>i}

The two commands above will indent the code inside the curly brackets. Specifically > indicates indentation, i indicates inside, and { or } indicates curly brackets.

You could of course make small manipulations, for example use < for de-indentation, and use any one of (){}[]<> after i for corresponding types of brackets/parenthesis.

Another way is to use vim's re-indentation feature, which will automatically adjust indentation by its own. Again, you will need to make sure to place the cursor within the block to run the following:

=i{

The first character = indicates re-indentation, and the rest should be identical to the previous context.

Finally, you may also re-indent the entire file by

gg=G

where gg places your cursor to the beginning of the file, and G indicates all the lines till the end of the file.

No comments:

Post a Comment