Delete space-expanded "tab" in Vim with one keystroke?

19

6

I edit Python code with Vim.

With the tab key I get four spaces inserted for indentation. How can I delete those spaces with one "Backspace"(?) stroke - instead of four?

There has to be some "set" option for that...

Here is my .vimrc:

set autoindent
set ts=4
set sw=4
set et

Evgeny

Posted 2009-07-16T03:12:49.047

Reputation: 397

Answers

24

You can use Ctrl+D to back up one tab stop. This actually moves the whole line to the left one tab stop; Ctrl+T does the same thing to the right. Note that these keystrokes only work in Insert mode (use << and >> for the equivalent in Command mode).

Greg Hewgill

Posted 2009-07-16T03:12:49.047

Reputation: 5 099

30

set softtabstop=4

sf17k

Posted 2009-07-16T03:12:49.047

Reputation: 301

1Did not know about this, and this was exactly what I was looking for. +1 – NigoroJr – 2015-05-12T19:46:16.463

10IMO this is exactly what the OP asked for. Deserves more upvotes. – Ludwig Weinzierl – 2012-01-09T07:06:45.870

2This is the correct answer. – mk12 – 2012-07-30T18:09:02.843

5

tl;dr: set tabstop=4 softtabstop=-1 shiftwidth=0 expandtab

short form: set ts=4 sts=-1 sw=0 et

Explanation

If you set softtabstop (or sts) to -1 it will automatically behave the same as tabstop (ts), which will save you some hassle if you change tabbing a lot. Setting shiftwidth (sw) to 0 should effectively make that the same as tabstop as well.

In Detail

shiftwidth sw

Number of spaces to use for each step of (auto)indent. Used for cindent, >>, <<, etc.
When zero the tabstop value will be used.

tabstop ts

Number of spaces that a in the file counts for. Also see :retab command, and softtabstop option.

softtabstop sts

Number of spaces that a Tab counts for while performing editing operations, like inserting a Tab or using BS. It feels like Tabs are being inserted, while in fact a mix of spaces and s are used. This is useful to keep the tabs is setting at its standard value, while being able to edit like it is set to sts. When sts is negative, the value of shiftwidth is used. This will save you some hassle if you change tabstops a lot. When expandtab is not set, the number of spaces is minimized by using Tabs.

expandtab et

In Insert mode: Use the appropriate number of spaces to insert a . Spaces are used in indents with the > and < commands and when autoindent is on. To insert a real tab when expandtab is on, use Ctrl-V Tab. See also :retab

Orwellophile

Posted 2009-07-16T03:12:49.047

Reputation: 451

Neat, I've been using vim for years but the inheritance of sts/sw/ts never caught my eye. – timss – 2017-08-30T15:00:42.460

2

I wrote GreedyBackspace.vim for someone who wanted something like this. I don't personally use it so it's been a while since I've updated it. I haven't received any bug reports on it lately, though.

Heptite

Posted 2009-07-16T03:12:49.047

Reputation: 16 267

1

Several options:

  1. You could 'retab' all whitespaces so they're replaced with tabs. That way they could be easily deleted.
  2. You could (this is untested, so I'm just proposing ideas here) make a function which 'execute's in normal mode 4 times the backspace key. Then map it to something. However, it has no way of knowing whether it will delete spaces or some important characters, so this is potentially unsafe.
  3. '4X' will delete the last 4 characters (that's big X, not small, small one will act like the Del key'.

Rook

Posted 2009-07-16T03:12:49.047

Reputation: 21 622

0

easiest way is <<, repeat with a .

vimmer

Posted 2009-07-16T03:12:49.047

Reputation: 11

Hello and welcome to superuser. Although i agree with your answer being a vi user myself. Your answer is short, does not explain why you suggest what you are suggesting and why you are not suggesting something that fits the question asked. – Mogget – 2016-04-03T21:50:45.330

-1

Have you checked using the expand tabs setting in your VIM?

:set noet

I usually prefer keeping the tabs on while working on the files.
When it is required, I replace the tabs to 4 spaces or as many as required.

:%s/<ctrl+V><tab>/    /g
                  ----

As a small bonus, your source file is shorter by 3 chars per tab :-)

nik

Posted 2009-07-16T03:12:49.047

Reputation: 50 788

Maybe you could have a vim function that expanded them to tabs on open and collapsed them to spaces on closing? Anyone have any ideas? – sixtyfootersdude – 2010-02-23T16:33:09.227

This is a really unhelpful answer--it needlessly reopens the tabs-vs-spaces debate, without actually providing any useful information. – Dietrich Epp – 2015-11-24T19:01:17.160

oh, I have et because I have to expand tabs - i think - because code I edit was initially written that way. now I'm just following that convention. – Evgeny – 2009-07-16T03:20:17.080

You could still do what i say and restore spaces at the end (when you have to give the file back to the repository) – nik – 2009-07-16T03:24:29.130

-2

I don't think there's a way. Once the tab key is expanded to four spaces, vim has no way of knowing they were ever a tab.

David Mackintosh

Posted 2009-07-16T03:12:49.047

Reputation: 3 728

4-1: Lack of imagination. – sixtyfootersdude – 2010-02-23T16:31:47.327