Auto formatting for vi?
Asked Answered
P

6

7

Does an auto-formatting tool exist for vi that'll allow me to define per language preferences?

edit: I'm not looking for syntax highlighting. I'm looking for something that will apply formatting rules to my code. (Like brace positioning, spaces around oeprators, etc)

Pleinair answered 20/1, 2009 at 23:27 Comment(0)
T
8

Well, there's Vim which comes with a lot of languages covered already and which is easy to customize per language.

Townes answered 20/1, 2009 at 23:30 Comment(0)
F
4

Vim has tons of support for filetype-specific customisations. You might find what you are looking for in there.

Folkmoot answered 21/1, 2009 at 0:38 Comment(0)
I
3

You can add a file in ~/.vim/ftplugin/ for each file type. For example, set ~/.vim/ftplugin/c.vim to

set tabstop=2 shiftwidth=2

This sets your indentation for C files to two spaces.

Italianize answered 21/1, 2009 at 1:40 Comment(1)
I found the default indent when auto-formatting ruby in vim too big, this helped to set the indent. I've added this to my ~/.vimrc file along with configuration from this question: https://mcmap.net/q/488081/-format-ruby-code-in-vimHeadpiece
S
2

You can use vim. If you're on GNU/Linux, take a look at /etc/vim/vimrc for global defaults. Some things you may want are "syntax on" "filetype indent on" and "set showmatch".

Scansorial answered 21/1, 2009 at 0:44 Comment(0)
S
2

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

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

It integrates external code-formatting programs into vim. For example, if you want to format C, C++, C# or Java code, you can install the program astyle, and vim sets it as the format program automatically.

Sulphanilamide answered 3/12, 2012 at 20:22 Comment(0)
S
1

As Darrin says, "flee from the vi wasteland" and embrace the one true vim path instead! Your desired language preferences, assuming that they're not for SNOBOL or Simula, will thank you!

Edit: Actually extending the syntax highlighting to cover SNOBOL or Simula would not be that hard! (-:

Spreadeagle answered 21/1, 2009 at 0:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.