Re: How to get Allman style while typing and templated C++ code in Vim 7.3

453 views
Skip to first unread message

Reid Thompson

unread,
May 22, 2013, 1:22:04 PM5/22/13
to vim...@googlegroups.com, Reid Thompson
On Wed, 2013-05-22 at 08:43 -0700, arj...@gmail.com wrote:
> Hi guys, my first post here and this is my 3rd day with vim.
>
> I'm under Ubuntu 13.04 64 bit so I have vim 7.3 installed, I currently use clang_complete to get some C++ related features like autocompletition but I'm stuck with code formatting rules and I can't find a way to speed up some particular activity when creating new classes or new code that can be easily templated because it follows some clear patterns.
>
> I'm used to astyle ( http://astyle.sourceforge.net/ ) and I like the Allman style and I would love the ability to get some smart formatting while typing C++ code, for example I found really inconsistent what happens with the brackets, if I have..



http://lug.fh-swf.de/vim/vim-c/screenshots-en.html

http://www.thegeekstuff.com/2009/01/tutorial-make-vim-as-your-cc-ide-using-cvim-plugin/


http://lug.fh-swf.de/vim/vim-doc/csupport.html


--
If you have received the message in error, please advise the sender by
reply email and please delete the message. This message contains
information which may be confidential or otherwise protected. Unless
you are the addressee (or authorized to receive for the addressee), you
may not use, copy, or disclose to anyone the message or any information
contained in the message.


David Fishburn

unread,
May 22, 2013, 1:22:01 PM5/22/13
to vim_use



On Wed, May 22, 2013 at 11:43 AM, <arj...@gmail.com> wrote:
...
 
The second part it's about how under Vim it's possible to create some pre-packed files with some boring code and being able to just replace some key parts with some script, for example when creating a new C++ class I need the usual set of boring methods and ctors and the only thing that changes most of the time it's just the name of the class, so I would like to spend less time coding this repetitive portions of the code and kinda template and script the code generation for my source files

There are various solutions for templates on vim.org.

Have you tried UltiSnips, which supports templates from various different templating solutions?


David 

Ben Fritz

unread,
May 22, 2013, 2:01:12 PM5/22/13
to vim...@googlegroups.com
On Wednesday, May 22, 2013 10:43:48 AM UTC-5, arj...@gmail.com wrote:
> Hi guys, my first post here and this is my 3rd day with vim.
>

Welcome!

>
> I'm used to astyle ( http://astyle.sourceforge.net/ ) and I like the Allman style and I would love the ability to get some smart formatting while typing C++ code

It looks from the description of astyle, that you should just be able to use astyle still, automatically as you type in Vim.

See :help 'formatprg' and :help 'formatoptions'.

> My actual .vimrc is this one
> ...
> syntax on
> ...
> set autoindent smartindent " auto/smart indent
> ...
> set autoindent
> set cindent

