Formatting PHP Code within Vim
Asked Answered
A

5

36

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?

Ashtray answered 13/5, 2009 at 13:15 Comment(1)
I like Wesley Mason's idea using "shift-v" followed by "=". I record the key sequence of "shift-v" and "=" into a macro. Then I call the macro as many times as I want to format multiple lines of code.Doublereed
E
78

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

Eloisaeloise answered 13/5, 2009 at 13:20 Comment(8)
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.Ashtray
Protip: ggvG= will select every line in the file and autoformatPip
Whaledawg. you can even just gg=G and save a character. Its worth remembering you can action-motion most commands in vimKirkcudbright
Love the shift-v + = shortcut. This is going to save me some headaches. Thanks a lot. How do I give more votes to you :)Lederer
WOW! simple & powerful!Quesnay
If you want to format a whole paragraph at once, let's say a function or method, press vip=. vip will select the paragraph in visual line mode (from anywhere in the function) and = will set the indentation properly. Note that = will set the indentation, not auto format the selected text as adding spaces before or after parenthesis, brackets, etc.Errant
Even better, I just discovered =ip indents a paragraph directly (= -> indent, i -> inside, p -> paragraph)Errant
Does anyone know of a way to set whether squiggly braces are on same line as if or below if?Rogation
T
17

Enter normal mode in vim and then type

1GVG=
Tsang answered 14/11, 2011 at 12:1 Comment(5)
1G - goes to first line; V - switches to visual mode; G - goes to end of file; = - auto format selected code;Tsang
gg=G is slighlty easier (less capslock)Ozone
Actually this command triggers the functionality in the indent script. It only reindents the code, no complete reformatting.Kaput
I would use ggvG= instead also for the record they are the same thingZebrawood
gg: Cursor to line N, default first lineMarchesa
D
10

Format in PSR-2 style

For the new standard Coding Style Guide PSR-2 use the PHP-CS-Fixer.

There is a Vim plugin: Vim-php-cs-fixer

How to install:

Install PHP-CS-Fixer (globally with Composer):

composer global require friendsofphp/php-cs-fixer

Then add the Vim plugin (Pathogen):

cd ~/.vim/bundle
git clone [email protected]:stephpy/vim-php-cs-fixer.git

Restart Vim.

Default mapping:

<leader>pcd " For directory
<leader>pcf " For flie
Dierolf answered 9/7, 2015 at 10:46 Comment(3)
To run php-cs-fixer automatically after the file is saved, add the following line in ~/.vimrc: autocmd BufWritePost *.php call PhpCsFixerFixFile()Treatise
I get the error E117: Unknown function: php_cs_fixer#fixBowman
@Mahdimehrabi Same here, any luck?Jacobean
K
8

There is a vim plugin that enables formatting on your code from within vim. It's called vim-autoformat and you can read about it and download it here:

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

It integrates external code-formatting programs into vim. When this plugin is installed, you only have to install an external code formatter to get everything to work out of the box. It supports the php formatter phpCB, which is the best php formatter i've seen so far.

UPDATE: phpCB is not supported anymore, due to code breaking behaviour. However, vim's indentfile is always used as fallback, allowing you to at least indent your code when there's is no formatter available.

Kaput answered 6/12, 2012 at 23:2 Comment(0)
P
2

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

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

Pip answered 13/5, 2009 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.