[vim] Replace tabs with spaces in vim

I would like to convert tab to spaces in gVim. I added the following line to my _vimrc:

set tabstop=2

It works to stop at two spaces but it still looks like one tab key is inserted (I tried to use the h key to count spaces afterwards).

I'm not sure what should I do to make gVim convert tabs to spaces?

This question is related to vim vi

The answer is


Try

set expandtab

for soft tabs.

To fix pre-existing tabs:

:%s/\t/  /g

I used two spaces since you already set your tabstop to 2 spaces.


first search for tabs in your file : /^I :set expandtab :retab

will work.


Try

set expandtab

for soft tabs.

To fix pre-existing tabs:

:%s/\t/  /g

I used two spaces since you already set your tabstop to 2 spaces.


gg=G will reindent the entire file and removes most if not all the tabs I get in files from co-workers.


expand is a unix utility to convert tabs to spaces. If you do not want to set anything in vim, you can use a shell command from vim:

:!% expand -t8

This got it working for me:

:set tabstop=2 shiftwidth=2 expandtab | retab

gg=G will reindent the entire file and removes most if not all the tabs I get in files from co-workers.


expand is a unix utility to convert tabs to spaces. If you do not want to set anything in vim, you can use a shell command from vim:

:!% expand -t8

Once you've got expandtab on as per the other answers, the extremely convenient way to convert existing files according to your new settings is:

:retab

It will work on the current buffer.


first search for tabs in your file : /^I :set expandtab :retab

will work.


This got it working for me:

:set tabstop=2 shiftwidth=2 expandtab | retab

This worked for me:

you can see tabs with first doing this:

:set list

then to make it possible to replace tabs then do this:

:set expandtab

then

:retab

now all tabs have been replaced with spaces you can then go back to normal viewing like this :

:set nolist

This article has an excellent vimrc script for handling tabs+spaces, and converting in between them.

These commands are provided:

Space2Tab Convert spaces to tabs, only in indents.

Tab2Space Convert tabs to spaces, only in indents.

RetabIndent Execute Space2Tab (if 'expandtab' is set), or Tab2Space (otherwise).

Each command accepts an argument that specifies the number of spaces in a tab column. By default, the 'tabstop' setting is used.

Source: http://vim.wikia.com/wiki/Super_retab#Script

" Return indent (all whitespace at start of a line), converted from
" tabs to spaces if what = 1, or from spaces to tabs otherwise.
" When converting to tabs, result has no redundant spaces.
function! Indenting(indent, what, cols)
  let spccol = repeat(' ', a:cols)
  let result = substitute(a:indent, spccol, '\t', 'g')
  let result = substitute(result, ' \+\ze\t', '', 'g')
  if a:what == 1
    let result = substitute(result, '\t', spccol, 'g')
  endif
  return result
endfunction

" Convert whitespace used for indenting (before first non-whitespace).
" what = 0 (convert spaces to tabs), or 1 (convert tabs to spaces).
" cols = string with number of columns per tab, or empty to use 'tabstop'.
" The cursor position is restored, but the cursor will be in a different
" column when the number of characters in the indent of the line is changed.
function! IndentConvert(line1, line2, what, cols)
  let savepos = getpos('.')
  let cols = empty(a:cols) ? &tabstop : a:cols
  execute a:line1 . ',' . a:line2 . 's/^\s\+/\=Indenting(submatch(0), a:what, cols)/e'
  call histdel('search', -1)
  call setpos('.', savepos)
endfunction

command! -nargs=? -range=% Space2Tab call IndentConvert(<line1>,<line2>,0,<q-args>)
command! -nargs=? -range=% Tab2Space call IndentConvert(<line1>,<line2>,1,<q-args>)
command! -nargs=? -range=% RetabIndent call IndentConvert(<line1>,<line2>,&et,<q-args>)

This helped me a bit more than the answers here did when I first went searching for a solution.


Add following lines to your .vimrc

set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>

Open a file in vim and press F2 The tabs will be converted to 4 spaces and file will be saved automatically.


Try

set expandtab

for soft tabs.

To fix pre-existing tabs:

:%s/\t/  /g

I used two spaces since you already set your tabstop to 2 spaces.


Once you've got expandtab on as per the other answers, the extremely convenient way to convert existing files according to your new settings is:

:retab

It will work on the current buffer.


This article has an excellent vimrc script for handling tabs+spaces, and converting in between them.

These commands are provided:

Space2Tab Convert spaces to tabs, only in indents.

Tab2Space Convert tabs to spaces, only in indents.

RetabIndent Execute Space2Tab (if 'expandtab' is set), or Tab2Space (otherwise).

Each command accepts an argument that specifies the number of spaces in a tab column. By default, the 'tabstop' setting is used.

Source: http://vim.wikia.com/wiki/Super_retab#Script

" Return indent (all whitespace at start of a line), converted from
" tabs to spaces if what = 1, or from spaces to tabs otherwise.
" When converting to tabs, result has no redundant spaces.
function! Indenting(indent, what, cols)
  let spccol = repeat(' ', a:cols)
  let result = substitute(a:indent, spccol, '\t', 'g')
  let result = substitute(result, ' \+\ze\t', '', 'g')
  if a:what == 1
    let result = substitute(result, '\t', spccol, 'g')
  endif
  return result
endfunction

" Convert whitespace used for indenting (before first non-whitespace).
" what = 0 (convert spaces to tabs), or 1 (convert tabs to spaces).
" cols = string with number of columns per tab, or empty to use 'tabstop'.
" The cursor position is restored, but the cursor will be in a different
" column when the number of characters in the indent of the line is changed.
function! IndentConvert(line1, line2, what, cols)
  let savepos = getpos('.')
  let cols = empty(a:cols) ? &tabstop : a:cols
  execute a:line1 . ',' . a:line2 . 's/^\s\+/\=Indenting(submatch(0), a:what, cols)/e'
  call histdel('search', -1)
  call setpos('.', savepos)
endfunction

command! -nargs=? -range=% Space2Tab call IndentConvert(<line1>,<line2>,0,<q-args>)
command! -nargs=? -range=% Tab2Space call IndentConvert(<line1>,<line2>,1,<q-args>)
command! -nargs=? -range=% RetabIndent call IndentConvert(<line1>,<line2>,&et,<q-args>)

This helped me a bit more than the answers here did when I first went searching for a solution.


Add following lines to your .vimrc

set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>

Open a file in vim and press F2 The tabs will be converted to 4 spaces and file will be saved automatically.


Once you've got expandtab on as per the other answers, the extremely convenient way to convert existing files according to your new settings is:

:retab

It will work on the current buffer.


If you want to keep your \t equal to 8 spaces then consider setting:

   set softtabstop=2 tabstop=8 shiftwidth=2

This will give you two spaces per <TAB> press, but actual \t in your code will still be viewed as 8 characters.


Once you've got expandtab on as per the other answers, the extremely convenient way to convert existing files according to your new settings is:

:retab

It will work on the current buffer.