Re: Folding in vim

Discussion in 'Python' started by Terry Hancock, Jul 4, 2005.

  1. On Saturday 02 July 2005 10:35 pm, Terry Hancock wrote:
    > I tried to load a couple of different scripts to
    > automatically fold Python code in vim, but none of them
    > seems to do a good job.
    >
    > I've tried:
    > python_fold.vim by Jorrit Wiersma
    > http://www.vim.org/scripts/script.php?script_id=515


    Actually, I think this one is doing what I want now. It seems
    to be that it isn't robust against files with lots of mixed tabs
    and spaces. I also got "space_hi.vim" which highlights tabs
    and trailing spaces, which made it a lot easier to fix the
    problem.

    After fixing my source files, python_fold seems to be able
    to handle them just fine.

    I must also recommend C. Herzog's python_box.vim
    which is fantastic -- especially the automatic Table of
    Contents generation for Python source, and pydoc.vim
    which puts access to pydoc into the editor.

    Nice. Now that I have a "very sharp saw", I'm going to
    have to go cut some stuff for a bit. ;-)

    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Jul 4, 2005
    #1
    1. Advertising

  2. Terry Hancock

    Ron Adam Guest

    Terry Hancock wrote:

    > On Saturday 02 July 2005 10:35 pm, Terry Hancock wrote:
    >
    >>I tried to load a couple of different scripts to
    >>automatically fold Python code in vim, but none of them
    >>seems to do a good job.
    >>
    >>I've tried:
    >>python_fold.vim by Jorrit Wiersma
    >> http://www.vim.org/scripts/script.php?script_id=515

    >
    >
    > Actually, I think this one is doing what I want now. It seems
    > to be that it isn't robust against files with lots of mixed tabs
    > and spaces. I also got "space_hi.vim" which highlights tabs
    > and trailing spaces, which made it a lot easier to fix the
    > problem.


    I edited my syntax coloring file to do the same thing. Not to mention
    adding a few key words that were missing. :)

    > After fixing my source files, python_fold seems to be able
    > to handle them just fine.
    >
    > I must also recommend C. Herzog's python_box.vim
    > which is fantastic -- especially the automatic Table of
    > Contents generation for Python source, and pydoc.vim
    > which puts access to pydoc into the editor.


    Sounds good. I'll give it a try! :)


    > Nice. Now that I have a "very sharp saw", I'm going to
    > have to go cut some stuff for a bit. ;-)
    >
    > --
    > Terry Hancock ( hancock at anansispaceworks.com )
    > Anansi Spaceworks http://www.anansispaceworks.com
    >
     
    Ron Adam, Jul 4, 2005
    #2
    1. Advertising

  3. On Sun, 3 Jul 2005 22:42:17 -0500, Terry Hancock
    <> wrote:

    >It seems to be that it isn't robust against files
    >with lots of mixed tabs and spaces.


    My suggestion is:

    - never ever use tabs; tabs were nice when they had
    a de-facto meaning (tabbing to next 8-space boundary)
    nowdays they're just noise as the meaning depends
    on the phase of the moon.
    Making tabs meaning anything had the pretty obvious
    implication of making tabs meaning nothing.

    - stick to 4-space indent

    I've even run in the past in editors that damaged
    my python sources because they were indented with
    two spaces (I'm used to an indent size of 2 when
    working in C/C++). With python IMO 4 spaces is
    perfectly adequate anyway; onced I tried it I never
    had the temptation of looking back.

    Andrea
     
    Andrea Griffini, Jul 4, 2005
    #3
  4. On Monday 04 July 2005 12:41 am, Ron Adam wrote:
    > > Actually, I think this one is doing what I want now. It seems
    > > to be that it isn't robust against files with lots of mixed tabs
    > > and spaces. I also got "space_hi.vim" which highlights tabs
    > > and trailing spaces, which made it a lot easier to fix the
    > > problem.

    >
    > I edited my syntax coloring file to do the same thing. Not to mention
    > adding a few key words that were missing. :)


    This script:
    http://www.vim.org/scripts/script.php?script_id=790
    "python.vim" (but in ~/.vim/syntax) by Dmitry Vasiliev

    adds a few dozen Python-specific syntax keywords, I used these
    and edited my usual color scheme to give them more refined
    syntax coloring, which is nice. It is quite useful to have Unicode
    strings and Raw strings display differently, for example, since
    it's easy to forget which you're using (unless you're right next
    to the mark).

    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Jul 5, 2005
    #4
  5. On Monday 04 July 2005 01:12 am, Andrea Griffini wrote:
    > - never ever use tabs; tabs were nice when they had
    > - stick to 4-space indent


    Nice ideals to which I ascribe. But if your editor isn't
    configured to support you on this, spacing over to, say
    column 24 gets pretty dull. Mine wasn't configured to
    do anything special, so I was leaving tabs in the file
    and etc.

    So I wasn't really living up to my ideals. ;-)

    Bad habits, I agree. I now have things working so that
    "tab" actually uses spaces in the file. The problems
    arose because I was still working with my old "tainted"
    files.

    All better now. :)

    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Jul 5, 2005
    #5
  6. Andrea Griffini enlightened us with:
    > - never ever use tabs


    I always use one tab for indents, and set my editor to display it as
    four spaces. I like being able to unindent a line by deleting a single
    character. I don't see a reason why _not_ to use tabs, really. As long
    as the use is consistent - I hate those files with an 8-space tab and
    4-space indent, where they replace two indents with a single tab. For
    me, one indent = one tab. No matter the tab setting, it'll look good.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jul 5, 2005
    #6
  7. Terry Hancock

    Mike Meyer Guest

    Sybren Stuvel <> writes:

    > Andrea Griffini enlightened us with:
    >> - never ever use tabs

    >
    > I always use one tab for indents, and set my editor to display it as
    > four spaces. I like being able to unindent a line by deleting a single
    > character. I don't see a reason why _not_ to use tabs, really. As long
    > as the use is consistent - I hate those files with an 8-space tab and
    > 4-space indent, where they replace two indents with a single tab. For
    > me, one indent = one tab. No matter the tab setting, it'll look good.


    And so if you're working with someone who uses a different tab stop -
    say the traditional 8-space tabs - they'll get code that may well be
    unreadable.

    If you're the only one dealing with your code, you can keep the use
    consistent. As soon as theirs more than one person editing the code,
    that pretty much goes out the window. You either mandate a standard -
    which is hard for a large group - or you mandate that you don't use
    tabs.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jul 5, 2005
    #7
  8. Terry Hancock

    Benji York Guest

    Sybren Stuvel wrote:
    > I always use one tab for indents, and set my editor to display it as
    > four spaces. I like being able to unindent a line by deleting a single
    > character.


    Your editor probably supports a "backspace unindents" option. If using
    Vim it would be something like "set softtabstop=4".
    --
    Benji York
     
    Benji York, Jul 5, 2005
    #8
  9. Benji York enlightened us with:
    > Your editor probably supports a "backspace unindents" option.


    Yes, it does. I'm using vim.

    > If using Vim it would be something like "set softtabstop=4".


    This gives you a mixture of tabs and spaces, which I don't like. I'd
    rather use real tabs for indenting. If you then use another tab width,
    you only see a wider indent, but the rest of the code is okay.

    When using a tab/space mixture, with eight spaces being one tab, and
    an indent of four spaces, things go wrong when the tab size is
    anything but eight spaces.

    My solution works for all tab sizes, the other solution only works for
    tabs of eight spaces. This is why in my opinion it's better to just
    use tabs.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jul 5, 2005
    #9
  10. Sybren Stuvel ha scritto:
    >>If using Vim it would be something like "set softtabstop=4".

    >
    >
    > This gives you a mixture of tabs and spaces, which I don't like. I'd
    > rather use real tabs for indenting. If you then use another tab width,
    > you only see a wider indent, but the rest of the code is okay.



    Why not use just spaces? Vim simplifies this immensely:

    set tabstop=4
    set shiftwidth=4
    set expandtab
    set smarttab
    set autoindent

    AFAICT this gives me all spaces, 4 spaces indent, tab inserts spaces and
    backspace over a block of 4 spaces deletes all of them (just like
    deleting a tab).


    --
    Renato
    --------------------------------
    Usi Fedora? Fai un salto da noi:
    http://www.fedoraitalia.org
     
    Renato Ramonda, Jul 5, 2005
    #10
  11. On Tuesday 05 July 2005 03:53 pm, Renato Ramonda wrote:
    > Why not use just spaces? Vim simplifies this immensely:
    >
    > set tabstop=4
    > set shiftwidth=4
    > set expandtab
    > set smarttab
    > set autoindent
    >
    > AFAICT this gives me all spaces, 4 spaces indent, tab inserts spaces and
    > backspace over a block of 4 spaces deletes all of them (just like
    > deleting a tab).


    Yep, this is what I just set up in my .vimrc. Works beautifully.

    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Jul 6, 2005
    #11
  12. Terry Hancock ha scritto:

    >
    > Yep, this is what I just set up in my .vimrc. Works beautifully.


    And (you probably already know, but it could be of use to others) you
    can bind the activation of some or all of those commands to au
    (autocommand) depending on the file extension.

    That way you can have 8 spaces real tabs in C files, for examples,
    without touching your conf.

    --
    Renato
    --------------------------------
    Usi Fedora? Fai un salto da noi:
    http://www.fedoraitalia.org
     
    Renato Ramonda, Jul 6, 2005
    #12
  13. Terry Hancock

    Bill Mill Guest

    On 7/6/05, Terry Hancock <> wrote:
    > On Tuesday 05 July 2005 03:53 pm, Renato Ramonda wrote:
    > > Why not use just spaces? Vim simplifies this immensely:
    > >
    > > set tabstop=4
    > > set shiftwidth=4
    > > set expandtab
    > > set smarttab
    > > set autoindent
    > >
    > > AFAICT this gives me all spaces, 4 spaces indent, tab inserts spaces and
    > > backspace over a block of 4 spaces deletes all of them (just like
    > > deleting a tab).

    >
    > Yep, this is what I just set up in my .vimrc. Works beautifully.
    >


    I don't use any of the fancy indenters; instead, I just add

    set foldmethod=indent

    to my .vimrc (_vimrc on windows), along with most of the
    aforementioned options (I don't like smarttab); it works nearly
    perfectly. Then zo opens the fold under the cursor one level, zO opens
    it recursively, zc and zC close it non- and recursively. zr opens all
    folds one level, zR opens them all recursively, zm closes them all one
    level, and zM closes them all recursively.

    It's pretty sweet. Maybe we should have a big Vim-python tip-a-thon thread?

    Peace
    Bill Mill
     
    Bill Mill, Jul 6, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tocer
    Replies:
    0
    Views:
    493
    tocer
    Aug 28, 2004
  2. Terry Hancock

    Folding in vim

    Terry Hancock, Jul 3, 2005, in forum: Python
    Replies:
    1
    Views:
    762
    Ron Adam
    Jul 3, 2005
  3. Ara.T.Howard

    rdoc and vim folding

    Ara.T.Howard, Nov 8, 2004, in forum: Ruby
    Replies:
    22
    Views:
    373
    Kaspar Schiess
    Nov 10, 2004
  4. Kaspar Schiess

    Bug?: vim folding for Ruby

    Kaspar Schiess, Nov 15, 2004, in forum: Ruby
    Replies:
    1
    Views:
    146
    Doug Kearns
    Nov 15, 2004
  5. Ara.T.Howard

    ruby/vim folding question

    Ara.T.Howard, Nov 22, 2005, in forum: Ruby
    Replies:
    8
    Views:
    280
    tony summerfelt
    Nov 22, 2005
Loading...

Share This Page