You have autoindent turned on twice, and smartindent (which I would recommend NOT using, it's not smart at all), and also cindent. cindent will override all the previous ones.

Additionally, normally you don't turn cindent on in your .vimrc.

Instead, right before your "syntax on" command, put:

filetype indent on

or

filetype plugin indent on

This will turn on Vim's smart filetype-based indenting.

If you want to use astyle you may not want to put the "indent" in there however, or you could override it for C/C++ code using the "after" directory (:help after-directory).

arj...@gmail.com

unread,
May 23, 2013, 3:53:58 AM5/23/13
to vim...@googlegroups.com
Now i have

############################################
" gnome-shell workaround http://askubuntu.com/questions/67/how-do-i-enable-full-color-support-in-terminal
if $COLORTERM == 'gnome-terminal'
set t_Co=256
endif

syntax on
autocmd FileType cpp set formatprg=astyle\ -s2pPA1
set number " show numbers on the left side
set scrolloff=5 " keep at least 5 lines above/below
set sidescrolloff=5 " keep at least 5 lines left/right

" tab & space related settings
set tabstop=2
set shiftwidth=2
set expandtab

colorscheme distinguished

" PLUGINS SETTINGS

" pathogen
" execute pathogen#infect()

" clang_complete settings
let g:clang_user_options="-std=c++0x"
############################################

but I got nothing out of this formatpgr, it's like nothing changed, not even with gg=G, I don't know where to look anymore, vim still uses the old formatting tool, it's supposed to be ok as far as I know.

arj...@gmail.com

unread,
May 23, 2013, 3:57:14 AM5/23/13
to vim...@googlegroups.com
looks ok, but apparently doesn't have 1 feature that I would like to have, for example after editing and adding several snippets to different files in different times, I would like to have the ability to go back and change a value for all the files in a single command, apparently after adding this snippets there is plain text and you can't really go back and pretend to re-edit things in a scripted way.

arj...@gmail.com

unread,
May 23, 2013, 3:59:59 AM5/23/13
to vim...@googlegroups.com, Reid Thompson
thanks, that documentation looks oddly paginated and not that easy to read, there is an rst version or a pdf one ? There is a more specific C++ plugin maybe with C++11 support ? I don't care about C.

Christian Brabandt

unread,
May 23, 2013, 6:03:44 AM5/23/13
to vim...@googlegroups.com
On Thu, May 23, 2013 09:59, arj...@gmail.com wrote:
> thanks, that documentation looks oddly paginated and not that easy to
> read, there is an rst version or a pdf one ? There is a more specific C++
> plugin maybe with C++11 support ? I don't care about C.

I don't know. Try, of any of those help you:
https://github.com/vim-jp/cpp-vim
http://www.vim.org/scripts/script.php?script_id=4293
http://www.vim.org/scripts/script.php?script_id=3797

regards,
Christian

Ben Fritz

unread,
May 23, 2013, 7:37:34 AM5/23/13
to vim...@googlegroups.com
On Thursday, May 23, 2013 2:53:58 AM UTC-5, arj...@gmail.com wrote:
>
> Now i have
>
> ...

>
> autocmd FileType cpp set formatprg=astyle\ -s2pPA1
>
> ...

>
> but I got nothing out of this formatpgr, it's like nothing changed, not even with gg=G, I don't know where to look anymore, vim still uses the old formatting tool, it's supposed to be ok as far as I know.

After you edit your cpp file, are things set as you expect?

:verbose set formatprg? formatexpr? filetype? formatoptions?

Reid Thompson

unread,
May 23, 2013, 10:44:57 AM5/23/13
to arj...@gmail.com, Reid Thompson, vim...@googlegroups.com
On Thu, 2013-05-23 at 00:59 -0700, arj...@gmail.com wrote:
>
> thanks, that documentation looks oddly paginated and not that easy to read, there is an rst version or a pdf one ? There is a more specific C++ plugin maybe with C++11 support ? I don't care about C.
it supports both c and c++

http://www.vim.org/scripts/script.php?script_id=213 --> The key mappings of this plugin (PDF) : http://lug.fh-swf.de/vim/vim-c/c-hotkeys.pdf

The help file online : http://lug.fh-swf.de/vim/vim-c/csupport.html

Reid Thompson

unread,
May 23, 2013, 10:46:12 AM5/23/13
to arj...@gmail.com, Reid Thompson, vim...@googlegroups.com
On Thu, 2013-05-23 at 14:44 +0000, Reid Thompson wrote:
> On Thu, 2013-05-23 at 00:59 -0700, arj...@gmail.com wrote:
> >
> > thanks, that documentation looks oddly paginated and not that easy to read, there is an rst version or a pdf one ? There is a more specific C++ plugin maybe with C++11 support ? I don't care about C.
> it supports both c and c++
>
> http://www.vim.org/scripts/script.php?script_id=213 --> The key mappings of this plugin (PDF) : http://lug.fh-swf.de/vim/vim-c/c-hotkeys.pdf
>
> The help file online : http://lug.fh-swf.de/vim/vim-c/csupport.html

forgot the vim.org page
http://www.vim.org/scripts/script.php?script_id=213

Gary Johnson

unread,
May 23, 2013, 1:51:02 PM5/23/13
to vim...@googlegroups.com
On 2013-05-23, arjicot wrote:
> Il giorno mercoled� 22 maggio 2013 20:01:12 UTC+2, Ben Fritz ha scritto:
> autocmd FileType cpp set formatprg=astyle\ -s2pPA1

> ############################################
>
> but I got nothing out of this formatpgr, it's like nothing
> changed, not even with gg=G, I don't know where to look anymore,
> vim still uses the old formatting tool, it's supposed to be ok as
> far as I know.

The 'formatprg' option does not do automatic formatting unless the
'a' flag of 'formatoptions' is set, which I would not recommend in
this case. Further, the = command uses 'equalprg', not 'formatprg'.

So, to format your code using astyle as the 'formatprg', you will
need to manually reformat sections as you go with gq, not =.

Alternatively, you could set 'equalprg' to astyle, then use the =
command.

I think the behavior may be different between using astyle as the
'formatprg' vs. the 'equalprg', but I don't know what it is off the
top of my head.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages