Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make existing tabs appear as 4 spaces in vim?

Tags:

vim

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.

like image 494
quant Avatar asked Oct 31 '22 19:10

quant


2 Answers

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.

like image 146
xk0der Avatar answered Nov 24 '22 16:11

xk0der


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?

like image 44
romainl Avatar answered Nov 24 '22 14:11

romainl