35

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?

gacrux
  • 930
  • 1
  • 9
  • 16
  • 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. –  Oct 02 '11 at 05:21

5 Answers5

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

Wes Mason
  • 1,611
  • 12
  • 13
  • 2
    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 May 13 '09 at 13:39
  • 12
    Protip: ggvG= will select every line in the file and autoformat – Whaledawg May 13 '09 at 14:06
  • 19
    Whaledawg. you can even just gg=G and save a character. Its worth remembering you can action-motion most commands in vim – michael May 13 '09 at 23:13
  • Love the shift-v + = shortcut. This is going to save me some headaches. Thanks a lot. How do I give more votes to you :) – recluze Apr 29 '11 at 11:49
  • WOW! simple & powerful! – zx1986 Jul 09 '15 at 07:17
  • 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. – Pascal Luxain Nov 20 '15 at 07:47
  • Even better, I just discovered `=ip` indents a paragraph directly (`=` -> indent, `i` -> inside, `p` -> paragraph) – Pascal Luxain Nov 20 '15 at 08:41
  • Does anyone know of a way to set whether squiggly braces are on same line as if or below if? – dougd_in_nc Mar 17 '23 at 17:04
16

Enter normal mode in vim and then type

1GVG=
Community
  • 1
  • 1
wormhit
  • 3,687
  • 37
  • 46
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 git@github.com:stephpy/vim-php-cs-fixer.git

Restart Vim.

Default mapping:

<leader>pcd " For directory
<leader>pcf " For flie
Community
  • 1
  • 1
Janghou
  • 1,613
  • 1
  • 21
  • 30
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.

chtenb
  • 14,924
  • 14
  • 78
  • 116
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.

Whaledawg
  • 4,234
  • 4
  • 26
  • 21