2012-09-30 16:09:46 -04:00
vim-autoformat
2012-12-01 05:18:53 -05:00
==============
2013-01-21 07:34:25 -05:00
Format your code with only one button press!
2013-02-09 10:27:20 -05:00
This plugin makes use of external formatprograms to achieve the best result.
2013-03-08 15:14:17 -05:00
Check the list of formatprograms to see which languages are supported by default.
2013-03-08 15:34:54 -05:00
You can easily customize or add your own formatprogram.
2013-04-20 17:46:42 -04:00
When no formatprogram exists (or no formatprogram is installed) for a certain filetype, vim-autoformat uses vim's indent functionality as a fallback.
2012-12-01 13:03:23 -05:00
2013-03-17 07:36:49 -04:00
How to install
2012-12-01 13:03:23 -05:00
-----------------------
2013-03-17 07:30:50 -04:00
###Vundle
2012-12-01 13:03:23 -05:00
Put this in your .vimrc
```vim
2014-08-26 10:58:22 -04:00
Plugin 'Chiel92/vim-autoformat'
2012-12-01 13:03:23 -05:00
```
2013-03-17 07:30:50 -04:00
Then restart vim and run `:BundleInstall` .
2013-03-18 12:04:49 -04:00
To update the plugin to the latest version, you can run `:BundleUpdate` .
2013-03-17 07:30:50 -04:00
###Pathogen
Download the source and extract in your bundle directory.
Updating has to be done manually, as far as I'm aware.
###Other
It is highly recommended to use a plugin manager such as Vundle, since this makes it easy to update plugins or uninstall them.
It also keeps your .vim directory clean.
2013-03-27 05:17:24 -04:00
Still you can decide to download this repository as a zip file or whatever and extract it to your .vim folder.
2013-03-17 07:30:50 -04:00
2012-12-02 09:12:04 -05:00
How to use
----------
2013-03-27 05:17:24 -04:00
First you should install an external program that can format code of the programming language you are using.
2013-04-23 10:01:16 -04:00
This can either be one of the programs that are listed below as defaultprograms, or a custom program.
For using a custom formatprogram, read the text below *How can I change the behaviour of formatters, or add one myself?*
If the formatprogram you want to use is installed correctly, in one of the following ways, vim automatically detects it.
* It suffices to make the formatprogram globally available, which is the case if you install it via your package manager.
* Alternatively you can point vim to the the binary by explicitly putting the absolute path in `g:formatprg_<filetype>` in your .vimrc.
2013-07-19 07:38:38 -04:00
2013-04-20 17:46:42 -04:00
Remember that when no formatprogram exists for a certain filetype, vim-autoformat uses vim's indent functionality as a fallback.
This will fix at least the indentation of your code, according to vim's indentfile for that filetype.
2012-12-02 09:12:04 -05:00
2013-03-08 15:14:17 -05:00
When you have installed the formatters you need, you can format the entire buffer with the command `:Autoformat` .
2014-06-29 15:30:05 -04:00
You can provide the command with a file type such as `:Autoformat json` , otherwise the buffer's filetype will be used.
2012-12-02 09:12:04 -05:00
For convenience it is recommended that you assign a key for this, like so:
2012-12-02 15:19:14 -05:00
2012-12-02 09:12:04 -05:00
```vim
2013-04-23 10:01:16 -04:00
noremap < F3 > :Autoformat< CR > < CR >
2012-12-02 09:12:04 -05:00
```
2013-03-08 15:14:17 -05:00
If you don't want to format the entire buffer, you can alternatively format visually selected code with `gq` .
2013-03-09 11:04:11 -05:00
However, some formatprograms will behave a bit weird this way, because they need the context of a piece of code.
2012-12-02 15:36:06 -05:00
For more ways to perform autoformatting type `:help gq` .
2013-03-27 05:17:24 -04:00
If you format a piece of code manually by using `gq` , vim will not fallback to auto indentation if there is no formatprogram available.
This way, the default behaviour of `gq` will remain functional.
2012-12-02 09:12:04 -05:00
2013-03-08 15:14:17 -05:00
Default formatprograms
2012-12-01 15:34:44 -05:00
------------------------
2013-04-20 17:46:42 -04:00
Here is a list of formatprograms that are supported by default, and thus will be detected and used by vim when they are installed properly.
2012-12-06 17:58:01 -05:00
2013-02-09 10:28:20 -05:00
* `astyle` for __C#__ , __C++__ , __C__ and __Java__ .
2012-12-15 07:56:15 -05:00
It's probably in your distro's repository, so you can download it as a regular package.
2012-12-06 17:58:01 -05:00
For Ubuntu type `sudo apt-get install astyle` in a terminal.
2013-02-08 16:46:20 -05:00
Otherwise, download it here: http://astyle.sourceforge.net/.
2012-12-02 14:54:59 -05:00
2012-12-02 15:19:14 -05:00
* `autopep8` for __Python__ .
2012-12-15 07:56:15 -05:00
It's probably in your distro's repository, so you can download it as a regular package.
2014-06-29 11:29:40 -04:00
For Ubuntu type `sudo apt-get install python-autopep8` in a terminal.
2013-02-08 16:46:20 -05:00
Here is the link to the repository: https://github.com/hhatto/autopep8.
And here the link to its page on the python website: http://pypi.python.org/pypi/autopep8/0.5.2.
2012-12-06 17:50:38 -05:00
2014-06-29 15:15:10 -04:00
* `js-beautify` for __Javascript__ and __JSON__ .
2013-12-20 14:34:38 -05:00
It can be installed by running `npm install -g js-beautify` .
Note that `nodejs` is needed for this to work.
Here is the link to the repository: https://github.com/einars/js-beautify.
* `html-beautify` for __HTML__ .
It is shipped with `js-beautify` , which can be installed by running `npm install -g js-beautify` .
Note that `nodejs` is needed for this to work.
Here is the link to the repository: https://github.com/einars/js-beautify.
2014-05-30 16:42:30 -04:00
* `css-beautify` for __CSS__ .
It is shipped with `js-beautify` , which can be installed by running `npm install -g js-beautify` .
Note that `nodejs` is needed for this to work.
Here is the link to the repository: https://github.com/einars/js-beautify.
2014-09-26 14:00:08 -04:00
* `sass-convert` for __SCSS__ .
2014-09-26 14:02:01 -04:00
It is shipped with `sass` , a CSS preprocessor written in Ruby, which can be installed by running `gem install sass` .
2014-09-26 14:00:08 -04:00
Here is the link to the SASS homepage: http://sass-lang.com/.
2013-12-20 14:34:38 -05:00
* `tidy` for __XHTML__ and __XML__ .
2013-01-20 12:56:32 -05:00
It's probably in your distro's repository, so you can download it as a regular package.
For Ubuntu type `sudo apt-get install tidy` in a terminal.
2013-04-23 10:01:16 -04:00
How can I change the behaviour of formatters, or add one myself?
2012-12-02 15:15:45 -05:00
---------------------------------------------
2013-04-23 10:01:16 -04:00
If you need a formatter that is not among the defaults, or if you are not satisfied with the default formatting behaviour that is provided by vim-autoformat, you can define it yourself.
*The formatprogram must read the unformatted code from the standard input, and write the formatted code to the standard output.*
2013-04-20 17:46:42 -04:00
The formatprogram that is used for a certain `<filetype>` is defined in `g:formatprg_<filetype>` .
The arguments that are passed to the formatprogram are either defined in `g:formatprg_args_expr_<filetype>` as an expression which can be evaluated, or else in `g:formatprg_args_<filetype>` as a plain string.
2013-03-16 08:53:16 -04:00
Defining any of these variable manually in your .vimrc, will override the default value, if existing.
2013-03-27 05:17:24 -04:00
So, a complete definition in your .vimrc for C# files could look like this:
2013-03-08 15:14:17 -05:00
```vim
let g:formatprg_cs = "astyle"
2013-03-23 05:20:33 -04:00
let g:formatprg_args_cs = "--mode=cs --style=ansi -pcHs4"
2013-03-08 15:14:17 -05:00
```
2013-03-23 05:20:33 -04:00
2013-03-26 09:29:35 -04:00
If you want to define the arguments dynamically, you can use the more powerful `g:formatprg_args_expr_<filetype>` instead.
2013-03-23 05:22:12 -04:00
Then, a similar definition would look like this:
2013-03-23 05:20:33 -04:00
```vim
let g:formatprg_cs = "astyle"
2013-12-20 14:34:38 -05:00
let g:formatprg_args_expr_cs = '"--mode=cs --style=ansi -pcHs".& shiftwidth'
2013-03-23 05:20:33 -04:00
```
2013-04-20 17:46:42 -04:00
Notice that `g:formatprg_args_expr_cs` now contains an expression that can be evaluated, as required.
2013-03-16 08:53:16 -04:00
As you see, this allows us to dynamically define some parameters.
2013-03-27 05:17:24 -04:00
In this example, the indent width that astyle will use, depends on the buffer local value of `&shiftwidth` , instead of being fixed at 4.
So if you're editing a csharp file and change the `shiftwidth` (even runtime), the `formatprg_args_expr_<filetype>` will change correspondingly.
2013-03-17 07:30:50 -04:00
2013-03-26 18:03:15 -04:00
For the default formatprogram definitions, the options `expandtab` , `shiftwidth` and `textwidth` are taken into account whenever possible.
2013-03-17 07:30:50 -04:00
This means that the formatting style will match your current vim settings as much as possible.
2013-03-16 08:53:16 -04:00
For the exact default definitions, have a look in `vim-autoformat/plugin/defaults.vim` .
2013-03-08 15:14:17 -05:00
2012-12-03 15:37:30 -05:00
2013-12-20 14:34:38 -05:00
Things that are not (yet) implemented
----------------------------------------------------------
2013-01-20 12:56:32 -05:00
* Check for windows support.
2013-12-20 14:34:38 -05:00
* Option for on-the-fly code-formatting, like visual studio (If ever. When you have a clever idea about how to do this, I'd be glad to hear.)
2013-01-22 14:49:39 -05:00
2013-12-20 14:34:38 -05:00
Pull requests are welcome.
2013-03-27 05:17:24 -04:00
Any feedback is welcome.
2013-12-20 14:34:38 -05:00
If you have any suggestions on this plugin or on this readme, if you have some nice default formatprg definition that can be added to the defaults, or if you experience problems, please contact me by creating an issue in this repository.
2013-03-09 06:10:10 -05:00
Change log
----------
2014-05-30 16:42:30 -04:00
### May 30 2014
* Added `css-beautify` to the defaults for formatting CSS files
2013-12-20 14:34:38 -05:00
### December 20 2013
* `html-beautify` is now the default for HTML since it seems to be better maintained, and seems to handle inline javascript neatly.
* The `formatters/` folder is no longer supported anymore, because it is unnecessary.
* `js-beautify` can no longer be installed as a bundle, since it only makes this plugin unnecessarily complex.
2013-03-10 15:17:54 -04:00
2013-12-20 14:34:38 -05:00
### March 27 2013
* The default behaviour of gq is enabled again by removing the fallback on auto-indenting.
Instead, the fallback is only used when running the command `:Autoformat` .
* For HTML,XML and XHTML, the option `textwidth` is taken into account when formatting.
This extends the way the formatting style will match your current vim settings.
2013-03-16 08:57:11 -04:00
2013-03-16 07:21:55 -04:00
### March 16 2013
2013-03-16 08:57:11 -04:00
The `dynamic_indent_width` branch has been merged into the master branch.
2013-03-16 08:53:16 -04:00
* The options `expandtab` , `shiftwidth` , `tabstop` and `softtabstop` are not overwritten anymore.
* This obsoletes `g:autoformat_no_default_shiftwidth`
2013-03-16 08:57:11 -04:00
* `g:formatprg_args_expr_<filetype>` is introduced.
2013-03-27 05:17:24 -04:00
2013-12-20 14:34:38 -05:00
### March 13 2013
* It is now possible to prevent vim-autoformat from overwriting your settings for `tabstop` , `softtabstop` , `shiftwidth` and `expandtab` in your .vimrc.
### March 10 2013
* When no formatter is installed or defined, vim will now auto-indent the file instead. This uses the indentfile for that specific filetype.
### March 9 2013
The `custom_config` branch has been merged into the master branch.
* Customization of formatprograms can be done easily now, as explained above.
* I set the default tabwidth to 4 for all formatprograms as well as for vim itself.
* The default parameters for astyle have been slightly modified: it will wrap spaces around operators.
* phpCB has been removed from the defaults, due to code-breaking behaviour.
* XHTML default definition added