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 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
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
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.