703

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.

Conner
  • 30,144
  • 8
  • 52
  • 73
mmcdole
  • 91,488
  • 60
  • 186
  • 222
  • Similar: [Re-indenting badly indented code](http://vi.stackexchange.com/q/236/467) at Vi SE – kenorb Sep 11 '15 at 18:06
  • 11
    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. – Chthonic Project Oct 14 '16 at 04:07
  • 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 Aug 28 '23 at 09:42

16 Answers16

1352

=, 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.

ib.
  • 27,830
  • 11
  • 80
  • 100
Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
  • 50
    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 Feb 03 '09 at 08:05
  • 2
    :0 is not so bad but gg is nice. (yeah, I learned ed first) – Erik Olson Dec 21 '09 at 22:14
  • 9
    Can I indent the entire file without leaving the current line? – Fábio Perez Aug 16 '11 at 23:27
  • 103
    @Fábio: `''` (two single quotes) takes you back to where you were so `gg=G''` should indent then return. – Nemo157 Aug 31 '11 at 23:45
  • @ArchimedesTrajano Mac OS's stock vim is very old, I recommend HomeBrew's version; it's much newer. I had some annoying issues with Mac's vim. Also, I heard that you should not mess with the built-in one, so don't try to update it yourself unless you're very careful. – AlexMA Apr 14 '14 at 20:22
  • Why gg=G works so long? In Sublime with plugin I can indent 5000 xml lines very quick, but in Vim it takes much more, not seconds, minutes... – Sonique May 17 '15 at 17:20
  • GG WP report 9x pls – Molten Ice Aug 26 '16 at 09:52
  • 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. – Patrick Forget Feb 16 '17 at 18:13
  • I'd go for `gg=G` to go back to the cursor position. I have this mapped to g like so : `nnoremap g gg=G` – Biggybi Aug 03 '19 at 15:02
  • This doesn't seem to work for `.razor` files, I mean it does change the indentation, but it is incorrect (all over the place). – J86 May 01 '23 at 08:22
137

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
derobert
  • 49,731
  • 15
  • 94
  • 124
  • 5
    You can set the `equalprg` option in a ftplugin to use an external filter for `=` indenting, rather than a custom keybinding. – Josh Lee Nov 13 '09 at 01:03
  • Theres also a pastetoggle keybinding option eg. :set pt \p to flip between modes – michael Dec 21 '09 at 22:33
  • Note: in grml's vimconfig the pastetoggle key is mapped to F11 – Thomas Nov 06 '12 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` – Raffi Khatchadourian Feb 27 '13 at 05:06
  • just downloaded perltidy after reading this, it's so much better than the default vim auto indent – Joey Ciechanowicz Mar 05 '14 at 19:37
53

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

=%
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
32

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

[[=]]
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
  • 1
    Sorry to revive this, but what did you mean by chords? Coords? – John P Jan 07 '16 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. – Ralph Giles May 13 '16 at 18:23
  • You can also use `=aB` which will not move cursor at all. – amin Jul 31 '16 at 09:59
  • 10
    On the Nordic keyboard, `[[=]]` requires 5 chords :( – k4kuz0 Oct 20 '16 at 17:30
  • @RalphGiles Probably applies to US keyboard layouts only. On German keyboards `[` is a chord, just as `=` is. – U. Windl Aug 28 '23 at 09:46
18

press escape and then type below combinations fast:

gg=G
tokhi
  • 21,044
  • 23
  • 95
  • 105
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 %
ib.
  • 27,830
  • 11
  • 80
  • 100
Naga Kiran
  • 8,585
  • 5
  • 43
  • 53
12

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.

ib.
  • 27,830
  • 11
  • 80
  • 100
Amjith
  • 22,626
  • 14
  • 43
  • 38
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.

0x89
  • 2,940
  • 2
  • 31
  • 30
7

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

Eric Johnson
  • 17,502
  • 10
  • 52
  • 59
7

: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.

ib.
  • 27,830
  • 11
  • 80
  • 100
Elsporko
  • 501
  • 4
  • 14
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.

Community
  • 1
  • 1
Pierz
  • 7,064
  • 52
  • 59
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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Wojtek Kruszewski
  • 13,940
  • 6
  • 38
  • 38
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 )

alphayax
  • 2,930
  • 2
  • 25
  • 25
2

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
  • 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. – Martin Braun Sep 30 '22 at 21:20
2

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.

Ramanand Yadav
  • 309
  • 3
  • 3
  • 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 Sep 07 '19 at 08:13
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"); 
}
Alex
  • 8,461
  • 6
  • 37
  • 49
ps2090
  • 223
  • 2
  • 15