Smart expandtab at beginning of line vs in mid-line?

95 views
Skip to first unread message

Michael Ludwig

unread,
Feb 28, 2010, 9:13:04 AM2/28/10
to vim...@googlegroups.com
For indenting at the beginning of the line, I want tabs.
But in mid-line, I want them to be expanded to spaces.
Can this be achieved in Vim?

bla.callSomeMethod( eins, zwei ); // jau
bla.callAnotherMethod( eins, zwei, drei ); // jaujau

The reason for this is that while it all looks fine given my
personal tabstop setting of 4, and possibly others for this
particular piece of text; but in general the end-of-line
comments will not look justified for each and every setting
of tabstop. Spaces are more robust here.

Is there a setting in Vim to expand tabs depending on whether
I'm at the beginning of a line (indenting), or in the middle
of the line (formatting)?

--
Michael Ludwig

John Beckett

unread,
Feb 28, 2010, 7:25:35 PM2/28/10
to vim...@googlegroups.com
Michael Ludwig wrote:
> For indenting at the beginning of the line, I want tabs.
> But in mid-line, I want them to be expanded to spaces.

The first has general principles; the second is on your point:
http://vim.wikia.com/wiki/Indenting_source_code
http://vim.wikia.com/wiki/Indent_with_tabs,_align_with_spaces

John

Michael Ludwig

unread,
Feb 28, 2010, 9:49:35 PM2/28/10
to vim...@googlegroups.com

Thanks! I had read the first but not the second. Excellent
documentation!

Smart Tabs (ctab.vim) 1.4.1 does exactly what I want.

The newer version 2.1, however, seems to be less robust. I got
the following error messages right after hitting "o" to insert
lines:

=<SNR>12_CheckAlign(line('.'))."\<END>"
Error detected while processing function <SNR>12_CheckAlign:
line 25:
E121: Undefined variable: inda
Press ENTER or type command to continue
Error detected while processing function <SNR>12_CheckAlign:
line 25:
E15: Invalid expression: inda / 50
Press ENTER or type command to continue
Error detected while processing function <SNR>12_CheckAlign:
line 26:
E121: Undefined variable: inda
Press ENTER or type command to continue
Error detected while processing function <SNR>12_CheckAlign:
line 26:
E15: Invalid expression: inda % 50
Press ENTER or type command to continue
Error detected while processing function <SNR>12_CheckAlign:
line 28:
E121: Undefined variable: indatabs
Press ENTER or type command to continue
Error detected while processing function <SNR>12_CheckAlign:
line 28:
E15: Invalid expression: indatabs*&tabstop + indaspace == indb
Press ENTER or type command to continue

Posting this only for informative purposes - so far, I'm happy
with version 1.4.1.

Thanks again!
--
Michael Ludwig

Andy Wokula

unread,
Mar 2, 2010, 3:11:45 PM3/2/10
to vim...@googlegroups.com

Here is a thread from Oct 2007:

Subject: expandtab after non-blanks only
http://groups.google.com/group/vim_use/browse_thread/thread/eb86a0cb32690a14

My old suggestion temporarily sets expandtab before the Tab is inserted
and resets it afterwards (btw: maybe it should use :setlocal instead of
:set).

The alternatives (mainly: scripts) manually calculate the amount of
spaces to be inserted. The following code does it the same way:


func! TabOrSpaces()
if !&expandtab && virtcol(".")-1 > indent(".")
" actual tabstop
let ats = &sts>0 ? &sts : &ts
return repeat(" ", ats - (virtcol(".")-1) % ats)
else
return "\<Tab>"
endif
endfunc

ino <silent> <Tab> <C-R>=TabOrSpaces()<CR>


... and is short enough to be put in the vimrc.


In the old thread I linked to

Converting tabs to spaces http://vim.wikia.com/wiki/VimTip12
which in turn mentions

Clever Tabs : Using tabs for indent only on start of line
http://www.vim.org/scripts/script.php?script_id=2308

(I just wanted to mention it).


What's left -- some backdraws of the above code:
- <Tab> no longer expands abbreviations
- <BS> (after non-blanks) only deletes one Space at a time
- probably conflicts with the SuperTab plugin (although I don't use it)

--
Andy

Reply all
Reply to author
Forward
0 new messages