42

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?

69

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.

  • 1
    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
14

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.

10

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.

7

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

3

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

:/<script>/,/<\/script>/ >>

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.