32

I'm trying to use Vim more and more when I can. One of my biggest grip between Vim and an IDE like Aptana is the ability to auto indent.

Is there a means of auto formatting code (HTML, CSS, PHP) so it is properly indented?

If so how do you install this into vim? I don't understand plugins very much. I tried reviewing this thread and it confused me more: How to change vim auto-indent behavior?

  • Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin. – Edd Steel Sep 1 '11 at 22:37
  • ideally Html/css and php – chrisjlee Sep 1 '11 at 23:50
51

To indent the whole file automatically:

gg
=G

Explained:

  • gg - go to beginning of the file
  • G - go to end of the file
  • = - indent
  • 2
    Could you break it down? What is g typically by itself? and =G? – chrisjlee Sep 2 '11 at 5:47
  • @Chris see the edit – takeshin Sep 2 '11 at 5:53
  • 1
    Nice hint, I never knew this. However, it does a rubbish job with bash. – Sparhawk Aug 8 '14 at 4:49
  • How to execute gg=G from shell without opening the file? – W.M. Aug 28 '16 at 16:18
  • @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal – Kasun Siyambalapitiya Dec 1 '16 at 11:22
13

I don't know about auto-formatting existing code, but if you are writing code and need auto indent:

  • :set autoindent (or :set ai) will turn on auto-indent
  • Ctrl-d to un-indent (AKA outdent) your code
  • Tab or spaces to indent -- I personally use tab
  • :set tabwidth=4 (or :set tw=4) will control how many spaces a tab should indent code
  • The >> command will indent the current line. If you prefix it by a number, say 4>> then it will indent 4 lines, starting with the current line.
  • Likewise the << command will un-indent a line

I hope this gives you a good start.

  • 7
    To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line. – Edd Steel Sep 1 '11 at 22:36
  • 1
    also make sure indent plugins are being loaded (e.g. with :filetype indent on) – jw013 Sep 2 '11 at 0:50
  • 2
    set tw=4 sets the TEXTWIDTH to 4 – Steffen Roller Apr 25 '17 at 2:01
4

Auto Indent *.sh

Just add the following lines in ~/.vimrc

filetype indent on
set smartindent
autocmd BufRead,BufWritePre *.sh normal gg=G
  • Does this apply only to editing with vim or all editor tools? – W.M. Aug 28 '16 at 16:18
  • 1
    ~./vimrc only applicable for vim editor. – Rahul Patil Aug 29 '16 at 3:54
2

This plugin makes it easier to perform formatting on your code. It integrates external formatters, and has a fallback on vim's indent functionality.

https://github.com/Chiel92/vim-autoformat

Also, notice the difference between formatting and indenting. Indenting only corrects the whitespace before every line, while formatting also deals with any other thing, such as whitespace around operators etc.

1

Create/edit the ~/.vimrc file and add the following line:

set autoindent
  • 2
    This adds nothing to the existing answers... – jasonwryan Jan 30 '17 at 6:11
  • @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently. – Akshat Maheshwari Jan 30 '17 at 7:12
  • your piece of comment is already mentioned by Rahul's answer, so think before posting – Indrajeet Gour May 8 '17 at 9:25
0

In bash I do this:

source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3

this eliminates comments and reindents the script "bash way". it will not work if the script contains HEREDOCS but if you do this:

source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
declare -f Zibri|head --lines=-1|tail --lines=+3

it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D

  • I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point) – eacousineau Mar 2 '17 at 18:29
0

vim's autoformat/indent works pretty well. First, put this line in your ~/.vimrc:

filetype plugin indent on

Then open a file in vim and type gg=G

(gg moves cursor to the first line. = runs the indent command. G tells indent command to run from here to the last line.)

If the autoformat looks really bad, like every line is just left indented, then run :scriptnames and check if .../indent/html.vim (or whatever language you're using) is in the list. If not, then make sure your ~/.vimrc is correct. Or if you ran :filetype plugin indent on from the vim command line, you will need to re-open the file :e

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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