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

increasing/reducing the number of leading white spaces dynamically

Expand Messages
  • Jose Caballero
    Hi, for our current python project, we are trying to follow the PEP recommendations, and therefore keeping all indentations to 4 white spaces. However, when I
    Message 1 of 8 , Nov 3, 2011
    • 0 Attachment
      Hi,


      for our current python project, we are trying to follow the PEP recommendations, and therefore keeping all indentations to 4 white spaces.
      However, when I am writing code, I feel more comfortable with 8 white spaces.
      I am pretty sure there must be a trick to put on .vimrc (or similar) in such a way the indents are expanded dynamically when I open a file, but they are reduced to 4 again when I leave ( ':q' or ':wq' )
      In this way the file on disk keeps having 4, but I see 8 when editing code.
      The only problem is that I don't know the keywords to search on google myself... If someone can provide for the keywords I can google it myself.


      Thanks a lot in advance.
      Jose

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Tim Chase
      ... My guess is that you want a pair of autocmds ... that trigger on BufWritePre and BufWritePost, something like (untested) autocmd BufWritePre *.py %s/^ (
      Message 2 of 8 , Nov 3, 2011
      • 0 Attachment
        On 11/03/11 10:36, Jose Caballero wrote:
        > for our current python project, we are trying to follow the PEP
        > recommendations, and therefore keeping all indentations to 4 white spaces.
        > However, when I am writing code, I feel more comfortable with 8 white
        > spaces.
        > I am pretty sure there must be a trick to put on .vimrc (or similar) in
        > such a way the indents are expanded dynamically when I open a file, but
        > they are reduced to 4 again when I leave ( ':q' or ':wq' )
        > In this way the file on disk keeps having 4, but I see 8 when editing code.
        > The only problem is that I don't know the keywords to search on google
        > myself... If someone can provide for the keywords I can google it myself.

        My guess is that you want a pair of autocmds

        :help :autocmd

        that trigger on BufWritePre and BufWritePost, something like
        (untested)

        autocmd BufWritePre *.py %s/^\(
        \{8}\)\+/\=substitute(submatch(0), repeat(' ', 8), repeat(' ',
        4), 'g')

        autocmd BufWritePost *.py %s/^\(
        \{4}\)\+/\=substitute(submatch(0), repeat(' ', 4), repeat(' ',
        8), 'g')

        The first one triggers before the file is written, searches for
        multiples of 8 spaces at the beginning of the line and replaces
        them with the corresponding number of 4-space instances. The
        second one triggers after you've written the file and internally
        restores the 8-space leading. The 2nd one might be simply
        replaced with

        autocmd BufWritePre *.py u

        which might undo the conversion to 4-space indentation a lot more
        cleanly. Test first :)

        For more info, read up at

        :help BufWritePre
        :h BufWritePost
        :h sub-replace-special
        :help :u

        -tim

        PS: it's interesting seeing it from your side where you want
        *more* leading whitespace. I tend to do my throw-away python
        with 2-space indentation, or with hard-tab characters that I then
        change 'ts'/'sw' to display in accordance with my mood.


        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Benjamin R. Haskell
        ... s/Pre/Post/ But, it seems to me it d be better to keep it explicit, so that it doesn t interact poorly with other BufWrite* autocmds that other plugins
        Message 3 of 8 , Nov 3, 2011
        • 0 Attachment
          On Thu, 3 Nov 2011, Tim Chase wrote:

          > My guess is that you want a pair of autocmds
          >
          > [...] like (untested)
          >
          > autocmd BufWritePre *.py %s/^\( \{8}\)\+/\=substitute(submatch(0), repeat(' ', 8), repeat(' ', 4), 'g')
          >
          > autocmd BufWritePost *.py %s/^\( \{4}\)\+/\=substitute(submatch(0), repeat(' ', 4), repeat(' ', 8), 'g')
          >
          > [...] The 2nd one might be simply replaced with
          >
          > autocmd BufWritePre *.py u

          s/Pre/Post/

          But, it seems to me it'd be better to keep it explicit, so that it
          doesn't interact poorly with other BufWrite* autocmds that other plugins
          might use.

          Also, you should add:

          /e

          to the end of each autocmd, for the unusual case that no lines in the
          .py file are indented. (If you just started a new file, for example.)
          Other than that, it seems to work for me on some simple files.

          --
          Best,
          Ben

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Tim Chase
          ... Doh, that s what I get for copy-n-pasting the Pre to the Post the first time and then pasting again for the u version. ... Ah, the products of actually
          Message 4 of 8 , Nov 3, 2011
          • 0 Attachment
            On 11/03/11 11:40, Benjamin R. Haskell wrote:
            > On Thu, 3 Nov 2011, Tim Chase wrote:
            >> [...] like (untested)
            >>
            >> autocmd BufWritePre *.py %s/^\( \{8}\)\+/\=substitute(submatch(0), repeat(' ', 8), repeat(' ', 4), 'g')
            >>
            >> autocmd BufWritePost *.py %s/^\( \{4}\)\+/\=substitute(submatch(0), repeat(' ', 4), repeat(' ', 8), 'g')
            >>
            >> [...] The 2nd one might be simply replaced with
            >>
            >> autocmd BufWritePre *.py u
            >
            > s/Pre/Post/

            Doh, that's what I get for copy-n-pasting the Pre to the Post the
            first time and then pasting again for the "u" version.

            > But, it seems to me it'd be better to keep it explicit, so that it
            > doesn't interact poorly with other BufWrite* autocmds that other plugins
            > might use.
            >
            > Also, you should add:
            >
            > /e
            >
            > to the end of each autocmd, for the unusual case that no lines in the
            > .py file are indented. (If you just started a new file, for example.)
            > Other than that, it seems to work for me on some simple files.

            Ah, the products of actually testing. So yes, Jose, incorporate
            Ben's suggestions.

            -tim


            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Andy Wokula
            ... Vim doesn t have read/write-filters (or similar), looks it isn t just a trick in the vimrc (nevertheless, you can put the following into your vimrc if
            Message 5 of 8 , Nov 3, 2011
            • 0 Attachment
              Am 03.11.2011 16:36, schrieb Jose Caballero:
              > Hi,
              >
              > for our current python project, we are trying to follow the PEP
              > recommendations, and therefore keeping all indentations to 4 white spaces.
              > However, when I am writing code, I feel more comfortable with 8 white
              > spaces.
              > I am pretty sure there must be a trick to put on .vimrc (or similar) in
              > such a way the indents are expanded dynamically when I open a file, but
              > they are reduced to 4 again when I leave ( ':q' or ':wq' )
              > In this way the file on disk keeps having 4, but I see 8 when editing code.
              > The only problem is that I don't know the keywords to search on google
              > myself... If someone can provide for the keywords I can google it myself.
              >
              >
              > Thanks a lot in advance.
              > Jose

              Vim doesn't have read/write-filters (or similar), looks it isn't just "a
              trick" in the vimrc (nevertheless, you can put the following into your
              vimrc if you want).

              After the other posts, this one tries to add a little more
              sophistication ...
              - keeps the cursor position
              - works with any 'ts', 'sw', 'sts' ... tabstop config
              - keeps 'modified' setting
              - tries to keep silent where appropriate
              - makes disabling easier with :augroup


              augroup PyIndentToggle
              au!
              au BufRead *.py call s:PyIndentToggle("read")
              au BufWritePre *.py call s:PyIndentToggle("write")
              au BufWritePost *.py call s:PyIndentToggle("undo")
              augroup End


              func! s:PyIndentToggle(situation)
              if a:situation == "undo"
              " only after "write"
              let sav_pos = winsaveview()
              sil! undo
              if !s:bufmod
              set nomodified
              endif
              call winrestview(sav_pos)
              return
              endif
              let sav_pos = winsaveview()
              let sav_inde = &inde
              try
              if a:situation == "read"
              let bufmod = &modified
              sil %call s:DoIndentLine(2 * indent("."))
              if !bufmod
              set nomodified
              endif
              elseif a:situation == "write"
              let s:bufmod = &modified
              sil %call s:DoIndentLine(indent(".") / 2)
              endif
              catch /:E21:/
              " cannot make changes, modifiable is off
              echoerr "PyIndentToggle: cannot change the indent, modifiable is off"
              finally
              let &l:inde = sav_inde
              call winrestview(sav_pos)
              endtry
              endfunc

              func! s:DoIndentLine(indent)
              exec "setlocal indentexpr=". a:indent
              normal! ==
              endfunc

              " beware of bugs, of course.

              --
              Andy

              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            • Andy Wokula
              Am 03.11.2011 18:25, schrieb Andy Wokula: Not a bug, but the s:bufmod check isn t necessary. ... *************** *** 11,19 **** only after write let
              Message 6 of 8 , Nov 3, 2011
              • 0 Attachment
                Am 03.11.2011 18:25, schrieb Andy Wokula:

                Not a bug, but the s:bufmod check isn't necessary.

                *** - Thu Nov 03 18:37:11 2011
                --- test\C865.vim Thu Nov 03 18:34:53 2011
                ***************
                *** 11,19 ****
                " only after "write"
                let sav_pos = winsaveview()
                sil! undo
                ! if !s:bufmod
                ! set nomodified
                ! endif
                call winrestview(sav_pos)
                return
                endif
                --- 22,28 ----
                " only after "write"
                let sav_pos = winsaveview()
                sil! undo
                ! set nomodified
                call winrestview(sav_pos)
                return
                endif
                ***************
                *** 27,33 ****
                set nomodified
                endif
                elseif a:situation == "write"
                - let s:bufmod = &modified
                sil %call s:DoIndentLine(indent(".") / 2)
                endif
                catch /:E21:/
                --- 36,41 ----


                --
                Andy

                --
                You received this message from the "vim_use" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php
              • Jose Caballero
                ... OK. The key thing is autocmd. Now I have something to start with. However, looking at these two things, I get confused. The first one makes sense. Just
                Message 7 of 8 , Nov 3, 2011
                • 0 Attachment
                  that trigger on BufWritePre and BufWritePost, something like (untested)

                   autocmd BufWritePre *.py %s/^\( \{8}\)\+/\=substitute(submatch(0), repeat(' ', 8), repeat(' ', 4), 'g')

                   autocmd BufWritePost *.py %s/^\( \{4}\)\+/\=substitute(submatch(0), repeat(' ', 4), repeat(' ', 8), 'g')



                  OK. The key thing is autocmd. Now I have something to start with.

                  However, looking at these two things, I get confused.
                  The first one makes sense. Just before "saving" the buffer on disk it changes 8 spaces by 4. Good.
                  But the second one ... shouldn't it be something like BufRead<whatever> in order to expand 4 spaces to 8 when I start editing the file?

                  --
                  You received this message from the "vim_use" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php
                • Tim Chase
                  ... You might want one of those too, using the BufRead event. The above two just do 1) change to 4-space 2) write to disk 3) change back to 8-space However,
                  Message 8 of 8 , Nov 3, 2011
                  • 0 Attachment
                    On 11/03/11 15:20, Jose Caballero wrote:
                    >> that trigger on BufWritePre and BufWritePost, something like (untested)
                    >>
                    >> autocmd BufWritePre *.py %s/^\( \{8}\)\+/\=substitute(**submatch(0),
                    >> repeat(' ', 8), repeat(' ', 4), 'g')
                    >>
                    >> autocmd BufWritePost *.py %s/^\( \{4}\)\+/\=substitute(submatch(0),
                    >> repeat(' ', 4), repeat(' ', 8), 'g')
                    >
                    > OK. The key thing is autocmd. Now I have something to start with.
                    >
                    > However, looking at these two things, I get confused.
                    > The first one makes sense. Just before "saving" the buffer on disk it
                    > changes 8 spaces by 4. Good.
                    > But the second one ... shouldn't it be something like BufRead<whatever> in
                    > order to expand 4 spaces to 8 when I start editing the file?

                    You might want one of those too, using the BufRead event. The
                    above two just do

                    1) change to 4-space
                    2) write to disk
                    3) change back to 8-space

                    However, you might also want to do the same as the BufWritePost
                    for the BufRead event so that when you start-up with the file, it
                    converts it into your preferred format.

                    -tim


                    --
                    You received this message from the "vim_use" maillist.
                    Do not top-post! Type your reply below the text you are replying to.
                    For more information, visit http://www.vim.org/maillist.php
                  Your message has been successfully submitted and would be delivered to recipients shortly.