Loading ...
Sorry, an error occurred while loading the content.

restyling C/C++ ?

Expand Messages
  • Joris Huizer
    Hello, I ve got this (?weird?) question: I d like to be able to clean up existing C/C++ code so it ll have a consistent view (like, same indenting
    Message 1 of 12 , Jun 2, 2004
    • 0 Attachment
      Hello,

      I've got this (?weird?) question: I'd like to be able to "clean up"
      existing C/C++ code so it'll have a consistent view (like, same
      indenting everywhere...)

      As I type new code, all is done fine by some vim settings I got
      somewhere, but I've got no idea of how to make vim restyle "existing"
      code. Is this possible?

      Thanks for any help,

      Joris
    • Brent Rice
      I believe that I would be correct in saying that you should use the = command followed by a motion (i.e. =% or =}). gg=G should restyle the complete file.
      Message 2 of 12 , Jun 2, 2004
      • 0 Attachment
        I believe that I would be correct in saying that you should use the =
        command followed by a motion (i.e. =% or =}).

        gg=G should restyle the complete file.

        Brent


        On Jun 2, 2004, at 3:28 PM, Joris Huizer wrote:

        > Hello,
        >
        > I've got this (?weird?) question: I'd like to be able to "clean up"
        > existing C/C++ code so it'll have a consistent view (like, same
        > indenting everywhere...)
        >
        > As I type new code, all is done fine by some vim settings I got
        > somewhere, but I've got no idea of how to make vim restyle "existing"
        > code. Is this possible?
        >
        > Thanks for any help,
        >
        > Joris
        >
      • Mikolaj Machowski
        ... gg=G m. -- LaTeX + Vim = http://vim-latex.sourceforge.net/ Vim-list(s) Users Map: (last change 10 Mar) http://skawina.eu.org/mikolaj/vimlist VimGDB -
        Message 3 of 12 , Jun 2, 2004
        • 0 Attachment
          Dnia środa, 2 czerwca 2004 23:28, Joris Huizer napisał:
          > Hello,
          >
          > I've got this (?weird?) question: I'd like to be able to "clean up"
          > existing C/C++ code so it'll have a consistent view (like, same
          > indenting everywhere...)
          >
          > As I type new code, all is done fine by some vim settings I got
          > somewhere, but I've got no idea of how to make vim restyle "existing"
          > code. Is this possible?
          >
          gg=G

          m.

          --
          LaTeX + Vim = http://vim-latex.sourceforge.net/
          Vim-list(s) Users Map: (last change 10 Mar)
          http://skawina.eu.org/mikolaj/vimlist
          VimGDB - http://skawina.eu.org/mikolaj/vimgdb
        • Joris Huizer
          Allright the gg=G does do the indenting :-) (what s the gg part there??) But there is still a small problem - in some ugly code lines aren t broken of the way
          Message 4 of 12 , Jun 2, 2004
          • 0 Attachment
            Allright the gg=G does do the indenting :-) (what's the gg part there??)
            But there is still a small problem - in some ugly code lines aren't
            broken of the way they should be, like, after an if() the code just
            continues on the same line (well this is "uglyfied" code to be honest,
            but it'd be really cool if vim could make it real nice code that'd be
            *really* good ;) )

            Also, a more practical problem, I found in the vim faq,
            (http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl#27.7) :
            :set comments=sr:/*,mb:*,el:*/
            will disable the repetition of the // comment in C++

            Is there a way to get this to work as a default? I tried putting that
            (without the : thing) in my ~/.vimrc but it wouldn't work -- can you
            tell me what's wrong? I attached my ~/.vimrc file.

            Thanks for your help so far,

            Joris

            Brent Rice wrote:
            > I believe that I would be correct in saying that you should use the =
            > command followed by a motion (i.e. =% or =}).
            >
            > gg=G should restyle the complete file.
            >
            > Brent
            >
            >
            > On Jun 2, 2004, at 3:28 PM, Joris Huizer wrote:
            >
            >> Hello,
            >>
            >> I've got this (?weird?) question: I'd like to be able to "clean up"
            >> existing C/C++ code so it'll have a consistent view (like, same
            >> indenting everywhere...)
            >>
            >> As I type new code, all is done fine by some vim settings I got
            >> somewhere, but I've got no idea of how to make vim restyle "existing"
            >> code. Is this possible?
            >>
            >> Thanks for any help,
            >>
            >> Joris
            >>
            >
            >
          • Antoine J. Mechelynck
            ... [...] RTFM gg Go to begin of file = {motion} Filter {motion} through the extarnal equalprg or, if undefined (default) through internal C-indenting
            Message 5 of 12 , Jun 2, 2004
            • 0 Attachment
              Joris Huizer <jorishuizer@...> wrote:
              > > Allright the gg=G does do the indenting :-) (what's the gg part
              > > there??)
              [...]

              RTFM

              gg Go to begin of file

              = {motion} Filter {motion} through the extarnal 'equalprg' or, if
              undefined (default) through internal C-indenting

              G Go to end of file.

              Regards,
              Tony.
            • Brent Rice
              gg simply moves the cursor to the upper left-hand side of the screen. Without that, =G would just format from the point you are in the file to the end, which
              Message 6 of 12 , Jun 2, 2004
              • 0 Attachment
                gg simply moves the cursor to the upper left-hand side of the screen.
                Without that, =G would just format from the point you are in the file
                to the end, which could be OK, but is not necessarily what you want.

                I am honestly not sure how to take care of the "uglified" code.

                As for your additions to the vimrc, they seem to be OK, at least from
                my semi-ignorant perspective. However, you definitely need to make
                sure that you either restart vim or do :source ~/.vimrc in order to
                make it take effect. Beyond this, no problem is apparent to me, but I
                am definitely no master of setting up the comment formats.

                Brent


                On Jun 2, 2004, at 4:13 PM, Joris Huizer wrote:

                > Allright the gg=G does do the indenting :-) (what's the gg part
                > there??)
                > But there is still a small problem - in some ugly code lines aren't
                > broken of the way they should be, like, after an if() the code just
                > continues on the same line (well this is "uglyfied" code to be honest,
                > but it'd be really cool if vim could make it real nice code that'd be
                > *really* good ;) )
                >
                > Also, a more practical problem, I found in the vim faq,
                > (http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl#27.7) :
                > :set comments=sr:/*,mb:*,el:*/
                > will disable the repetition of the // comment in C++
                >
                > Is there a way to get this to work as a default? I tried putting that
                > (without the : thing) in my ~/.vimrc but it wouldn't work -- can you
                > tell me what's wrong? I attached my ~/.vimrc file.
                >
                > Thanks for your help so far,
                >
                > Joris
                >
                > Brent Rice wrote:
                >> I believe that I would be correct in saying that you should use the =
                >> command followed by a motion (i.e. =% or =}).
                >> gg=G should restyle the complete file.
                >> Brent
                >> On Jun 2, 2004, at 3:28 PM, Joris Huizer wrote:
                >>> Hello,
                >>>
                >>> I've got this (?weird?) question: I'd like to be able to "clean up"
                >>> existing C/C++ code so it'll have a consistent view (like, same
                >>> indenting everywhere...)
                >>>
                >>> As I type new code, all is done fine by some vim settings I got
                >>> somewhere, but I've got no idea of how to make vim restyle
                >>> "existing" code. Is this possible?
                >>>
                >>> Thanks for any help,
                >>>
                >>> Joris
                >>>
                >
                > " An example for a vimrc file.
                > "
                > " Maintainer: Bram Moolenaar <Bram@...>
                > " Last change: 2002 May 28
                > "
                > " To use it, copy it to
                > " for Unix and OS/2: ~/.vimrc
                > " for Amiga: s:.vimrc
                > " for MS-DOS and Win32: $VIM\_vimrc
                > " for OpenVMS: sys$login:.vimrc
                >
                > " When started as "evim", evim.vim will already have done these
                > settings.
                > if v:progname =~? "evim"
                > finish
                > endif
                >
                > " Use Vim settings, rather then Vi settings (much better!).
                > " This must be first, because it changes other options as a side
                > effect.
                > set nocompatible
                >
                > " allow backspacing over everything in insert mode
                > set backspace=indent,eol,start
                >
                > set autoindent " always set autoindenting on
                > "if has("vms")
                > set nobackup " do not keep a backup file, use versions instead
                > "else
                > " set backup " keep a backup file
                > "endif
                > set history=50 " keep 50 lines of command line history
                > set ruler " show the cursor position all the time
                > set showcmd " display incomplete commands
                > set incsearch " do incremental searching
                >
                > " For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu
                > entries
                > " let &guioptions = substitute(&guioptions, "t", "", "g")
                >
                > " Don't use Ex mode, use Q for formatting
                > map Q gq
                >
                > " This is an alternative that also works in block mode, but the deleted
                > " text is lost and it only works for putting the current register.
                > "vnoremap p "_dp
                >
                > " Switch syntax highlighting on, when the terminal has colors
                > " Also switch on highlighting the last used search pattern.
                > if &t_Co > 2 || has("gui_running")
                > syntax on
                > " set hlsearch
                > endif
                >
                > " don't make it look like there are line breaks where there aren't:
                > " set nowrap
                >
                > " use indents of 2 spaces, and have them copied down lines:
                > set shiftwidth=2
                > set shiftround
                > set expandtab
                > set autoindent
                >
                >
                > au BufNewFile,BufRead *.html set nocindent
                > " au BufNewFile,BufRead *.c *.js *.cc *.cpp set cindent
                > au BufNewFile,BufRead *.c set cindent
                > au BufNewFile,BufRead *.js set cindent
                >
                > set comments=sr:/*,mb:*,el:*/
                >
                > " Only do this part when compiled with support for autocommands.
                > if has("autocmd")
                >
                > " Enable file type detection.
                > " Use the default filetype settings, so that mail gets 'tw' set to
                > 72,
                > " 'cindent' is on in C files, etc.
                > " Also load indent files, to automatically do language-dependent
                > indenting.
                > " filetype plugin indent on
                >
                > " For all text files set 'textwidth' to 78 characters.
                > autocmd FileType text setlocal textwidth=78
                >
                > " When editing a file, always jump to the last known cursor position.
                > " Don't do it when the position is invalid or when inside an event
                > handler
                > " (happens when dropping a file on gvim).
                > autocmd BufReadPost *
                > \ if line("'\"") > 0 && line("'\"") <= line("$") |
                > \ exe "normal g`\"" |
                > \ endif
                >
                > endif " has("autocmd")
                >
              • Erlend Hamberg
                ... I don t think Vim can do that, but astyle can. http://astyle.sourceforge.net/ -- Erlend Hamberg ehamberg@online.no All your Languages are belong to us! You
                Message 7 of 12 , Jun 2, 2004
                • 0 Attachment
                  On Wednesday 02 June 2004 23:28, Joris Huizer wrote:
                  > As I type new code, all is done fine by some vim settings I got
                  > somewhere, but I've got no idea of how to make vim restyle "existing"
                  > code. Is this possible?

                  I don't think Vim can do that, but astyle can.
                  http://astyle.sourceforge.net/

                  --
                  Erlend Hamberg
                  ehamberg@...

                  All your Languages are belong to us!
                  You have no chance to compile!
                  make your_time
                • Brent Rice
                  From the usage of astyle (what I understand from having glanced at the documentation), it appears that you could probably just use ... or, for a visual line
                  Message 8 of 12 , Jun 2, 2004
                  • 0 Attachment
                    From the usage of astyle (what I understand from having glanced at the
                    documentation), it appears that you could probably just use

                    :%!astyle_command_with_the_options_that you want

                    or, for a visual line selection,

                    :'<,'>!astyle_command_with_the_options_that you want

                    from within vim. I obviously have not tested this, but figured that it
                    was a simple addition worth noting.

                    Brent



                    On Jun 2, 2004, at 5:23 PM, Erlend Hamberg wrote:

                    > On Wednesday 02 June 2004 23:28, Joris Huizer wrote:
                    >> As I type new code, all is done fine by some vim settings I got
                    >> somewhere, but I've got no idea of how to make vim restyle "existing"
                    >> code. Is this possible?
                    >
                    > I don't think Vim can do that, but astyle can.
                    > http://astyle.sourceforge.net/
                    >
                    > --
                    > Erlend Hamberg
                    > ehamberg@...
                    >
                    > All your Languages are belong to us!
                    > You have no chance to compile!
                    > make your_time
                    >
                  • PAN Shizhu
                    I recommend you use GNU indent together with the = command. ... then gg=G The GNU indent is a very feature rich and highly customizable utility. IMO it is
                    Message 9 of 12 , Jun 3, 2004
                    • 0 Attachment
                      I recommend you use GNU indent together with the '=' command.

                      In VIM you can do:
                      :%!indent
                      then
                      gg=G

                      The GNU indent is a very feature rich and highly customizable utility. IMO
                      it is far versatile than astyle. If you carefully choose the parameters of
                      GNU indent to suit your need, you may omit the step 'gg=G'.

                      The GNU indent is avaiable online, and is included in most GNU
                      distributions. (Linux, FreeBSD, Cygwin, etc.)

                      HTH
                      Pan Shizhu.

                      _________________________________________________________________
                      与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn
                    • Charlie Farbstein
                      The last time I checked, GNU indent worked well for C code but did not handle C++ code very well. Charlie Farbstein ... From: PAN Shizhu
                      Message 10 of 12 , Jun 3, 2004
                      • 0 Attachment
                        The last time I checked, GNU indent worked well for C code but did not
                        handle C++ code very well.

                        Charlie Farbstein

                        ----- Original Message -----
                        From: "PAN Shizhu" <dicpan@...>
                        Date: Thursday, June 3, 2004 0:35 am
                        Subject: Re: restyling C/C++ ?

                        > I recommend you use GNU indent together with the '=' command.
                        >
                        > In VIM you can do:
                        > :%!indent
                        > then
                        > gg=G
                        >
                        > The GNU indent is a very feature rich and highly customizable
                        > utility. IMO
                        > it is far versatile than astyle. If you carefully choose the
                        > parameters of
                        > GNU indent to suit your need, you may omit the step 'gg=G'.
                        >
                        > The GNU indent is avaiable online, and is included in most GNU
                        > distributions. (Linux, FreeBSD, Cygwin, etc.)
                        >
                        > HTH
                        > Pan Shizhu.
                        >
                        > _________________________________________________________________
                        > ���������������������������� MSN Messenger:
                        > http://messenger.msn.com/cn
                        >
                        >
                      • Neil Bird
                        Around about 02/06/2004 23:13, Joris Huizer typed ... ... I guess it s being set by the cpp.vim syntax file; any setting you make will then be reset by that.
                        Message 11 of 12 , Jun 7, 2004
                        • 0 Attachment
                          Around about 02/06/2004 23:13, Joris Huizer typed ...
                          > :set comments=sr:/*,mb:*,el:*/
                          >
                          > Is there a way to get this to work as a default? I tried putting that
                          > (without the : thing) in my ~/.vimrc but it wouldn't work -- can you
                          > tell me what's wrong? I attached my ~/.vimrc file.

                          I guess it's being set by the cpp.vim syntax file; any setting you
                          make will then be reset by that. Create a ~/.vim/after/syntax/cpp.vim
                          and put it in there (this file is run after the cpp syntax file is run).

                          --
                          [neil@fnx ~]# rm -f .signature
                          [neil@fnx ~]# ls -l .signature
                          ls: .signature: No such file or directory
                          [neil@fnx ~]# exit
                        • Benji Fisher
                          ... I think that gg=G has already been explained well. I can confirm what another post suggested tentatively: the internal reformatting done with = only
                          Message 12 of 12 , Jun 9, 2004
                          • 0 Attachment
                            On Thu, Jun 03, 2004 at 12:13:48AM +0200, Joris Huizer wrote:
                            > Allright the gg=G does do the indenting :-) (what's the gg part there??)
                            > But there is still a small problem - in some ugly code lines aren't
                            > broken of the way they should be, like, after an if() the code just
                            > continues on the same line (well this is "uglyfied" code to be honest,
                            > but it'd be really cool if vim could make it real nice code that'd be
                            > *really* good ;) )

                            I think that "gg=G" has already been explained well. I can confirm
                            what another post suggested tentatively: the internal reformatting done
                            with = only affects indent, i.e., leading white space. It will not
                            break nasty lines.

                            If you combine some suggestions from other posts, you may get
                            something very convenient to use. First, RTFM: start with

                            :help =

                            and follow the link to the explanation of 'equalprog'. Second, see
                            whether astyle or GNU indent, or some other program, does what you want.
                            Finally,

                            :set equalprg=...
                            gg=G

                            > Also, a more practical problem, I found in the vim faq,
                            > (http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl#27.7) :
                            > :set comments=sr:/*,mb:*,el:*/
                            > will disable the repetition of the // comment in C++
                            >
                            > Is there a way to get this to work as a default? I tried putting that
                            > (without the : thing) in my ~/.vimrc but it wouldn't work -- can you
                            > tell me what's wrong? I attached my ~/.vimrc file.
                            [snip]

                            From the part I snipped, it looks as though you started with the
                            sample vimrc file from the distribution, so things like ftplugins should
                            be enabled. Then you want to put the :set lines (the one I suggested
                            and the one quoted above) in your ftplugin/c.vim file, or maybe
                            after/ftplugin/c.vim . See

                            :help ftplugin-overrule

                            Or maybe you should start earlier in the docs, with

                            :help ftplugins

                            I strongly suggest using an ftplugin/ directory instead of a syntax/
                            directory, as another post suggested.

                            HTH --Benji Fisher
                          Your message has been successfully submitted and would be delivered to recipients shortly.