Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make terminal open gui application with focus on it?

I might be nitpicking here but it's really annoying when terminal doesn't open application like Chrome, Sublime Text with focus on it if I open them from terminal. I do something like :-

shriek@ubuntu ~ $ subl . 

And then I have to press Alt + tab to search that application if I have multiple application running. It defeats the whole purpose of trying to get to my application faster from terminal.

If there's a reason for this kind of behavior can I know why it's like this and also possible workaround for this?

like image 897
shriek Avatar asked Aug 28 '14 23:08

shriek


1 Answers

I am using wmctrl for this. (You can install it with sudo apt-get install wmctrl)

Add the following line in your .bashrc to open and focus Sublime Text:

alias subl="subl && wmctrl -a \"\$(wmctrl -l | grep subl | cut -f 1,2,3,4 -d ' ' --complement)\" 2>&1 >/dev/null"

wmctrl -l scans your windows and lists the applications you can switch to.
wmctrl -a WINDOW_TITLE focus on the application with the specific WINDOW_TITLE.
To get the window title I grep and format it with grep APP_NAME and cut.

You can use this alias for every other application by only changing the alias-name, the command to start the application and with the argument grep is looking for. For example:

alias chrome="google-chrome && wmctrl -a \"\$(wmctrl -l | grep Chrome | cut -f 1,2,3,4 -d ' ' --complement)\" 2>&1 >/dev/null"
like image 75
miu Avatar answered Oct 08 '22 01:10

miu