How do I fix the indentation of an entire file in Vi?

前端 未结 16 2164
不知归路
不知归路 2020-12-02 03:24

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

相关标签:
16条回答
  • 2020-12-02 03:50

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

    0 讨论(0)
  • 2020-12-02 03:51

    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 讨论(0)
  • 2020-12-02 03:56

    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.

    0 讨论(0)
  • 2020-12-02 03:59

    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.

    0 讨论(0)
  • 2020-12-02 04:00

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

    0 讨论(0)
  • 2020-12-02 04:01

    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.

    0 讨论(0)
提交回复
热议问题