2

Running vim with the following settings:

expandtab
tabstop=4
softtabstop=4
shiftwidth=4

And no other user plugins.

In insert mode...

If I enter:

a<Tab>b<Tab>c

all tabs are replaced by 4 spaces as expected.

BUT, if I enter:

a<space>b<Tab>c

The <Tab> is only replaced by a single space.

What's causing this seeming 'context-based' replacement of <Tab>? How can I debug this sort of thing (I tried vim -V15 vim.log, but nothing obvious showed up in the log)?

9

This is how tabs work. Pressing the Tab doesn't mean "insert n spaces", but rather "go to the next tabstop".

Every line is divided in "cells" of n spaces, for example four:

---->--->--->--->--->--->--->---

Pressing the Tab key will make it go to the start of the next cell (indicated by the >). In your example there is just one space left to the next tabstop, so only one space is inserted.

The reason it works like this is because the tab key is intended to align data. Consider text like:

Quote               | Author          |
--------------------+-----------------|
Hello, world        | Brian Kernighan |
It's an Aardvark!   | Black Adder     |

As you can see the two columns are neatly aligned. One way to achieve this is to repeatedly mash the space key until it lines up, but just pressing the tab key once or twice to align it is much easier.

You've chosen to expand tabs with spaces, but that won't change the actual behaviour of how the tab key works; it will still work the same, but instead of inserting a tab character (0x09) it will insert the correct number of spaces. As far as I know, it works like this in all editors and IDEs (at least those that I've used).

If you want Vim to always just insert four spaces you can use:

:inoremap <Tab> <Space><Space><Space><Space>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.