21

If I want to indent all lines in a document, I can type >G to apply it to the current line and below, followed by and >gg to apply it to the lines above.

Is there a combination I can use to just apply > to all lines at once?

4 Answers 4

19

You are looking for the :> command and a range of 1,$ (% for short) for the whole file.

:%>

For more help see:

:h :>
:h :range
1
  • 5
    Because of the title of this question ("apply a command to all lines"), I feel it's worth noting that the example in the question mentions the Normal-mode operator >, while this answer uses the Command-mode command :>. These are not, strictly speaking, the same thing. That they have the same name should be considered a coincidence; don't assume you can interchange operators and commands in general. For example, the gU operator converts text to uppercase, but :%gU is not a valid command to apply the same to the entire buffer.
    – tommcdo
    Dec 9, 2015 at 0:43
7

One way to indent all lines in a document:

:%>
4

Without leaving normal mode you can simply do:

gg>G
9
  • 2
    the downside here is that you lose your position in the file Dec 8, 2015 at 20:56
  • All the other answers share that downside and the OP didn't mention "respect the cursor position" as a requirement.
    – romainl
    Dec 8, 2015 at 21:05
  • 1
    <c-o> can be used to go back to your previous position @romainl's solution requires you to go back twice while the other only require going back once
    – Brett Y
    Dec 8, 2015 at 21:24
  • Surely you can appreciate the value of keeping your place, and how an answer that would keep that position would be valuable. Dec 8, 2015 at 22:33
  • 1
    That's why it's not being downvoted. It's simply a note saying there's a better option somewhere and that they should keep looking. For example, on a programming SE site, sloppy code that solves the problem solves the problem, but you'd still expect to find another solution that solves the problem and looks good. Dec 8, 2015 at 23:04
4

Another option that has not been mentioned yet:

:g/^/>

This will run the command > on all lines that match the pattern ^.

Downside is that this will overwrite your search history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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