Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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.
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 ;)
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...
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).
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.