Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For eample, 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?

share|improve this question
    
In command mode: gg=G –  Prince John Wesley Jul 19 '11 at 8:22

5 Answers 5

up vote 13 down vote accepted

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.

share|improve this answer
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

To supplement the above answer, take a look here. http://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.

share|improve this answer

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

share|improve this answer

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

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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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