How to Convert Spaces to Tabs in Vim or Linux

Converting a file's indentation from tabs to spaces

awk '{s="";i=1;while(substr($0,i++,1)=="\t")s=s "   ";sub(/^\t*/,s)}1'

How can I convert tabs to spaces in every file of a directory?

Warning: This will break your repo.

This will corrupt binary files, including those under svn, .git! Read the comments before using!

find . -iname '*.java' -type f -exec sed -i.orig 's/\t/ /g' {} +

The original file is saved as [filename].orig.

Replace '*.java' with the file ending of the file type you are looking for. This way you can prevent accidental corruption of binary files.

Downsides:

  • Will replace tabs everywhere in a file.
  • Will take a long time if you happen to have a 5GB SQL dump in this directory.

Visual Studio Code - Convert spaces to tabs

There are 3 options in .vscode/settings.json:

// The number of spaces a tab is equal to.
"editor.tabSize": 4,

// Insert spaces when pressing Tab.
"editor.insertSpaces": true,

// When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents.
"editor.detectIndentation": true

editor.detectIndentation detects it from your file, you have to disable it.
If it didn't help, check that you have no settings with higher priority.
For example when you save it to User settings it could be overwritten by Workspace settings which are in your project folder.

Update:

To access these settings, you can open File » Preferences » Settings, click the Manage cog icon at the bottom left, or use the keyboard shortcut:

CTRL+, (Windows, Linux)

+, (Mac)

Update:

Now you have an alternative to editing those options manually.
Click on selector Spaces:4 at the bottom-right of the editor:

 Ln44, Col .  [Spaces:4] . UTF-8 with BOM . CTRLF . HTML . :)

EDIT:
To convert existing indentation from spaces to tabs hit Ctrl+Shift+P and type:

>Convert indentation to Tabs

This will change the indentation for your document based on the defined settings to Tabs.

Tabs and spaces in vim

It is perhaps a good idea not to use tabs at all.

:set expandtab

If you want to replace all the tabs in your file to 3 spaces (which will look pretty similar to tabstop=3):

:%s/^I/   /

(where ^I is the TAB character)

From the VIM online help:

'tabstop' 'ts'      number  (default 8)
local to buffer
Number of spaces that a <Tab> in the file counts for. Also see
|:retab| command, and 'softtabstop' option.

Note: Setting 'tabstop' to any other value than 8 can make your file
appear wrong in many places (e.g., when printing it).

There are four main ways to use tabs in Vim:
1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
(or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim
will use a mix of tabs and spaces, but typing <Tab> and <BS> will
behave like a tab appears every 4 (or 3) characters.
2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
'expandtab'. This way you will always insert spaces. The
formatting will never be messed up when 'tabstop' is changed.
3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
|modeline| to set these values when editing the file again. Only
works when using Vim to edit the file.
4. Always set 'tabstop' and 'shiftwidth' to the same value, and
'noexpandtab'. This should then work (for initial indents only)
for any tabstop setting that people use. It might be nice to have
tabs after the first non-blank inserted as spaces if you do this
though. Otherwise aligned comments will be wrong when 'tabstop' is
changed.

Redefine tab as 4 spaces

It depends on what you mean. Do you want actual tab characters in your file to appear 4 spaces wide, or by "tab" do you actually mean an indent, generated by pressing the tab key, which would result in the file literally containing (up to) 4 space characters for each "tab" you type?

Depending on your answer, one of the following sets of
settings should work for you:

  • For tab characters that appear 4-spaces-wide:

    set tabstop=4

    If you're using actual tab character in your source code you probably also want these settings (these are actually the defaults, but you may want to set them defensively):

    set softtabstop=0 noexpandtab

    Finally, if you want an indent to correspond to a single tab, you should also use:

    set shiftwidth=4
  • For indents that consist of 4 space characters but are entered with the tab key:

    set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab

To make the above settings permanent add
these lines to your vimrc.

In case you need to make adjustments, or would simply like to understand what these options all mean, here's a breakdown of what each option means:

tabstop


The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an actual tab character.

shiftwidth


The size of an "indent". It's also measured in spaces, so if your code base indents with tab characters then you want shiftwidth to equal the number of tab characters times tabstop. This is also used by things like the =, > and < commands.

softtabstop


Setting this to a non-zero value other than tabstop will make the tab key (in insert mode)
insert a combination of spaces (and possibly tabs) to simulate tab stops at this width.

expandtab


Enabling this will make the tab key (in insert mode) insert spaces instead of
tab characters. This also affects the behavior of the retab command.

smarttab


Enabling this will make the tab key (in insert mode) insert spaces or tabs to
go to the next indent
of the next tabstop when the cursor is at the beginning of a line (i.e. the
only preceding characters are whitespace).

For more details on any of these see :help 'optionname' in vim (e.g. :help 'tabstop')



Related Topics



Leave a reply



Submit