# Set the tab length in cmd.exe

By default tab characters in cmd are written as 8 characters long. Is there any way I can change this to something smaller?

We use tabs for indentation in our code. So diff output from Mercurial (i.e. hg diff) becomes quite horrible to read.

-
Are you sure there isn't an option in Mercurial to set the tab width for diff? (Don't use Mercurial, but I know you can certainly do it with git and I would have expected every almost programmer's editor and viewer to be configurable). – sgmoore May 16 '12 at 17:18
No, I couldn't find one. I may try to write an extension for it. But I thought it would be worth checking if there wasn't a simple solution in cmd first. – Gieron May 16 '12 at 18:38

With a batch file you can use line by line string substitution to replace tabs with four spaces:

@echo off

setlocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('hg diff') do (
set __temp=%%a
echo !__temp:   =    !
)

endlocal


The EnableDelayedExpansion enabled the use of !s and the modification of variables within the for loop.

It appears inserting code into this answer has substituted the tab for three spaces. When you copy this to a batch file, make sure there's an actual tab character between the : and = in the echo line.

If you have single quotes in the command you need to run (I don't actually know the full syntax of hg diff), then you have to surround it with backquotes and enable usebackq. Which means you can't use backquotes in the command you need to run. Pick one.

Backquoted version:

@echo off

setlocal EnableDelayedExpansion

for /f "usebackq tokens=* delims=" %%a in (hg diff) do (
set __temp=%%a
echo !__temp:   =    !
)

endlocal

-
I find it interesting that you didn't need to use usebackq and then hg diff ... cool solution. – jftuga May 16 '12 at 11:55
@jftuga usebackq is only necessary if the command to be executed contains quotes (') itself. If he needs to use single quotes in his command, then yes, the usebackq option is needed and backquotes should be used instead of quotes around the command. But that means backquotes cannot be used in the command... hmm... pick one ;) – Bob May 16 '12 at 11:57
Great tip! Thanks for the explanation. I did not know the details of usebackq. – jftuga May 16 '12 at 12:04
This is the primary reason I favor spaces over tabs, wrapping all of my hg, svn, git commands in this manner just so my diffs, blames and cats have the same indentation as they do in my editor seems silly. But kudos to @Bob for the clever solution. I suppose my alternative could be to set tab indention to 8 spaces in my editor... – Marcus Pope Sep 18 '12 at 18:15

I'm not sure where this 8 chars are coming from for you, but I have always had 4 chars when typing any text file out from cmd.exe using TYPE or COPY, it may be that the output is double-tabbed. In keeping with traditional DOS, the width of a TAB is 4 chars, there are MANY command line tools out there that rely on this, that is the most likely reason there is no way to change it as far as I know (if there was it would be somewhere in the registry, but i never saw it), unless you decompile/recompile cmd.exe. My suggestion is to write a script or console tool that prints the output to the console screen for you, interpreting the tabs however you want or find a tool like this already made somewhere.

-
I disagree, as does every version of every operating system (dos, windows, linux, osx etc) that I have ever used and and so does en.wikipedia.org/wiki/Tab_key which says '... de facto standardized at every multiple of 8 characters horizontally'. – sgmoore May 16 '12 at 17:06

I have no idea how can you do what you want, but you could:

1. change the size of the buffer and window widths to increase the total line space;
2. redirect the output to a file, then read the file with a text editor like Notepad++.

c:\>hg diff > file.txt

-

This isn't a general solution, but since you happened to ask about 'hg diff', and it happened to show up in my Google query.. :)

So the way I solved the tab problem for this particular case was to use less.exe. I happened to be using it already, actually, but it just needed an extra parameter. Here's a bit of my hgrc (simplified):

[extensions]
pager =

[pager]
attend = annotate, cat, diff, export, glog, help, log, status
pager = path\to\less.exe --quit-if-one-screen --raw-control-chars --tabs=4
hg16mode = yes

-