Automatically indented code with broken indentation in vim

I work with CMS where code indentation is a mess. Is it possible to fix it automatically with vim by running some command?

+4
source share
2 answers

Generally, you can use the = operation to indent.

See :help =

(You also need to have filetype indent on in your vimrc to include different indent rules for each type of file you are editing)

gg=G will contain whole files. (gg moves to the beginning of the file, = will rewind each line under the movement, G goes to the end of the file)

By default, Vim supports C and C well as a language. See :help C-indenting for parameters. You may need to tweak these settings before reformatting your files. You will need to define set cindent in your .vimrc if you want to use the smart indents controlled by cinoptions .

See :help indent-expression for other languages.

In addition, you may have to use the shiftwidth , expandtab and tabstop if you want to use spaces or tab indentation.

For example, if you replace all tabs with 4 spaces, you would have to use:

 set shiftwidth=4 " used by >>, << and tab. set tabstop=4 " number of space characters used when displaying TAB set expandtab " replace TAB by spaces 
+9
source

I usually do this with ggVG= . gg = go to the beginning of the file, V = mark lines, G = go to the end of the file, = = indent.

This may not be the smallest keystroke, but I think it's easy to remember.

+1
source

Source: https://habr.com/ru/post/1334334/


All Articles