Vim re-indentation option - vim

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.

Related

vim command-line snippets possible?

Is there any way to define and use snippets on the vim command-line? For example, when I want to select a portion of text I want to replace a string of text within, excluding the rest of the line, I have to change:
:'<,'>
to
:%s\%VsPat/rPat/g
which once in awhile might be alright, but lately I find myself performing this kind of s/S/R/ often enough to make it a PITA; yet still more efficient than making each change in the selection manually.
Actually there are quite a few regular editing command I use in VIM that would increase my efficiency if I could tie them to a snippet somehow. So is there any way to use snippets at vim command-line?
What about just going on with your substitution without changing anything?
:'<,'>s/\%VsPat/rPat/g
Or using a visual mode mapping?
xnoremap <key> :s/\%V/g<left><left>
There is build-in feature cnoreabbrev it is simple replacement, so if you define it for example
:cnoreabbrev ss %s\%VsPat/rPat/g
then when you type :ss and press <space>, it will automatically replace ss with following
:%s\%VsPat/rPat/g
Check it out, it should solve at least half of your problems :-)

Table-of-contents with VIM folding

A long time ago there was an editor called MultiEdit. It has a feature which I can not find in all powerful VIM. In MultiEdit I could press some hot key and it would show something like table-of-contents (aka, condensed-mode, aka outline) were I could see only 1st line of all functions (let it be C source) in current file. I could then move cursor to function that I need and after pressing enter, mode will switch to normal and I would be in function that I need. Very useful for those who likes to put many functions in one file. This feature was extremely simple to use: one config option to enter regex for selecting title-lines and one hotkey for mode toggle.
VIM has folding. But this is different. Folding hides parts of files and displays folded-lines-indicator. It is much more difficult to specify what to hide in folds for table-of-content-display: you need to start fold at title-line and end before next title line. This is more complex than simple regex to select titles.
I so much wanted this feature, I even wrote VIM macro to emulate this MultiEdit behavior, even though I don't know VIM that much. I've wrote it in part because it was easier to to learn a new language and write a macro than to figure out VIM folding module complexity.
Unfortunately, after upgrading VIM, this macro now does not work (infinite loop?). I've wrote it long time ago, and what little I did know about VIM is all forgotten and I could not fix it now. (EDIT: I've fixed my script. Thanks to #romainl for the link).
My question is how to get this table-of-content like display in VIM?
This recent vimcast by Drew Neil explains the generalities of folding and this one goes through the process of creating the kind of folding you are (probably) after.
Depending on the language you work with and your coding style, something as simple as
set foldmethod=marker
set foldmarker={,}
and zM can get you a long way:
If you want to customize what information is displayed, the second link above is almost certainly just what you need.
Have a look at ctags and the vim plugin tagbar.
You also check out the vim plugin unite with the extension Unite-Ouline
it gives a behavior quite close to what you describe.
It's not great but have you tried setlocal foldmethod=syntax? It seems to do a decent job in 7.3 on Windows. ...although I just realized that our coding standard has the opening brace for a block at the end of the line rather than on a new line and if I change to having it on a new line it works substantially less well.

Trigger command on buffer load and buffer save

I want to write a vim plugin that does certain text transformations to the text while in the editor, but I don't want these transformations visible inside the file.
As an example, consider the word Gnarly in a text file I want to edit. Upon load I would want my vim script change that to G, but when I save the buffer I want it to expanded back to Gnarly.
My scenario is a little bit more complex because it will involve an external script, but I want to see exactly how that would be invoked.
Also I'd want to be able to apply this change only to some files based on their extension.
See :h autocmd. The events you need are BufRead and BufWrite.
Maybe you will be interested by :h conceal.
First of all, define your own filetype, e.g. gnarly. Read :help new-filetype for the details, but basically it's this autocmd:
:autocmd BufRead,BufNewFile *.gnarly set filetype=gnarly
Then, the conceal feature introduced in Vim 7.3 is the way to go. Write a syntax script ~/.vim/syntax/gnarly.vim. For your example it would contain:
:syntax keyword gnarlyConceal Gnarly conceal cchar=G
But you can also use :syntax match for more complex patterns.
Finally, concealment is off by default. To turn it on, put the following command into ~/.vim/ftplugin/gnarly.vim (you could put it into the syntax file, too, but this separation is recommended and done by all full plugins that ship with Vim):
:setlocal conceallevel=1
You can customize the 'concealcursor' behavior, too. If you still need help, have a look at the help pages, or existing plugins that use concealment.

Specify which function to fold

Is it possible to specify which functions should be folded by vim automatically.
In Netbeans, there is something like
// <editor-fold defaultstate="collapsed" desc="user-description">
...any code...
// </editor-fold>
Do you know about something similar I can use in vim?
When I close the vim I want folded functions to be folded again if I open the file again.
This is actually a little different than what you're asking, because it doesn't deal with semantic folding, which NetBeans and other IDEs do. However, storing a set of folds is normally done using the :mkview command, and you can automate this command and the :loadview command to make it transparent to you. The details are in this Vim wiki page. I use one of the simpler versions in my vimrc, rather than the plugin, but both should work for what you need.

Auto formatting for vi?

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

Resources