Tab behaviour

7 views
Skip to first unread message

jerous

unread,
Jun 12, 2008, 6:45:23 PM6/12/08
to vim_use
Hey,

I wonder if there is any setting where I can tune following tab
behaviour. Imagine, this piece of code (I use tabs, instead of spaces
in my source):
<code>
def foo():
search()
</code>
and the cursor is right after search().
I press Enter once, the line continues on the same indentation as the
search() line. But if I now do another Enter or move to another line,
the indentation on the line below search() has been reset.

Does anyone know how to change it to the - in my opinion - normal
situation that the indentation is kept, even after moving to another
line?

Thanks,
jerous

Gary Johnson

unread,
Jun 12, 2008, 7:02:42 PM6/12/08
to vim...@googlegroups.com

If you use filetype detection and let the file type set the
indentation behavior, e.g., by putting this in your vimrc file:

filetype plugin indent on

then the $VIMRUNTIME/indent/python.vim plugin will be loaded
automatically and should give you that kind of indentation behavior.

HTH,
Gary

jerous

unread,
Jun 12, 2008, 7:31:03 PM6/12/08
to vim_use
Thank you for your fast reply, Gary.

Unfortunately, this doesn't seem to fix the behaviour.
I removed my ~/.vim/ folder, stored the line "filetype plugin indent
on" in my ~/.vimrc and confirmed that the file $VIMRUNTIME/indent/
python.vim exists, but it still keeps forgetting previous indents ...

Are there some other options I can try?

Thank you,
jerous

ps: I'm using vim 7.1 on SuSE10.2, if it would matter.

Stahlman Family

unread,
Jun 12, 2008, 7:59:39 PM6/12/08
to vim...@googlegroups.com

jerous wrote:
>
>
> On 13 jun, 01:02, Gary Johnson <garyj...@spk.agilent.com> wrote:
>> On 2008-06-12, jerous <jer...@gmail.com> wrote:
>>
>>
>>
>>
>>
>>> Hey,
>>> I wonder if there is any setting where I can tune following tab
>>> behaviour. Imagine, this piece of code (I use tabs, instead of spaces
>>> in my source):
>>> <code>
>>> def foo():
>>> search()
>>> </code>
>>> and the cursor is right after search().
>>> I press Enter once, the line continues on the same indentation as the
>>> search() line. But if I now do another Enter or move to another line,
>>> the indentation on the line below search() has been reset.
>>> Does anyone know how to change it to the - in my opinion - normal
>>> situation that the indentation is kept, even after moving to another
>>> line?

Jerous,
When you hit <Enter> on the line containing search(), the python indent
plugin uses 'autoindent' to preserve the indentation of the preceding
line. Note the following from the documentation on 'autoindent':

"Copy indent from current line when starting a new line (typing <CR>
in Insert mode or when using the "o" or "O" command). If you do not
type anything on the new line except <BS> or CTRL-D and then type
<Esc>, CTRL-O or <CR>, the indent is deleted again. Moving the cursor
to another line has the same effect, unless the 'I' flag is included
in 'cpoptions'."

You could add the 'I' flag to 'cpoptions' to prevent the indent from
being deleted when you move the cursor to another line, but I'm not sure
there's much you can do to fix the <CR> case, short of using a python
indent plugin that doesn't use 'autoindent' to achieve indentation...

Brett Stahlman

Gary Johnson

unread,
Jun 12, 2008, 8:16:17 PM6/12/08
to vim...@googlegroups.com

To make sure we're on the same page, here's what I did. I started
vim as

vim -N -u NONE -i NONE

to make sure neither my ~/.vimrc nor any plugins were loaded. Then
I executed

:so $VIMRUNTIME/indent/python.vim

Then I typed

a

to start adding text, typed

def foo():

and when I hit Enter, the cursor moved to the next line with an
indent of one tab stop, stopping right below the ')'. Then I typed

search()

and hit Enter again. The cursor moved to the next line, right below
the 's'. Hitting Enter again leaves an empty line, but the cursor
is still in column 9. I can hit Enter as many times as I want and
the cursor remains in column 9. If I hit Esc, the cursor moves to
column 1.

So in that scenario, vim has remembered the previous indent.

Now, to add more lines below "search()", moving the cursor to the
"search()" line and typing 'o', or typing 'A' followed by Enter,
will start a new line with the same indent as the "search()" line.
Any other method of starting a new line will start the line at
column 1.

Is this what you see?

I'm using vim 7.1.297 with plugins updated on May 12, and
indent/python.vim dated 2006 Jun 18.

Regards,
Gary

Brett Stahlman

unread,
Jun 13, 2008, 9:59:15 AM6/13/08
to vim_use
Gary,
If I understand correctly, it's the "empty line" that's troubling him.
The indentation of the subsequent line is correct. As I mentioned in
my preceding post, this is a result of the way the distributed python
indent plugin uses the autoindent mechanism to achieve the indent...

Brett Stahlman

