715

In Vim, what is the command to correct the indentation of all the lines?

Often times I'll copy and paste code into a remote terminal and have the whole thing messed up. I want to fix this in one fell swoop.

4
  • Similar: Re-indenting badly indented code at Vi SE
    – kenorb
    Commented Sep 11, 2015 at 18:06
  • 13
    Prevention is better than cure. In that spirit comes this comment. Do a ":set paste" before entering insert mode and pasting code from remote terminal. Commented Oct 14, 2016 at 4:07
  • 1
    In classic vi one would :se noai (disable auto-indent) before pasting. Also when pasting to a text terminal all TABs are most likely expanded to spaces. Personally I use Emacs for this type of tasks: When you copy withing Emacs, TABs are preserved, and (in current version) a mis-indented paste can be fixed by simply pressing TAB.
    – U. Windl
    Commented Aug 28, 2023 at 9:42
  • If you want to format using vim in one script, superuser.com/a/1243288/1658455 is helpful where argdo setf htmldjango is unnecessary for me in vim 9.1.
    – An5Drama
    Commented Jun 9 at 7:12

16 Answers 16

1381

=, the indent command can take motions. So, gg to get the start of the file, = to indent, G to the end of the file, gg=G.

10
  • 52
    I'll never be able to unlearn my precious 1G =) One of my favorites is =% standing on an opening bracket. It fixes the indents of the whole block.
    – PEZ
    Commented Feb 3, 2009 at 8:05
  • 2
    :0<return> is not so bad but gg is nice. (yeah, I learned ed first)
    – Erik Olson
    Commented Dec 21, 2009 at 22:14
  • 9
    Can I indent the entire file without leaving the current line? Commented Aug 16, 2011 at 23:27
  • 108
    @Fábio: '' (two single quotes) takes you back to where you were so gg=G'' should indent then return.
    – Nemo157
    Commented Aug 31, 2011 at 23:45
  • 1
    In order to paste "as is" I often use :r !cat which reads content pasted into the cat command. Once done pasting content press ctrl+d ctrl+d to send to your vim at your current cursor position. Commented Feb 16, 2017 at 18:13
140

Before pasting into the terminal, try :set paste and then :set nopaste after you're done. This will turn off the auto-indent, line-wrap and other features that are messing up your paste.

edit: Also, I should point out that a much better result than = indenting can usually be obtained by using an external program. For example, I run :%!perltidy all the time. astyle, cindent, etc. can also be used. And, of course, you can map those to a key stroke, and map different ones to the same keystroke depending on file type.

5
  • 5
    You can set the equalprg option in a ftplugin to use an external filter for = indenting, rather than a custom keybinding.
    – Josh Lee
    Commented Nov 13, 2009 at 1:03
  • Theres also a pastetoggle keybinding option eg. :set pt \p to flip between modes
    – michael
    Commented Dec 21, 2009 at 22:33
  • Note: in grml's vimconfig the pastetoggle key is mapped to F11
    – Thomas
    Commented Nov 6, 2012 at 17:47
  • 1
    I use formatpgm with tidy and astyle and then gq. Here are some examples from my .vimrc: au FileType xml set fp=tidy\ -q\ -i\ -xml and au FileType java set fp=/usr/local/bin/astyle\ --mode=java\ --indent=tab
    – khatchad
    Commented Feb 27, 2013 at 5:06
  • just downloaded perltidy after reading this, it's so much better than the default vim auto indent Commented Mar 5, 2014 at 19:37
57

The master of all commands is

gg=G

This indents the entire file!

And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.

To indent the all the lines below the current line

=G

To indent the current line

==

To indent n lines below the current line

n==

For example, to indent 4 lines below the current line

4==

To indent a block of code, go to one of the braces and use command

=%
1
  • 1
    Thanks! Using =G enables you to repeat the command throughout tabs with the . (dot) command.
    – knowname
    Commented Mar 24, 2021 at 14:19
32

If you want to reindent the block you're in without having to type any chords, you can do:

