Skip navigation

Tag Archives: CLI

I was always worried about having to go up in a unix directory tree with cd ../. Then I was introduced to this project at work where I noticed that I was doing so much cd-ing around. Sometimes I went so deep in the directory tree I had this scary feeling inside that I’d get lost in a dark pit (true story).

I wanted a better way to do it. So I did what anyone these days would do. But the best the internet could offer were using pusd/popd, aliasing multiple ../s to ..n and using CDPATH. None of these felt natural enough.

So on one boring evening at work I started to scratch my own itch. I started to do it in python but I ended up in a simpler, much better solution. A function for my bashrc.

I call it za because …

cd to za

cd to za (image: markhillary flickr)

you can use za to jumpback any number of directories up to your $HOME directory. Just put the following function in your .bashrc

za 2 #will jump 2 directories up
za #will jump one directory up

Just see it in action.


za in action

Hope you find it useful.

PS: My blog marked 6 years to yesterday (8th August 2012). I’m out of (a year?) long block with this post :)

Update (07-03-2014): There’s a Fish Shell port of Za now.

I like Command Line Interface. Maybe I’ve been saying this earlier on my twitter if not on this blog. Since I started with GNU/Linux I was so fascinated with the CLI. The first reason was it’s something not everyone preferred. The next it’s the preferred way of all the hackers I read about in text files.

With time I learned why people called it’s easy and powerful. I was familiar with it to the point that I felt home in a text terminal with green text on black background. Then came the time I was looking for CLI tools for most of my day to day stuff. Ever since I found Amarok not doing well for me I didn’t use a GUI music player. Right now I’m using MoC and never had second thoughts.

And I recently had this moment I found GUI to be very confusing. And when I was thinking about it later it was very confusing for me to understand too. GUI are made to be easy to use right?.

It was the last semester and we were doing Prolog. There was a practical session in labs to get us familiar with the visual Prolog (I’m not certain though) interface. But as always I had a good reason to not go there which I can’t remember now. But there’s a limit even a student can push things off. And so came the day I do Prolog.

As you can imagine it was the last possible moment start with Prolog and I was in a hurry. I checked out the the IDE on a friends machine and I thought it’s the end.

What Visual Prolog presented me (thx @varunarl)

What I saw. cc

If I found the software for GNU/Linux I would use it, somehow. But luckily I didn’t and found swi-prolog instead. I just wanted to write some code and see how it works. swi-prolog got me going in no time. I wanted to learn how to use consult and it was pretty much all about it. I learned how to do it in visual Prolog in order to help a friend. But it was still way too complicated.

Swi-prolog basic usage

I know that the fact I’m trying to convey is not really rational. But the point is, in the GUI environments all controls are visible and for a beginner that can be too much. But in the CLI I only chose what I want. I learned both swi-prolog and visual Prolog with guides found on internet but I’m sure I took more time to filter out the necessary details in visual Prolog guide.

I didn’t do any serious programming in Prolog so I can’t say how each tool would serve you in such a scenario. But in my situation the CLI tool saved me a lot of frustration and time which helped me achieve required knowledge level for the exam. And on a not so related side note, *surprise surprise* I have passed that module :D.

Hacking is always fun. That’s why I always lose focus on everything once I find something to hack around. And that’s why I spent whole last night trying to figure out how to mimic Window focus event in a shell script. Though this is completely new to _me_, you maybe very much comfortable with implementing it :) . If so please let me know in comments.

Anyway here is my story.

I found this nice command line IM client called centerIM (Thanx @chathuraw for info). Once I got it set up I was just scanning on it’s documentation page & came across this interesting part “External actions & auto-responses“. I wrote a script right away for GUI notifications, inspired by script for irssi. Here is the script if you are interested.

Since the load of IM messages was very high at a time & It was useless when the terminal window with centerIM was already focused (which means I’m chatting on centerIM) I was thinking of getting notifications only when centerIM window is not in focus. Few Google searches lead me to very limited resources & I chose xwininfo & xdpyinfo commands for my work.

Workaround: I’m using gnome for now & so gnome-terminal as my terminal emulator. I had to set Edit >Pprofile Preferences > Title & command option to keep initial title, in order to have “Terminal” as the terminal window name all the time.

And ran xininfo to get following output.

chanux@nim:~$ xwininfo -name Terminal

xwininfo: Window id: 0x3800003 "Terminal"

 Absolute upper-left X:  0
 Absolute upper-left Y:  52
 Relative upper-left X:  0
 Relative upper-left Y:  27
 Width: 1280
 Height: 691
 Depth: 24
 Visual Class: TrueColor
 Border width: 0
 Class: InputOutput
 Colormap: 0x20 (installed)
 Bit Gravity State: NorthWestGravity
 Window Gravity State: NorthWestGravity
 Backing Store State: NotUseful
 Save Under State: no
 Map State: IsViewable
 Override Redirect State: no
 Corners:  +0+52  -0+52  -0-25  +0-25
 -geometry 156x37+0+25

This can be used to get the window id of the window which runs the script.

Then I can find the focused window at the moment as following.

chanux@nim:~$ xdpyinfo |grep focus
focus:  window 0x3800004, revert to Parent

I fetched the window IDs with a little bit more work.

Anyway the problem I had is that, I get 0x3800003 (at this example) or likewise for the Terminal window id & even when the Terminal id is focused I get 0x3800004 as the focused window id. Yes I know with some more work I can manage to handle that & come to a point that I can compare those window Ids to check for the focused window. But I really like to know why that difference come up. Anyone have an explanation? Or anyone like to mess with this?

Following is the way I fetched window ids from above outputs

chanux@nim:~$ xwininfo -name "Terminal" | grep xwininfo | cut -d " " -f 4
chanux@nim:~$ xdpyinfo | grep focus | cut -d " " -f 4 | sed s/,//