> the cursor remains in column 9.  If I hit Esc, the cursor moves to
> column 1.
>
> So in that scenario, vim has remembered the previous indent.
>
> Now, to add more lines below "search()", moving the cursor to the
> "search()" line and typing 'o', or typing 'A' followed by Enter,
> will start a new line with the same indent as the "search()" line.  
> Any other method of starting a new line will start the line at
> column 1.
>
> Is this what you see?
>
> I'm using vim 7.1.297 with plugins updated on May 12, and
> indent/python.vim dated 2006 Jun 18.
>
> Regards,
> Gary- Hide quoted text -
>
> - Show quoted text -

jerous

unread,
Jun 13, 2008, 10:09:24 AM6/13/08
to vim_use

> Jerous,
> When you hit <Enter> on the line containing search(), the python indent
> plugin uses 'autoindent' to preserve the indentation of the preceding
> line. Note the following from the documentation on 'autoindent':
>
> "Copy indent from current line when starting a new line (typing <CR>
> in Insert mode or when using the "o" or "O" command).  If you do not
> type anything on the new line except <BS> or CTRL-D and then type
> <Esc>, CTRL-O or <CR>, the indent is deleted again.  Moving the cursor
> to another line has the same effect, unless the 'I' flag is included
> in 'cpoptions'."
>
> You could add the 'I' flag to 'cpoptions' to prevent the indent from
> being deleted when you move the cursor to another line, but I'm not sure
> there's much you can do to fix the <CR> case, short of using a python
> indent plugin that doesn't use 'autoindent' to achieve indentation...
>
> Brett Stahlman

Pff, I guess I have to try to get used to it then ...
vim disappoints me on this one :p
I get the same behaviour, but it forgets the indentation of the line
before the previous lines.
I think it is clearly visible when you
:set list
and then perform the pressing of Enter again.
Then you can see the indentation being removed as you add new lines by
pressing Enter several times.


Thank you for the answers,
jerous

Ben Schmidt

unread,
Jun 13, 2008, 12:04:33 PM6/13/08
to vim...@googlegroups.com
jerous wrote:
>
>> Jerous,
>> When you hit <Enter> on the line containing search(), the python indent
>> plugin uses 'autoindent' to preserve the indentation of the preceding
>> line. Note the following from the documentation on 'autoindent':
>>
>> "Copy indent from current line when starting a new line (typing <CR>
>> in Insert mode or when using the "o" or "O" command). If you do not
>> type anything on the new line except <BS> or CTRL-D and then type
>> <Esc>, CTRL-O or <CR>, the indent is deleted again. Moving the cursor
>> to another line has the same effect, unless the 'I' flag is included
>> in 'cpoptions'."
>>
>> You could add the 'I' flag to 'cpoptions' to prevent the indent from
>> being deleted when you move the cursor to another line, but I'm not sure
>> there's much you can do to fix the <CR> case, short of using a python
>> indent plugin that doesn't use 'autoindent' to achieve indentation...
>>
>> Brett Stahlman
>
> Pff, I guess I have to try to get used to it then ...
> vim disappoints me on this one :p

I'm not sure what you're wanting here.

If you're wanting lines in your files with indent but nothing else,
which is unusual, IMHO, how about just doing

:inoremap <CR> x<BS><CR>

? Vim doesn't delete the indent if you type something, even if you
delete that something, and a mapping can ensure you type something even
if you don't!

If you're just wanting the indent to be at the 'right' level if you have
blank lines, e.g.

if whatever:
do something

do another thing

and you place the cursor on the blank line and press o, you want there to
be indent, then why not achieve this by wrapping the indentexpr in
another that makes it essentially ignore blank lines? I think this will
do it, but I haven't fully tested it:

function! IndentIgnoringBlanks(child)
while v:lnum > 1 && getline(v:lnum-1) == ""
normal k
let v:lnum = v:lnum - 1
endwhile
if a:child == ""
if v:lnum <= 1 || ! &autoindent
return 0
elseif &cindent
return cindent(v:lnum)
else
return indent(v:lnum-1)
endif
else
exec "let indent=".a:child
return indent==-1?indent(v:lnum-1):indent
endif
endfunction
augroup IndentIgnoringBlanks
au!
au FileType * if match(&indentexpr,'IndentIgnoringBlanks') == -1 |
\ let &indentexpr = "IndentIgnoringBlanks('".
\ substitute(&indentexpr,"'","''","g")."')" |
\ endif
augroup END

You can change the * to make it apply to only the filetypes you want, or
put the command explicitly in after scripts (:help after-directory).

This is quite handy. I reckon I might use it myself! Might even make a
tip out of it if nobody else has already done it (you can tell I didn't
look, can't you?)...

I think this is more standard. I don't think it's good/common to have
lines that contain only whitespace. Much better to have completely empty
lines, but get the indent right nonetheless.

Ben.

jerous

unread,
Jun 14, 2008, 8:37:47 PM6/14/08
to vim_use
The "inoremap" trick is perfect!

I prefer it this way, because I often tend to insert some empty new
lines, and then start typing on one of those lines. If the indentation
then is missing, I have to reindent, and I'm pretty lazy ;p

Thanks a lot!
jerous
Reply all
Reply to author
Forward
0 new messages