[[=]]
5
  • 1
    Sorry to revive this, but what did you mean by chords? Coords?
    – John P
    Commented Jan 7, 2016 at 16:31
  • 7
    "Chords" here refers to commands issued by holding down one key while pressing another, in analogy to musical chords were several notes sound at once. So G is shift+g, ^] is ctrl+], and so on. These take longer to type than single-key bindings. Commented May 13, 2016 at 18:23
  • You can also use =aB which will not move cursor at all.
    – amin
    Commented Jul 31, 2016 at 9:59
  • 10
    On the Nordic keyboard, [[=]] requires 5 chords :(
    – k4kuz0
    Commented Oct 20, 2016 at 17:30
  • @RalphGiles Probably applies to US keyboard layouts only. On German keyboards [ is a chord, just as = is.
    – U. Windl
    Commented Aug 28, 2023 at 9:46
19

press escape and then type below combinations fast:

gg=G
2
  • 37
    I typed slowly, you won't believe what happened next.
    – k0pernikus
    Commented Nov 27, 2015 at 16:17
  • I just gave the answer and the comment an upvote because that was priceless! lol @k0pernikus
    – gglasses
    Commented May 26, 2016 at 23:34
17

You can use tidy application/utility to indent HTML & XML files and it works pretty well in indenting those files.

Prettify an XML file

:!tidy -mi -xml %

Prettify an HTML file

:!tidy -mi -html %
11

1G=G. That should indent all the lines in the file. 1G takes you the first line, = will start the auto-indent and the final G will take you the last line in the file.

2
  • Or even 1gg=G
    – Biggybi
    Commented Jul 28, 2019 at 11:53
  • 1G=G fixed it for me. Awesome!
    – Natus Drew
    Commented May 25, 2020 at 22:50
8

if you do not want to use :set paste, middle-click, set nopaste, you can also paste the content of the clipboard:

"*p
"+p

That way you don't have to leave normal mode. if you have to paste + or * depends on how you selected the text, see :help quoteplus.

8

:set paste is your friend I use putty and end up copying code between windows. Before I was turned on to :set paste (and :set nopaste) copy/paste gave me fits for that very reason.

1
  • 1
    Yes, I'm also using putty. :set paste is awesome
    – mmcdole
    Commented Feb 3, 2009 at 22:03
7

In Vim, use :insert. This will keep all your formatting and not do autoindenting. For more information help :insert.

3

For complex C++ files vim does not always get the formatting right when using vim's = filter command. So for a such situations it is better to use an external C++ formatter like astyle (or uncrustify) e.g.:

:%!astyle

Vim's '=' function uses its internal formatter by default (which doesn't always gets things right) but one can also set it use an external formatter, like astyle, by setting it up appropriately as discussed in this question.

3

You can create a mapping to do this for you.

This one will auto indent the whole file and still keep your cursor in the position you are:

nmap <leader>ai mzgg=G`z
1
  • I was looking for this. mz will mark the current location as z and `z will jump back to the z mark. So it's important to know that the z mark will be overwritten. Commented Sep 30, 2022 at 21:20
3

Just go to visual mode in vim , and select from up to down lines after selecting just press = , All the selected line will be indented.

1
  • 1
    I'd avoid this. Visual mode is for when you're unsure about your motions, if you know how to capture a set of characters then there's no point to the extra step of dropping into visual mode. Why ggvG= when you can gg=G. What was the point in making the motion visible? That goes for all actions which take motions.
    – alextes
    Commented Sep 7, 2019 at 8:13
3

vim-autoformat formats your source files using external programs specific for your language, e.g. the "rbeautify" gem for Ruby files, "js-beautify" npm package for JavaScript.

2

For XML files, I use this command

:1,$!xmllint --format --recover - 2>/dev/null

You need to have xmllint installed (package libxml2-utils)

(Source : http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html )

0

For vi Editor, use :insert. This will keep all your formatting and not insert auto-indenting.Once done press escape to view the actual formatted file otherwise you'l see some garbage characters. like ^I e.g:

public static void main(String[] args) {
^I
^I System.out.println("Some Garbage printed upon using :insert"); 
}

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