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

前端 未结 16 2163
不知归路
不知归路 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:43

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

    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.

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

    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.

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

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

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

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

    [[=]]
    
    0 讨论(0)
  • 2020-12-02 03:49

    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.

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