Hooray for Hard Tabs!

chromatic on 2006-12-03T07:17:25

I understand and appreciate all of the arguments for using spaces instead of hard tabs in source code, but there's one drawback I just can't escape. A tab is a single character you can delete with one backspace or skip over with one left or right cursor keypress.

More people would complain if Enter produced carriage return plus newline (as it does now) but your editor treated both separately.

How do I make Vim respect soft tabs as a single entity?


Easy

Aristotle on 2006-12-03T08:05:55

:help 'backspace

Re:Easy

chromatic on 2006-12-03T08:26:21

I already have :set backspace=indent,eol,start. That's still not everything I want. Backspace does back up past autoindent, which is okay, but that's not the only thing I want. I want Vim to treat tabs as four spaces when it saves the file, but as single characters when I move around in or edit the file.

Did I read the help text incorrectly?

Re:Easy

Aristotle on 2006-12-03T09:15:47

Ah, OK. There’s no builtin way of doing that to my knowledge. I could swear I saw a way to do this with scripting in either the scripts or tips section of vim.org, but I can’t unearth it now.

# vim: set ts=8 sts=4 sw=4 noet:

mr_bean on 2006-12-03T09:39:01

There is some info at :h 'ts' that explains it.

Re:# vim: set ts=8 sts=4 sw=4 noet:

fishbot on 2006-12-03T16:25:02

As far as I can tell, the magic here is

'softtabstop' 'sts'

I have expandtab set, and shiftwidth, tabstop et al. are all set to four. Leaving all that constant, and setting sts=4, vim will now backspace over tabs. It won't treat tabs as single chars for x, but you can always I<BS> if you are in a leading tab-grouping and want to delete one tab. Of course, at that point you might as well <<

set smarttab

Smylers on 2006-12-03T18:18:02

set smarttab and then BkSpc will remove an entire indent.

Also note that in insert mode Ctrl+T will indent (“tab”) and Ctrl+D will delete an indent. You can press these with the cursor anywhere in a line and the indent will be tweaked at the start of the line.

Hard tabs?

DAxelrod on 2006-12-03T20:51:16

Yep, tabs are hard. :)

tab settings to live with

petdance on 2006-12-04T00:46:34

set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab

^D, &lt;&lt;, &gt;&gt; and such.

Abigail on 2006-12-04T09:04:06

I've been using vi and vi-clones for more than 20 years. And while my favourite vi-clone doesn't insert tabs (but spaces), I've seldomly the need to delete tabs. Neither in vi or in a vi-clone. As pointed out elsewhere, in insert mode, ^D removes an indent. And in command mode, < does so. And that's a command that takes a movement as argument, so doubling deletes an indent from the current line (<<), to delete indents from 7 lines I do 7<< and <} deletes indents from the current line to the next blank line.