2012-09-30 13:09:46 -07:00
|
|
|
vim-autoformat
|
2012-12-01 11:18:53 +01:00
|
|
|
==============
|
2013-01-21 13:34:25 +01:00
|
|
|
Format your code with only one button press!
|
2013-02-09 16:27:20 +01:00
|
|
|
This plugin makes use of external formatprograms to achieve the best result.
|
2013-03-08 21:14:17 +01:00
|
|
|
You can specify how to use which formatprograms for which filetypes.
|
|
|
|
A set of reasonable defaults are given, so you should be able to use it right away.
|
|
|
|
Check the list of formatprograms to see which languages are supported by default.
|
2012-12-01 19:03:23 +01:00
|
|
|
|
|
|
|
How to install (Vundle)
|
|
|
|
-----------------------
|
|
|
|
Put this in your .vimrc
|
|
|
|
|
|
|
|
```vim
|
|
|
|
Bundle "Chiel92/vim-autoformat"
|
|
|
|
```
|
|
|
|
|
2012-12-02 15:12:04 +01:00
|
|
|
How to use
|
|
|
|
----------
|
2012-12-06 23:58:01 +01:00
|
|
|
First you have to install an external program that can format code of the programming language you are using.
|
2013-03-08 21:14:17 +01:00
|
|
|
For the default supported formatprograms, it suffices to make the formatprogram either globally available
|
|
|
|
(which is the case if you install it via your package manager)
|
2012-12-11 18:34:49 +01:00
|
|
|
or to put it in the `formatters/` folder.
|
2012-12-02 21:28:27 +01:00
|
|
|
Sometimes alternative installation methods are supported.
|
2012-12-02 15:12:04 +01:00
|
|
|
|
2013-03-08 21:14:17 +01:00
|
|
|
When you have installed the formatters you need, you can format the entire buffer with the command `:Autoformat`.
|
2012-12-02 15:12:04 +01:00
|
|
|
For convenience it is recommended that you assign a key for this, like so:
|
2012-12-02 21:19:14 +01:00
|
|
|
|
2012-12-02 15:12:04 +01:00
|
|
|
```vim
|
2012-12-03 09:03:07 +01:00
|
|
|
noremap <F7> :Autoformat<CR><CR>
|
2012-12-02 15:12:04 +01:00
|
|
|
```
|
|
|
|
|
2013-03-08 21:14:17 +01:00
|
|
|
If you don't want to format the entire buffer, you can alternatively format visually selected code with `gq`.
|
|
|
|
However, some formatprograms will behave a bit weird then, because they need the context of a piece of code.
|
2012-12-02 21:36:06 +01:00
|
|
|
For more ways to perform autoformatting type `:help gq`.
|
2012-12-02 15:12:04 +01:00
|
|
|
|
2013-03-08 21:14:17 +01:00
|
|
|
Default formatprograms
|
2012-12-01 21:34:44 +01:00
|
|
|
------------------------
|
2013-03-08 21:14:17 +01:00
|
|
|
For most linux distro's, if you installed a formatprogram, it's automatically globally available.
|
2012-12-15 13:56:15 +01:00
|
|
|
If this is not the case, you can either make it globally available manually, or put it's binary in the `formatters/` directory.
|
|
|
|
Sometimes alternative installation methods are presented.
|
2012-12-02 19:55:32 +01:00
|
|
|
Here is a list of formatprograms that are currently supported.
|
2012-12-06 23:58:01 +01:00
|
|
|
|
2013-02-09 16:28:20 +01:00
|
|
|
* `astyle` for __C#__, __C++__, __C__ and __Java__.
|
2012-12-15 13:56:15 +01:00
|
|
|
It's probably in your distro's repository, so you can download it as a regular package.
|
2012-12-06 23:58:01 +01:00
|
|
|
For Ubuntu type `sudo apt-get install astyle` in a terminal.
|
2013-02-08 22:46:20 +01:00
|
|
|
Otherwise, download it here: http://astyle.sourceforge.net/.
|
2012-12-02 20:54:59 +01:00
|
|
|
|
2012-12-15 13:56:15 +01:00
|
|
|
* `jsbeautify` (the python CLI version) for __Javascript__.
|
|
|
|
This one can also be installed as a vundle package, and I recommend to do so.
|
|
|
|
Put this in your .vimrc: `Bundle "einars/jsbeautify"`.
|
2012-12-06 23:58:01 +01:00
|
|
|
Note that we're only using the python version, so `node` doesn't have to be installed.
|
2013-02-08 22:46:20 +01:00
|
|
|
Here is the link to the repository: https://github.com/einars/js-beautify.
|
2012-12-06 23:50:38 +01:00
|
|
|
|
2012-12-02 21:19:14 +01:00
|
|
|
* `autopep8` for __Python__.
|
2012-12-15 13:56:15 +01:00
|
|
|
It's probably in your distro's repository, so you can download it as a regular package.
|
2012-12-06 23:58:01 +01:00
|
|
|
For Ubuntu type `sudo apt-get install autopep8` in a terminal.
|
2013-02-08 22:46:20 +01: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 23:50:38 +01:00
|
|
|
|
|
|
|
* `phpCB` for __PHP__.
|
2013-02-08 22:46:20 +01:00
|
|
|
You can download it's binary here:
|
|
|
|
http://www.waterproof.fr/products/phpCodeBeautifier/download.php.
|
2012-12-06 23:50:38 +01:00
|
|
|
I recommend putting the phpCB binary in the `formatters/` directory.
|
2012-12-01 21:34:44 +01:00
|
|
|
|
2013-02-09 16:28:20 +01:00
|
|
|
* `tidy` for __HTML__ and __XML__.
|
2013-01-20 18:56:32 +01: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.
|
|
|
|
|
2012-12-02 21:15:45 +01:00
|
|
|
How can I change the behaviour of formatters?
|
|
|
|
---------------------------------------------
|
2013-03-08 21:14:17 +01:00
|
|
|
The formatprg for a filetype is defined in the global `g:formatprg_<filetype>`.
|
|
|
|
The arguments given to the formatprogram are defined in `g:formatprg_args_<filetype>`.
|
|
|
|
So for example, if you want to set the arguments passed to `astyle` for formatting a C# file, you would put a line like this in your .vimrc:
|
|
|
|
|
|
|
|
```vim
|
|
|
|
let g:formatprg_args_cs = "--mode=cs --style=java -H"
|
|
|
|
```
|
|
|
|
|
|
|
|
How can I define a formatprogram myself?
|
|
|
|
---------------------------------
|
|
|
|
A definition looks like this:
|
|
|
|
|
|
|
|
```vim
|
|
|
|
let g:formatprg_cs = "astyle"
|
|
|
|
let g:formatprg_args_cs = "--mode=cs --style=ansi -p -c -H"
|
|
|
|
```
|
|
|
|
|
|
|
|
If you change the tabwidth for a formatprogram, I would suggest to change the indent options of vim correspondingly for that filetype:
|
|
|
|
|
|
|
|
```vim
|
|
|
|
au filetype *.cs set tabstop=4
|
|
|
|
au filetype *.cs set softtabstop=4
|
|
|
|
au filetype *.cs set shiftwidth=4
|
|
|
|
```
|
|
|
|
|
2012-12-03 21:37:30 +01:00
|
|
|
|
2013-01-20 18:56:32 +01:00
|
|
|
Todo list
|
|
|
|
---------
|
|
|
|
* Check for windows support.
|
2013-02-23 21:19:18 +01:00
|
|
|
* Option for on-the-fly code-formatting (like visual studio)
|
2013-01-22 20:49:39 +01:00
|
|
|
|
|
|
|
|
2013-02-08 22:46:20 +01:00
|
|
|
If you have any suggestions on this plugin or on this readme or if you experience problems, please contact me by creating an issue in this repository.
|