Auto formatting for vi? - vim

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)

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

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

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.

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".

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.

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! (-:

Related

Vim: good pascal support (syntax, indent, probably omnicomplete)

I need to work on existing freepascal project ( it's an open-source project double commander ), so I'm trying to set Vim for editing source files, but pascal support bundled with Vim is surprisingly ugly.
More, I can't even find anything for better pascal support. At least, I really need for working syntax and indent scripts (both of them are buggy by default).
It would be very nice to set up omnicompletion too, but it seems this is completely hopeless.
I will probably hack on syntax and indent scripts myself, but I can't believe we still don't have them, so I decided to ask first.
Perhaps you can improve the situation by telling Vim to use the "delphi" mode instead of the "pascal" mode (this trick works with Emacs too). The reason for this is that in general "Pascal" refers to the old language designed by Wirth in the '70, while Free Pascal incorporates many syntactical additions introduced by Turbo Pascal and Delphi in the following years. Most of the development on Vim/Emacs modes usually goes in the Delphi mode, not the Pascal one.
Have a look at http://www.vim.org/scripts/script.php?script_id=3078. This will not provide autocompletion, but at least it will highlight keywords like "unit", "interface", "implementation", etc., as well as // comments, correctly.
As VIM 7.4.52 for Linux there isn't 'Delphi' option. So far, the 'Pascal' mode works good.
for a freepascal project (fpc), you need to get fpc.vim.
Doing it with 'Plug'
open your .vimrc file and add:
call plug#begin('~/.vim/plugged')
Plug 'vim-scripts/fpc.vim'
call plug#end()
reopen your vim, and use the command :PlugInstall
Highlighting
in order to highlight pascal use command:
:set filetype=pascal
this also enables indent for the code. and it will auto indent the existing code.
or you can use:
:set syntax=pascal
this may fail to indent.

Vim Code Folding for Scilab

I've just recently started to use the folding functionality of vim and it's very helpful for the languages that it works really well for.
My issue lies in the way vim comments out the fold markers in scilab code. It defaults to
/*{{{*/ and /*}}}*/
which works great in languages like C, but is not actually a comment in scilab. I get a multiplication error when i try to run the code.
I've tried adding
autocmd FileType scilab set fmr=//{{{,//}}}
to my .vimrc file which doesn't quite do what I'd like. It results in
/*//{{{*/ and /*//}}}*/
which are still not comments. The code "folds" fine but becomes unusable. I could set up a macro to replace every instance of "/*" with "//", but that could have unintended consequences when applied globally to a file.
So the question is: how can i setup vim fold markers comments for scilab files such that it will not render the file unusable?
You do not add the comments to 'foldmarker' itself, there's the 'commentstring' option that influences how Vim surrounds the fold markers (when creating folds with zf). Try setting
:setlocal commentstring=//%s
for your scilab filetype. (Put the command in ~/.vim/after/ftplugin/scilab.vim to make it permanent.)
It sounds to me like vim doesn't understand how SciLab comments work. I'm not sure if vim comes with SciLab syntax logic these days, is syntax highlighted correctly in your SciLab files? If not, you can get the syntax file from here.
Is there a particular reason you want to use marks? They aren't actually needed. If you don't want vim to auto-fold by syntax or indentation level, you can still manually define folds with
:set foldmethod=manual
That lets you select a chunk of text in visual mode and make it into a fold with 'zf'. No marks required.
More info on various vim folding techniques here.

Vim re-indentation option

I want to convert C files from one coding style to other. I can use indent and other similar tools. But, I was wondering, if Vim has a built-in support for this kind of need.
Vim's automatic indenting can be configured in various ways. Your first step is to get it so that it's indenting new code that you type in the style you want.
Then you can reformat any existing code with the = command. For example =G will reformat from the current line to the end of the file.
Sorry, there's no :KRToAllman or :AllmanToKR command built into Vim. You'll need to play with indent or some other external program.
While I doubt you can use it to switch from one style to the other, you could probably take a look at :h 'cinoptions' and :h c-indenting.

Configure tab to show list of variants instead of cycling through in VIM

When opening new buffer it VIM, I type:
new /path/to/fi
If I hit "tab" at this point it cycles through files. How to configure VIM to show list of variants instead of going for the first one?
set wildmenu
Is all you need to add to your .vimrc. Read :help wildmenu.
Set your wildmode setting to something different, for example
set wildmode=list:longest
If I misunderstood the question completely, yell ... :)
(This is not a direct answer to your question, but I think it's even better :)
You should check out the Command-T plugin, inspired by TextMate's 'Go To File'. It filters out possible combinations very intelligently, just type a few characters of each subdirectory enough to distinguish it and it 'gets' it, the characters don't have to be at the beginning and can don't have to be sequential. It also shows you a list of options left.
I realize this is a terrible explanation so check out this video to see how it works.
The downside is it requires Vim to be compiled with Ruby support.
Control-P (ctrlp.vim) is a replacement for Command-T written in VimScript, so it doesn't require Ruby.

How do you use indent in vim for web development?

I'm starting to use Linux and Vim at work. I'm started reading vims documentation and creating my own .vimrc file and such.
I'm a web developer working with HTML, XML, CSS, JS, Python, PHP, ZPT, DTML and SQL.
I would like to have an indent feature like this one: for each language/set, a corresponding indent solution.
So, in js, writing function test(){|} would turn in
function test(){
|
}
If php, writing <?php function test(){|}:
<?php
function test(){
|
}
?>
...and such. Writing a function definition in Python, and then creating a for loop sentece, it would automatically create an indent.
I'm starting with autoindent, smartindent, cindent but I'm a little confused about their differences.
How do the indent in vim works? Am I supposed to download plugins for each language? Is the behavior I described possible with already existing plugins you're used to or do I have to create it?
I keep seeing people using Vim and I'm trying to do this as well since the machine I'm using is too limited, but I'm afraid I won't be able to have a decent auto indenting solution in it. And I really think that having to manually indent code all the time (instead of sometimes only) is a waste of time and it's against vim's "MOTTO" I've seen called "productivity".
(I have used autoindenting in a little small project in Visual Studio, and really liked their approach. Is there a plugin for that?)
Vim is usually pretty smart about indenting once you define the correct settings for tab size and the like. (Edit: As Igor mentions in the other answer, be sure to turn on filetype-specific indenting.) It seems that you want vim to automatically insert newlines though, which I don't think it can do without a plugin.
However, I think you may want to look at snipMate, which is a plugin that defines a large number of 'snippets' for different programming languages, and you can also define your own. It's basically a kind of improved tab-completion:
One example:
php<tab>
turns into
<?php
|
?>
With | being your cursor. Some snippets even define multiple cursor-positions which you can switch to with another press of tab.
vim usually comes with a bunch of syntax plugins for different languages. if you want to use those for indenting, you will need:
set autoindent
filetype indent on
you might also need syntax on but i'm not sure if that's needed for indenting. couldn't hurt though...
I found the setup for this based on a blog post:
set autoindent
inoremap {<CR> {<CR>}<Esc>O<Tab>
Having this with snipmate.vim and autoclose.vim is working flawlessly.

Resources