vim-autoformat/README.markdown

118 lines
5.3 KiB
Markdown
Raw Normal View History

2012-09-30 16:09:46 -04:00
vim-autoformat
==============
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.
2012-12-01 13:03:23 -05:00
How to install (Vundle)
-----------------------
Put this in your .vimrc
```vim
Bundle "Chiel92/vim-autoformat"
```
2012-12-02 09:12:04 -05:00
How to use
----------
2012-12-06 17:58:01 -05:00
First you have to install an external program that can format code of the programming language you are using.
2013-03-09 11:04:11 -05:00
It suffices to make the formatprogram globally available, which is the case if you install it via your package manager.
2013-03-09 14:54:51 -05:00
Alternatively, you can put its binary (or a link to it) in the `formatters/` folder.
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`.
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
noremap <F7> :Autoformat<CR><CR>
2012-12-02 09:12:04 -05:00
```
If there is no formatprogram available for the current filetype, vim will auto-indent the buffer, instead of auto-formatting it.
This will fix at least the indentation of your code, according to vim's indentfile for that filetype.
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`.
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-03-08 15:34:54 -05:00
Here is a list of formatprograms that are supported by default.
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-15 07:56:15 -05: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 17:58:01 -05:00
Note that we're only using the python version, so `node` doesn't have to be installed.
2013-02-08 16:46:20 -05:00
Here is the link to the repository: https://github.com/einars/js-beautify.
2012-12-06 17:50:38 -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.
2012-12-06 17:58:01 -05:00
For Ubuntu type `sudo apt-get install 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
2013-03-09 13:24:05 -05:00
* `tidy` for __HTML__, __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.
2012-12-02 15:15:45 -05:00
How can I change the behaviour of formatters?
---------------------------------------------
2013-03-09 14:54:51 -05:00
If you are not satisfied with the default configuration for a certain filetype, you can override it by defining it yourself.
The formatprogram used for a `<filetype>` is defined in `g:formatprg_<filetype>`.
2013-03-09 11:00:35 -05:00
The arguments passed to the formatprogram are defined in `g:formatprg_args_<filetype>`.
2013-03-09 14:54:51 -05:00
The formatprogram must read the unformatted code from the standard input, and write the formatted code to the standard output.
By defining one or both of these variable in your .vimrc, you override any possible default value.
So, a complete definition for C# files could look like this:
2013-03-08 15:14:17 -05:00
```vim
let g:formatprg_cs = "astyle"
let g:formatprg_args_cs = "--mode=cs --style=ansi -p -c -H"
```
By default vim-autoformat equals `tabstop`, `softtabstop` and `shiftwidth` to 4 and sets `expandtab`, matching the default configuration for the default formatprograms.
This overwrites these options from your .vimrc.
If you don't want vim-autoformat to set these options, put this line in your .vimrc.
2013-03-12 19:26:51 -04:00
```vim
let g:autoformat_no_default_shiftwidth = 1
2013-03-12 19:26:51 -04:00
```
2013-03-09 13:58:32 -05:00
If you change the tabwidth for a certain formatprogram, I would suggest to change the indent options of vim correspondingly for that filetype.
2013-03-08 15:14:17 -05:00
```vim
2013-03-09 06:10:10 -05:00
au filetype *.cs set tabstop=2
au filetype *.cs set softtabstop=2
au filetype *.cs set shiftwidth=2
2013-03-08 15:14:17 -05:00
```
2012-12-03 15:37:30 -05:00
2013-01-20 12:56:32 -05:00
Todo list
---------
* Check for windows support.
2013-03-10 19:01:37 -04: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-03-16 05:38:45 -04:00
* Create a help file
2013-01-22 14:49:39 -05:00
2013-03-08 15:41:16 -05:00
If you have any suggestions on this plugin or on this readme, if you think some default formatprg definition is missing, or if you experience problems, please contact me by creating an issue in this repository.
2013-03-10 19:01:37 -04:00
You can also send a message to ctje92 at gmail dot com.
2013-03-09 06:10:10 -05:00
Change log
----------
2013-03-09 14:54:51 -05:00
### March 9 2013
2013-03-09 13:58:32 -05:00
The custom_config branch has been merged into the master branch.
* Customization of formatprograms can be done easily now, as explained above.
2013-03-09 13:24:05 -05:00
* I set the default tabwidth to 4 for all formatprograms as well as for vim itself.
2013-03-09 11:53:35 -05:00
* The default parameters for astyle have been slightly modified: it will wrap spaces around operators.
2013-03-09 14:55:36 -05:00
* phpCB has been removed from the defaults, due to code-breaking behaviour.
* XHTML default definition added
2013-03-10 15:17:54 -04:00
### 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 13 2013
* It is now possible to prevent vim-autoformat from overwriting your settings for `tabstop`, `softtabstop`, `shiftwidth` and `expandtab` in your .vimrc.