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

How to apply the right indentation

Expand Messages
  • caruso_g
    Hi group, I just started to use Vim (actually, MacVim) and I really love it. :) I would like to ask you how to re-indent old files I edited in TextMate. They
    Message 1 of 6 , Oct 3, 2009
    • 0 Attachment
      Hi group,
      I just started to use Vim (actually, MacVim) and I really love it. :)
      I would like to ask you how to re-indent old files I edited in
      TextMate. They use spaces instead of tabs, set to two spaces.
      Now I have some rows rightly indented to the Vim default and some
      stick to two spaces like in TextMate.
      Which is the command to reformat them?
      And, which are the settings to add to my .vimrc file?
      Thanks you all.
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tim Chase
      ... Welcome aboard! ... which you can learn about at ... Setting the tabstop to 2 changes your tab-stops to 2-visual-spaces-per-tab. I like to keep my
      Message 2 of 6 , Oct 3, 2009
      • 0 Attachment
        > I just started to use Vim (actually, MacVim) and I really love it. :)

        Welcome aboard!

        > I would like to ask you how to re-indent old files I edited in
        > TextMate. They use spaces instead of tabs, set to two spaces.
        > Now I have some rows rightly indented to the Vim default and some
        > stick to two spaces like in TextMate.
        > Which is the command to reformat them?

        The settings are controlled by various correlated settings:

        :set ts=2 sw=2 et

        which you can learn about at

        :help 'tabstop'
        :help 'shiftwidth'
        :help 'expandtab'
        :help :retab

        Setting the 'tabstop' to 2 changes your tab-stops to
        2-visual-spaces-per-tab. I like to keep my 'shiftwidth' in sync
        with my 'tabstop' setting just so things like the ">"/"<"
        operators and ^D/^T work as expected in insert-mode.

        If Vim is inserting real tabs instead of expanding spaces, the
        'expandtab' setting will change future insertions. To change
        past insertions of real-tabs, once you have the above ":set"
        command, you can then use ":retab" to force vim to change
        existing tabs to the spaces you defined.

        > And, which are the settings to add to my .vimrc file?

        You should be able to just add the above "set" (minus the colon)
        to your vimrc to get this behavior by default. Alternatively, if
        you allow modelines, you can include a modeline in your files
        that specifies these settings:

        :help modeline
        :help 'modeline'

        which would allow you to put something like

        /* vim: set tw=2 sw=2 et */

        at the top or bottom of your file, and vim will set them on a
        per-file basis.

        Hope this helps,

        -tim




        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • caruso_g
        Hi Tim, thanks a lot, I tried the :set ts=2 sw=2 et and it worked perfectly but only on the new tabs inserted by MacVim. ... or also without the number value
        Message 3 of 6 , Oct 3, 2009
        • 0 Attachment
          Hi Tim,
          thanks a lot, I tried the ":set ts=2 sw=2 et" and it worked perfectly
          but only on the new tabs inserted by MacVim.
          I tried to select all the text and to apply the command:

          :'<, '>retab!4

          or also without the number value (since accordingly to the manual it
          should set it the ts value) but it didn't change.
          I don't know if I invoked in the right way the command, did I?

          Anyway, thanks a lot, I am going to set them in my .vimrc file for
          next files. :)
          Is it right in the following way?

          set ts=2 sw=2 et

          One last question. Can I set different values for different file
          types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?

          Thanks a lot. :)

          On 3 Ott, 17:35, Tim Chase <v...@...> wrote:
          > > I just started to use Vim (actually, MacVim) and I really love it. :)
          >
          > Welcome aboard!
          >
          > > I would like to ask you how to re-indent old files I edited in
          > > TextMate. They use spaces instead of tabs, set to two spaces.
          > > Now I have some rows rightly indented to the Vim default and some
          > > stick to two spaces like in TextMate.
          > > Which is the command to reformat them?
          >
          > The settings are controlled by various correlated settings:
          >
          >    :set ts=2 sw=2 et
          >
          > which you can learn about at
          >
          >    :help 'tabstop'
          >    :help 'shiftwidth'
          >    :help 'expandtab'
          >    :help :retab
          >
          > Setting the 'tabstop' to 2 changes your tab-stops to
          > 2-visual-spaces-per-tab.  I like to keep my 'shiftwidth' in sync
          > with my 'tabstop' setting just so things like the ">"/"<"
          > operators and ^D/^T work as expected in insert-mode.
          >
          > If Vim is inserting real tabs instead of expanding spaces, the
          > 'expandtab' setting will change future insertions.  To change
          > past insertions of real-tabs, once you have the above ":set"
          > command, you can then use ":retab" to force vim to change
          > existing tabs to the spaces you defined.
          >
          > > And, which are the settings to add to my .vimrc file?
          >
          > You should be able to just add the above "set" (minus the colon)
          > to your vimrc to get this behavior by default.  Alternatively, if
          > you allow modelines, you can include a modeline in your files
          > that specifies these settings:
          >
          >    :help modeline
          >    :help 'modeline'
          >
          > which would allow you to put something like
          >
          >    /* vim: set tw=2 sw=2 et */
          >
          > at the top or bottom of your file, and vim will set them on a
          > per-file basis.
          >
          > Hope this helps,
          >
          > -tim
          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • John Beckett
          ... I have put a new section ( Different settings for different file types ) in this tip: http://vim.wikia.com/wiki/Indenting_source_code Would Vimmers please
          Message 4 of 6 , Oct 4, 2009
          • 0 Attachment
            caruso_g wrote:
            > One last question. Can I set different values for different
            > file types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?

            I have put a new section ("Different settings for different file
            types") in this tip:
            http://vim.wikia.com/wiki/Indenting_source_code

            Would Vimmers please have a look at the above and let me know if
            any changes should be made because I find it incredibly
            irritating that we do not have an easily-accessible and
            "correct" set of answers to good questions such as the above.

            BTW, for all new users:
            Please bottom post on this list. Quote a small (relevant) part
            of the message you are replying to, and put your text underneath.

            See
            http://groups.google.com/group/vim_use/web/vim-information

            John


            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Robert H
            On 10/3/09 5:56 PM, caruso_g wrote: ... autocmd FileType perl :set ts=4 sw=4 et tw=78 autocmd FileType xml :set ts=2 sw=2 autocmd FileType html
            Message 5 of 6 , Oct 4, 2009
            • 0 Attachment
              On 10/3/09 5:56 PM, caruso_g wrote:
              <snip>
              > set ts=2 sw=2 et
              >
              > One last question. Can I set different values for different file
              > types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?
              >

              autocmd FileType perl :set ts=4 sw=4 et tw=78
              autocmd FileType xml :set ts=2 sw=2
              autocmd FileType html :set ts=2 sw=2 et tw=78

              Those are just examples...you can do your own. :-)


              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Tony Mechelynck
              ... Or rather, put them into vim-scripts in ~/.vim/after/ftplugin/ (on Unix) ... setlocal ts=4 sw=4 tw=78 et ... setlocal ts=2 sw=2 tw=0 noet etc. (Create the
              Message 6 of 6 , Nov 1, 2009
              • 0 Attachment
                On 04/10/09 22:09, Robert H wrote:
                >
                > On 10/3/09 5:56 PM, caruso_g wrote:
                > <snip>
                >> set ts=2 sw=2 et
                >>
                >> One last question. Can I set different values for different file
                >> types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?
                >>
                >
                > autocmd FileType perl :set ts=4 sw=4 et tw=78
                > autocmd FileType xml :set ts=2 sw=2
                > autocmd FileType html :set ts=2 sw=2 et tw=78
                >
                > Those are just examples...you can do your own. :-)

                Or rather, put them into vim-scripts in ~/.vim/after/ftplugin/ (on Unix)
                or in ~/vimfiles/after/ftplugin/ (on Windows):

                --- ftplugin/perl.vim
                setlocal ts=4 sw=4 tw=78 et

                --- ftplugin/xml.vim
                setlocal ts=2 sw=2 tw=0 noet

                etc. (Create the files and/or directories if they don't exist yet.)

                It is important to use ":setlocal" rather than ":set", to avoid changing
                the setting on other files being edited in parallel.


                Best regards,
                Tony.
                --
                hundred-and-one symptoms of being an internet addict:
                171. You invent another person and chat with yourself in empty chat rooms.

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              Your message has been successfully submitted and would be delivered to recipients shortly.