How can I convert leading spaces to tabs in Vim or Linux?
Asked Answered
L

10

233

I've looked over several questions on Stack Overflow for how to convert spaces to tabs without finding what I need. There seem to be more questions about how to convert tabs to spaces, but I'm trying to do the opposite.

In Vim I've tried :retab and :retab! without luck, but I believe those are actually for going from tabs to spaces anyways.

I tried both expand and unexpand at the command prompt without any luck.

Here is the file in question:

http://gdata-python-client.googlecode.com/hg-history/a9ed9edefd61a0ba0e18c43e448472051821003a/samples/docs/docs_v3_example.py

How can I convert leading spaces to tabs using either Vim or the shell?

Larianna answered 1/2, 2012 at 23:2 Comment(4)
In @Matt's comment that is now deleted, the first example ( sed "s/ +/`echo -e '\t'`/g" < input.py > output.py )appears to convert all spaces, not just leading spaces. In the second example (sed "s/^ +/`echo -e '\t'`/g" < input.py > output.py ) it only replaces the first space on each line with a tab and leaves the rest of them.Larianna
Opposite related: How to replace tabs with spaces? at Vim SEAldas
I didn't find answer for all/many files, so I've wrote my own one: https://mcmap.net/q/117253/-how-can-i-convert-leading-spaces-to-tabs-in-vim-or-linux. With find, awk and blackjack (too long to leave it in comments, though)Crymotherapy
Does this answer your question? How can I convert tabs to spaces in every file of a directory?Crymotherapy
T
394

Using Vim to expand all leading spaces (wider than 'tabstop'), you were right to use retab but first ensure 'expandtab' is reset (:verbose set ts? et? is your friend). retab takes a range, so I usually specify % to mean "the whole file".

:set tabstop=2      " To match the sample file
:set noexpandtab    " Use tabs, not spaces
:%retab!            " Retabulate the whole file

Before doing anything like this (particularly with Python files!), I usually set 'list', so that I can see the whitespace and change.

I have the following mapping in my .vimrc for this:

nnoremap    <F2> :<C-U>setlocal lcs=tab:>-,trail:-,eol:$ list! list? <CR>
Trihedron answered 2/2, 2012 at 1:26 Comment(12)
here's how i got it to work, not sure what is necessary and what is not, and btw i don't know what the "%" before reatab is doing: :set noet, :set tabstop=2, :retab!, :%retab!, :set tabstop=1, :retab!, :%retab!Larianna
I've updated my answer to include an explanation as to why I use %. The F2 mapping is exactly as it's typed.Trihedron
great, thank you. I was only able to remove all spaces by setting tabstop twice (first as 2, then to 1) - does that sound right? thanks for sharing .vimrcLarianna
For that particular file, I would just do: :set noet ts=2 |%retab!Trihedron
I would add that you need :set nopreserveindent as well.Pul
@Unk: Quite possibly. I've never set 'preserveindent' and it defaults to off.Trihedron
@Johnsyweb to be fair, I was wrong: :%retab! still works. I was confused with ==, etc which does respect the preserveindent setting.Pul
To convert a coffeescript file from spaces to tabs I followed @Johnsyweb's answer (including the addition to the .vimrc file) but instead with a :set tabstop=4Aseptic
does this only adjust the leading indentation (i.e. spaces starting at column 0 until the first source code) or does it also convert spaces used for alignment (i.e. any spaces after the first source code)?Desiree
@TrevorBoydSmith: Leading only.Trihedron
I had an issue with all whitespace sections in the document being converted to tabs, how is this better than a plain find replace like :%s# #\t#g?Jeaninejeanlouis
For posterity, setting list means issuing :set list! which displays normally invisible tab characters as ^I and normally invisible newline as $. To "undo" this command, just reissue :set list! (the command behaves like a toggle switch)Dillon
L
50

1 - If you have spaces and want tabs.

First, you need to decide how many spaces will have a single tab. That said, suppose you have lines with leading 4 spaces, or 8... Than you realize you probably want a tab to be 4 spaces. Now with that info, you do:

:set ts=4
:set noet
:%retab!

There is a problem here! This sequence of commands will look for all your text, not only spaces in the begin of the line. That mean a string like: "Hey,␣this␣␣␣␣is␣4␣spaces" will become "Hey,␣this⇥is␣4␣spaces", but its not! its a tab!.

To settle this little problem I recomend a search, instead of retab.

:%s/^\(^I*\)␣␣␣␣/\1^I/g

This search will look in the whole file for any lines starting with whatever number of tabs, followed by 4 spaces, and substitute it for whatever number of tabs it found plus one.

This, unfortunately, will not run at once!

At first, the file will have lines starting with spaces. The search will then convert only the first 4 spaces to a tab, and let the following...

You need to repeat the command. How many times? Until you get a pattern not found. I cannot think of a way to automatize the process yet. But if you do:

`10@:`

You are probably done. This command repeats the last search/replace for 10 times. Its not likely your program will have so many indents. If it has, just repeat again @@.

Now, just to complete the answer. I know you asked for the opposite, but you never know when you need to undo things.

2 - You have tabs and want spaces.

First, decide how many spaces you want your tabs to be converted to. Lets say you want each tab to be 2 spaces. You then do:

:set ts=2
:set et
:%retab!

This would have the same problem with strings. But as its better programming style to not use hard tabs inside strings, you actually are doing a good thing here. If you really need a tab inside a string, use \t.

