55

For example, I'm editing this code:

<html>
<body>
<script>
    var a = 10;
    a += 100;
</script>
</body>
</html>

now I need to indent the script line:

<html>
<body>
    <script>
        var a = 10;
        a += 100;
    </script>
</body>
</html>

How could I do this without moving cursor to the begin of each line and press Tab?

86

Press V to switch to VISUAL LINE mode and highlight the lines you want to indent by pressing j. Then press > to indent them. So the complete command would be Vjjj>.

Alternatively, put your cursor on the <script> tag and use 4>> to indent four lines.

| improve this answer | |
  • 2
    Also, when changing multiple indent levels, . is extremely useful (it will move the same 'block' wether with visual, marks or a [count] like the above 4>>). – Pif Jan 17 '12 at 11:18
  • Also, if your syntax file can do tag matching, you can have your cursor on the word "script", and press v for visual mode, % to go to matching tag, and > to indent or = to auto indent based on syntax, so the total command would be v%> or v%= – ben Feb 1 '13 at 20:42
  • This works with character-wise visual mode too (lowercase v), which is a bit easier to type. So vjjj> – James Scriven Jan 3 '16 at 0:59
17

To supplement the above answer, take a look here. https://stackoverflow.com/questions/235839/how-do-i-indent-multiple-lines-quickly-in-vi

There are more than one way to do this, and I can't hope to compete with documentation already provided there.

My personal favorite is == to auto-indent. 5== to auto-indent 5 lines.

| improve this answer | |
12

To indent the all the lines below the current line

=G

So, to indent entire file, go to the beginning of the file (gg) and then indent all the lines below the current line (=G)

gg=G

To indent the current line

==

So, to indent n lines below the current line

n==

For example, to indent 4 lines below the current line

4==

These are the simplest commands to indent multiple lines.

| improve this answer | |
9

If it was me, I would notice that there are 4 lines to indent, position onto the top line and then type >4>. If there were too many lines to count, I would position onto the top line and type mk to make a mark named k, then position onto the bottom line (which might be several screenfuls away), and type >'k

| improve this answer | |
  • nice tip to save doing the math on lines, thanks! – flickerfly Sep 20 '19 at 21:21
4

Here is a nice approach, if you are trying to indent over a great number of lines:

:/<script>/,/<\/script>/ >>
| improve this answer | |
0

In normal mode

type >> to indent the current line, or << to unindent (shift). Each command can be used with a count. The operators > and < do the same for motions, text objects and visual selections. For all commands, pressing . repeats the operation.

For example, typing 5>> . . shifts the first five lines to the right (starting from the line of the cursor), and then repeats the operation twice so that the five lines are shifted three times.

Try to see! It's the best thing! But otherwise 5>> will activate the first indent and it happen as long as you hit the third stroke! And . will repeat! And it happen immediatly, u if you want to undo!

In insert mode

CtrlT indents the current line, and CtrlD unindents.

When indenting or unindenting, lines are shifted one 'shiftwidth' to the right or left.

 Basic examples

To adjust the indent on three lines:

Put the cursor anywhere in the first line. Press V then jj to visually select the three lines. Or just navigate with Up Down.

Press > to indent (shift text one 'shiftwidth' to the right), or press < to shift left. Press . to repeat the indent, or u to undo if you have shifted too far. Type gv if you want to reselect the lines (not needed).

Alternatively, if you know that you want to adjust three lines, you can simply:

Type 3>> to shift right or 3<< to shift left. Or:

Type >2j to shift right or <2j to shift left. As mentioned above, the > and < commands combine with arbitrary Vim movements and text objects. For example, >} to indent from the cursor to the next blank line, or <aB to un-indent the current C-like {...} "block" structure.

Check more

There is more you can check on this awesome tuto! It's from where i get the information! And added some lines!

https://vim.fandom.com/wiki/Shifting_blocks_visually

 For code blocks

https://vim.fandom.com/wiki/Indent_a_code_block

int myfunction(int a)
 {
 if ( a == 1 ) {
 printf("one");
return 1;        // the cursor is in this line
 }
 return 0;
   }

These commands will fix the indents:

=i{ reindents "inner block" (inside the braces). =a{ reindents "a block" (including the braces). =2a{ reindents 2 blocks (this block and containing block). Instead of {, you can use } or B, for example, =aB indents a block.

These commands will decrease or increase indents:

>i{ increase indent for inner block. . repeat last change (increase indent of block again). <i{ decrease indent for inner block. With the cursor on { or }:

=% indents the block (including matching brace). >% or <% indents or unindents the block.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.