5

I am working in a git repository that has all of its indenting as tabs, but I like to work in spaces (4 spaces per indent). I don't want to just do a text replace of the tabs because then I'll end up having a horrible mess in my diffs. Instead, I want vim to make tabs appear as if they're spaces.

I created this question after reading this one:

Redefine tab as 4 spaces

One the answers (from Alan Haggai Alavi) says the following:

set tabstop=4       " The width of a TAB is set to 4.
                    " Still it is a \t. It is just that
                    " Vim will interpret it to be having
                    " a width of 4.

set shiftwidth=4    " Indents will have a width of 4

set softtabstop=4   " Sets the number of columns for a TAB

set expandtab       " Expand TABs to spaces

This seems to suggest that running :set expandtab will make tabs appear as spaces. Apparently that's not the case. How can I achieve what I'm after? I'm using vim 7.4.

Community
  • 1
  • 1
quant
  • 21,507
  • 32
  • 115
  • 211
  • Try: `set noexpandtab` – Cole Tierney Aug 02 '14 at 13:55
  • 1
    `set expandtab` causes tabs *that you enter* to be expanded to spaces; it doesn't affect existing tabs. To expand existing tabs, `:%!expand -t 4` – Keith Thompson Aug 02 '14 at 17:44
  • What do you mean by having tabs "appear as spaces"? It's all whitespace, they already appear the same as spaces. Unless you have the "list" option set? – Ben Aug 02 '14 at 23:47

4 Answers4

1

From command mode, just invoke

:retab

This will convert existing tabs to spaces (given you have :set expandtab, which you already have in your .vimrc). Plus, since you already have set tabstop value to 4 spaces, :retab will use that value and replace existing tabs to 4 spaces.

For more information check the inbuilt help

:help retab

And if you want do more nifty things, check out this link : http://vim.wikia.com/wiki/Super_retab

enabling expandtab does not convert existing tabs to spaces, only new insertion of TAB characters are expanded.

xk0der
  • 3,660
  • 4
  • 26
  • 35
  • This will change my whole document though right? I was hoping for a solution that would leave the document intact. – quant Aug 03 '14 at 07:28
  • Yes, exactly! And now I get it that you just want Vim to 'behave' as if tabs were spaces. In that case you would probably have to roll out something of your own, to convert tabs->space on reading and back to tabs when writing. Not entirely impossible, but other subtle formatting issues may crop up. One extremely crude way is to `:set noexapndatb`, then `:retab` and then `:w[rite]`. You may write macros to automate this. – xk0der Aug 03 '14 at 10:27
  • I'm marking this as the answer because it explains why what I'm trying to do can't be done easily. – quant Aug 03 '14 at 23:55
1

Unless you :set list and have a custom value for listchars, there's no difference, appearance-wise, between a tab and tabstop spaces. Here you have the same buffer with and without :set list:

<tab>foo
<space><space><space><space><space><space><space><space>bar

tabs vs spaces

So… are you asking about "appearance" or functionality?

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Appearance. I want to be able to "walk" through a tab just as I would through 4 spaces. – quant Aug 03 '14 at 07:29
  • 2
    That's "functionality", not "appearance" and there's AFAIK no way to do what you want without changing the whole buffer to use spaces on read and changing it back to use tabs on write. I'd suggest you bite the bullet and adjust to that project's convention, though. – romainl Aug 03 '14 at 08:53
  • Ok. Thanks. Yeah that was my suspicion but I thought I'd ask :) – quant Aug 03 '14 at 08:54
0

These are the settings I use:

set tabstop=4
set shiftwidth=4
set noexpandtab
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
0
gg=G

Your settings are correct. You just need to re-indent the document.

Arithran
  • 1,219
  • 3
  • 11
  • 22