Lalittah answered 24/8, 2014 at 16:10 Comment(4)
Seems a extremely rare race condition. Anyway, create a function accepting visual-range selection and iterate search function until no match found, I guess that would be a more clever and useful answer.Ambert
Yeas, or that. If you want a more definitive solution you can use a function. Anyway, its always good to know how to do in the ex command, because this would be what is inside the function. And no, its not rare. You just need to have strings with spaces to have a mess. Not rare at all. I've being there. Thanks for commenting.Lalittah
Please explain why /g is not sufficient to convert all groups of 4 in each line consecutive spaces to tabs, instead of having to run search multiple times.Chancellorship
Hi @BjarturThorlacius, the problem is that if you remove the ^ symbol, to start the search from the begin of the line, you risk changing spaces inside strings. With the ^ you guarantee you are changing only spaces and tabs since the begin of the line, thus, indentation. Besides that, if you are sure it is ok to do all at once, remove the ^ and run only once with: :%s/\(^I*\)␣␣␣␣/\1^I/gLalittah
M
21
:%s/\(^\s*\)\@<=    /\t/g

Translation: Search for every instance of 4 consecutive spaces (after the = character), but only if the entire line up to that point is whitespace (this uses the zero-width look-behind assertion, \@<=). Replace each found instance with a tab character.

Metronome answered 28/1, 2016 at 0:22 Comment(1)
The only solution that worked for expanding 1 space to 1 tab, but beware the unicode in the answer, I just used :%s/\(^\s*\)\@<= /\t/g - put the appropriate number of spaces (to convert 4 spaces to 1 tab, put in 4 spaces) right after the <=Moshe
C
9

Linux: with unexpand (and expand)

Here is a very good solution: https://mcmap.net/q/99773/-how-can-i-convert-tabs-to-spaces-in-every-file-of-a-directory, mostly because it uses *nix-utilities:

  1. unexpand — spaces -> tabs
  2. expand — tabs -> spaces

Linux: custom script

My original answer

Bash snippet for replacing 4-spaces indentation (there are two {4} in script) with tabs in all .py files in the ./app folder (recursively):

find ./app -iname '*.py' -type f \
    -exec awk -i inplace \
    '{ match($0, /^(( {4})*)(.*?)$/, arr); gsub(/ {4}/, "\t", arr[1]) }; { print arr[1] arr[3] }' {} \; 

It doesn't modify 4-spaces in the middle or at the end.

Was tested under Ubuntu 16.0x and Linux Mint 18

Crymotherapy answered 16/6, 2017 at 5:30 Comment(0)
G
8

Changes all spaces to tab :%s/\s/\t/g

Graham answered 28/11, 2017 at 7:30 Comment(2)
This is precisely what I needed. Thanks. The only change I had to make was to replace every 4 spaces with a tab instead of doing every single space.Picrite
This will convert all spaces to tabs, not just indentation.Pellerin
C
2

at a shell with GNU coreutils: unexpand -t2 --first-only originalFile > newFile explanation: unexpand: the command to turn spaces into tabs -t2: turn 2 spaces into 1 tab --first-only: only modify whitespace at the beginning of a line (including mixed tabs and spaces), do not modify whitespace in the middle or at the end of a line

the reciprocal of this is expand and it takes a simple -i instead of --first-only, with -t2 unchanged

the popular green-checked answer by Johnsyweb is flat out WRONG because it modifies whitespace all over the place!!!

p.s. the crazy formatting of this post does not reflect the quality of the content. it reflects how stackoverflow does not support my browser. thank you.

Conch answered 3/1, 2022 at 22:47 Comment(0)
H
1

In my case, I had multiple spaces(fields were separated by one or more space) that I wanted to replace with a tab. The following did it:

:% s/\s\+/\t/g
Hoebart answered 22/1, 2019 at 16:35 Comment(0)
A
0

If you have GNU coreutils installed, consider %!unexpand --first-only or for 4-space tabs, consider %!unexpand -t 4 --first-only (--first-only is present just in case you were accidentally invoking unexpand with --all).

Note that this will only replace the spaces preceding the prescribed tab stops, not the spaces that follow them; you will see no visual difference in vim unless you display tabs more literally; for example, my ~/.vimrc contains set list listchars=tab:▸┈ (I suspect this is why you thought unexpand didn't work).

Astrophotography answered 28/8, 2019 at 21:46 Comment(0)
T
0

To use Vim to retab a set of files (e.g. all the *.ts files in a directory hierarchy) from say 2 spaces to 4 spaces you can try this from the command line:

find . -name '*.ts' -print0 | xargs -0 -n1 vim -e '+set ts=2 noet | retab! | set ts=4 et | retab | wq'

What this is doing is using find to pass all the matching files to xargs (the -print0 option on find works with the -0 option to xargs in order to handle files w/ spaces in the name).

xargs runs vim in ex mode (-e) on each file executing the given ex command which is actually several commands, to change the existing leading spaces to tabs, resetting the tab stop and changing the tabs back to spaces and finally saving and exiting.

Running in ex mode prevents this: Vim: Warning: Input is not from a terminal for each file.

Torque answered 25/1, 2020 at 0:50 Comment(0)
H
-1

Simple Python Script:

import os

SOURCE_ROOT = "ROOT DIRECTORY - WILL CONVERT ALL UNDERNEATH"

for root, dirs, files in os.walk(SOURCE_ROOT):
    for f in files:
        fpath = os.path.join(root,f)
        assert os.path.exists(fpath)
        data = open(fpath, "r").read()
        data = data.replace("    ", "\t")
        outfile = open(fpath, "w")
        outfile.write(data)
        outfile.close()
Hearts answered 29/3, 2016 at 15:50 Comment(2)
One huge issue: this snippet replaces all 4-spaces, not only indentation (at the beginning of the line)Crymotherapy
and the second: file-filter is not implementedCrymotherapy

© 2022 - 2025 — McMap. All rights reserved.