views:

1265

answers:

2

I'm currently using Vim as a lightweight IDE. I have NERDTree, bufexplorer, supertab, and ctags plugins which do almost everything I want. Only big thing missing for me is auto code formatting.

I'm working with some messy PHP code which has inconsistent indenting and code formatting, ideally I could highlight the code I want formatted (whole files would be fine too) and run a command to tidy it.

Does anybody have a nice solution for this working in Vim?

+6  A: 

Quick way to fix PHP indentation in vim is to visually select the lines you want to work with using shift-v, and then press equals (=) to trigger auto-formatting.

As for other formatting issues you're probably looking at employing some regex search and replaces, such as ":%s/^M/\r/g" (that's ctrl-V ctrl-m, not caret-M) to fix line-endings

Wesley Mason
I can't believe I didn't know about equals auto-formatting years ago, that's really useful. For the formatting issues I *could* build some custom regex's, I guess I was hoping someone had already done (and tested) it.
gacrux
Protip: ggvG= will select every line in the file and autoformat
Whaledawg
Whaledawg. you can even just gg=G and save a character. Its worth remembering you can action-motion most commands in vim
michael
A: 

The vim website is not the easiest to navigate, but there is a wealth of chewy nugget center there.

For instance I found this php indenting script there. Give it a try.

Whaledawg