Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Apr 25, 2010

cat that displays tabs at 4 spaces

So in the tabs vs spaces war I'm squarely on the side of use tabs. tabs have meaning. tabs allow people to set there editor's to whatever space display width their eyes are comfortable with as opposed to what yours are (e.g. you like 2 spaces I like 4). However, I've heard the argument what about when you cat the file, etc. Well it is actually a bit annoying... so let's fix it.

my requirements when I type cat foo if foo has tabs they will be displayed at 4 spaces, and I can still pass all cli options as I normally would to cat. To do this we're going to create a shell function called etcat()
I'd like to note, that in order for this to work you must use either an absolute path or a call to env cat because otherwise the shell will attempt to call cat as a recursive function. I put this in my .alias file and sourced it.

you can now put alias cat="etcat" in your aliases file, or run etcat. This is to protect you from any problems that could otherwise be caused by overriding cat in scripts you don't control (not that I'm actually aware of any for this function).

P.S. Thanks to @greybot on #bash for the incantation as I was having trouble with the "$@"

UPDATE:
changed name of function.

Feb 16, 2010

bash and konsole

So I've had lots of problems using a multi-tab xterminal, for the casual user I'm sure they'd not even notice them, or think I'm silly, but when you spend all day in a shell they become less so.

History


one of the big problems I've had is with history. see when you close a shell then it writes the history. well if you have 5 tabs open each with with a super long history they'll write those histories out in the order they close in. basically what happens is all the history saves at once so one of your open shells histories will be before another's and if each has like 50 entries it can be quite a way back. In fact I'm sure I've had it happen where parts of my history was lost. So what do you do? you have to separate them per tab. One way to do this is with gnu screen. I don't like screen's history because it records the output of the terminal as well as the command, so this wasn't a good solution for me. The solution is that each tab must have its own history file. Well how do we do that? before I get to it I have another problem.

PS1, umask, and default group

I also have a need to have different settings depending on what I'm working on. If I'm working on files that need to be read and written to by my web server they need to have a different group and umask. first I solved this by su - to a different user, then I solved it by having different bashrc's but neither fully solved the history problem.

howto do it

first you have to have one konsole profile for each of the different settings you need. so I have one for web development called webdev. Then you have to add an environment variable to it I'm going to call it KONSOLE_PROFILE and set that to whatever your profile is called. Neither name really matters so long as they don't conflict with others. Then you need to code your bashrc (or other shell to work with it). I have a shortened sample bashrc that you can use in a gist