LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-11-2005, 10:32 AM   #1
benr77
Member
 
Registered: Sep 2004
Location: London, UK
Distribution: Fedora Core 4
Posts: 59

Rep: Reputation: 15
Tab width in Bash - how do I set it


I've managed to set my tab width in vi to 4. How do I do the same for the Bash shell itself ??

If I view a file with code indented with tabs in vi, it looks fine, but if I then cat the file it is displayed differently due to different settings for tabs in the shell

Any ideas

Thanks
 
Old 05-11-2005, 04:51 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
That's not a job of bash but of cat/less/... or whatever you
use to view the file. cat doesn't have any options in this
respect, less has -x (man less for details). If you need to
do general processing jobs use the pair expand/unexpand
to convert tabs to spaces (and vice versa) before you do
to the file what you do.


Cheers,
Tink
 
Old 05-11-2005, 04:52 PM   #3
username17
Member
 
Registered: Aug 2004
Location: Norfolk VA
Distribution: Slackware 11
Posts: 230

Rep: Reputation: 30
Hello,
I've done a bit of reseach into this issue via google.
As far as I can see, there is no way to change the tab length for bash.

I do, however, have a workaround.
Use spaces instead of tabs in vi (personally, this is what I do).
Then when you cat the files out, the formatting stays the same.


Someone else might come along and have a resolution, but I didn't even find a hint of that cabapility of bash. One thing that you might try is looking up the functionality for sh and see if it was carried over.
GL,
-Jason
 
Old 05-11-2005, 05:02 PM   #4
benr77
Member
 
Registered: Sep 2004
Location: London, UK
Distribution: Fedora Core 4
Posts: 59

Original Poster
Rep: Reputation: 15
Hmmm. I've searched Google for ages, and the only thing I have seen that is close to what I want is a command called 'tabs', where you execute something like 'tabs -4' to set the tab width to 4. However, this doesn't appear to be available to many systems - I've seen it on Solaris, but can't find it for Redhat / Fedora which is what some of my systems run.

Regarding using spaces instead of tabs inside VI, well, personally I hate using spaces for indentation and such - it's not efficient character-wise, and is harder to keep code tidy and properly aligned. However, I don't want to open up the whole tabs vs. spaces issue as I know it's a contentious one!! But I do see your point username17.

I have also tried the expand/unexpand etc, which work, and also looked into the settings for each individual command. However, I want something that will work across the board and not on a per program basis, especially if it means extra keystrokes each time I want to execute it....

This issue does seem like a bit of a weird one, unless I'm missing something important.
 
Old 05-11-2005, 05:06 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
Quote:
Originally posted by benr77
This issue does seem like a bit of a weird one, unless I'm missing something important.
The "issue" is that you misinterpret what the shells
responsibilities are. It has NOTHING to do with the
representation of the output of other programs.

If there was a common place to look at things like
output formatting it would be the termcaps or something
along those lines, not the shell.

If you don't like the extra keystrokes make a script
or an alias ;)


Cheers,
Tink
 
Old 05-11-2005, 05:17 PM   #6
benr77
Member
 
Registered: Sep 2004
Location: London, UK
Distribution: Fedora Core 4
Posts: 59

Original Poster
Rep: Reputation: 15
Yes ok, that makes sense. So now I need to spend a while reading the man pages for commands I use and finding out how to control their output formatting, if it's possible...

Cheers
 
Old 05-11-2005, 09:50 PM   #7
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You could try the 'setterm' command. As in: setterm -regtabs 4. Try putting the command in your ~/.bashrc. Then reload .bashrc with the command 'source ./.bashrc'. Then try 'cat <somefile>' to see if it works. (all commands without quotes).
 
Old 06-10-2009, 03:35 AM   #8
BB_DaKraxor
LQ Newbie
 
Registered: Oct 2007
Location: Hungary, Budapest
Distribution: Gentoo
Posts: 17

Rep: Reputation: 0
Hi,

I know this thread has been dead for a long time but I think someone might find this information useful.

First of all, setterm solves the problem, but only works in tty.

If you want to "cat" a file, you can write a simple bash script that replaces tabs with a number of spaces in the output (not in the source file itself!) like this:

Code:
#!/bin/bash

# Author: BB_DaKraxor <bbdakraxor@gmail.com>

# Default number of spaces per tab:
NUM=4

if [[ $# -lt 1 || $1 == "--help" ]];
then
    echo "Usage: cattab <file> [space_count]"
    echo "Default value for space_count is $NUM."
    exit 1
fi

if [[ $# -gt 1 && $2 -gt 0 ]];
then
    NUM=$2
fi

SPC=""
for i in `seq 1 $NUM`;
do
    SPC="$SPC "
done

cat $1 | sed -e "s/\t/$SPC/g"
I like to call it "cattab". If you put it in your PATH (for example in /usr/bin/) you can run it as a replacement for cat when you want to format files that use tabs for indentation. Hope someone finds it useful.
 
Old 12-04-2012, 11:53 AM   #9
torment
LQ Newbie
 
Registered: Dec 2012
Posts: 1

Rep: Reputation: Disabled
Lightbulb

Ok first,
I know this is an old thread, but I've been looking for a more simple solution to this and I've come across this thread a few times while trying to do the same..
And finally found something that suits my needs without having to copy my custom formatting script to each and every server I access.. (similar to BB_DaKraxor's)

So, what worked for me is "expand":
Code:
expand -t 4 testfile
OR
Code:
cat testfile | expand -t 4
OR
Code:
whatever script | expand -t 4
In my case, I had field lengths that differed greatly.. ie: first column would be between 4 and 20 chars.. 2nd column would be between 5 and 6, etc.
So here's what I use.. you can define the list of tab lengths. (see man tab)
Code:
whatever | expand -t 24,34,52 #etc...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make a tab is 4 spaces width in vim? ICO SUSE / openSUSE 8 03-05-2013 11:24 AM
bash "tab tab" - how to turn off? nicflatterie Linux - General 2 04-04-2005 04:09 PM
How can a bash script findout the width of the terminal window alex.e.c Linux - Software 2 10-14-2004 12:20 PM
Set text (runlevel 3) console character width davee Linux - Newbie 2 11-16-2003 04:33 PM
Changing Text Size/Terminal Width in Bash TastyWheat Linux - General 2 11-03-2003 12:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration