Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible using fluxbox to setup up a shortkey to say winkey+r that opens up a terminal (say xterm or whatever) with these properties:

  1. If there is already a terminal that the working directory is = ~ and it is not currently running a command (i.e. its at the prompt) then show that one.
  2. Or if not create a new terminal
share|improve this question
Is your shell bash, zsh, or something else? – Mikel Feb 3 '11 at 7:49
I suggested changing the title because I don't think the question is specific to Fluxbox. No harm leaving it in the tags or the question tho. – Mikel Feb 3 '11 at 12:29

Here's how I'd do it:

  1. make your shell set the title to "bash" when at the prompt, and the name of the command when it runs a command, or anything so you can tell them apart
  2. create a script that calls wmctrl to list open windows, then:
    • if wmctrl finds a window with a title of "bash", raise it
    • else start an xterm
  3. use xbindkeys to call that script when you press your shortcut

making your shell set the window title when you run a command

Here's a simplified version of how I do it in bash. I haven't double checked the code, but it works on my machine.

# set the title when we run a command
    if $has_debug_trap
        trap 'command=$BASH_COMMAND; eval settitle "\"${title}\""; trap - DEBUG' DEBUG

# set the xterm title
    printf "\033]0;$*\007"

promptstring='$(hostname):$(pwd)$ '
title='$(hostname) "${command:-$0}"'

# prompt and window title
if test -n "${title}"
    PROMPT_COMMAND='command=; eval settitle "\"${title}\""'
    if $has_debug_trap
        PROMPT_COMMAND="$PROMPT_COMMAND; setcommandhook"
if test -n "${promptstring}"
    PS1='$(eval echo -n "\"${promptstring}\"")'

Doing it in zsh is easier, because it has the preexec hook.

You can see my shell configs for more details, e.g. the getcommand function which handles commands like fg in a nicer way.

raising the xterm that has a bash prompt otherwise starting a new one

Write a script that uses wmctrl -l to list windows, looking for one with bash in the title. If one is found, then run wmctrl -i -a <id returned by wmctrl -l> to raise it, else just call xterm.

Here is a script that does it:

# bashprompt
# if there is an xterm open at a bash prompt, raise/focus that window
# if there isn't start a new xterm
# requires that your xterm window title has "bash" at the end
# when there is no command running and doesn't have "bash" at the end
# when a command is running
# see <http://unix.stackexchange.com/questions/6842> for more details
# Mikel Ward <mikel@mikelward.com>

# change this to whatever is unique about your window title
# (i.e. a string that appears in the title when the shell is at a prompt
#  but does not appear when running a command)

if ! type wmctrl >/dev/null 2>&1; then
    echo "wmctrl can't be found, please install it" 1>&2
    exit 1

if ! output="$(wmctrl -l)"; then
    echo "Error running wmctrl -l" 1>&2
    exit 1
while IFS=$'\n' read -r line; do
    if [[ $line =~ $prompttitle ]]; then
        id=${line%% *}
done <<EOF

if test -n "$id"; then
    wmctrl -i -a "$id"

Or download it from my scripts repository.

running the script when you press Win+R

Assuming your script is called /usr/local/bin/bashprompt, make a file ~/.xbindkeysrc containing:

    Mod4 + r

then run xbindkeys. Add it to your .Xclients file or similar to make it start up automatically.

share|improve this answer
I wrote the bashprompt script. You should now have everything you need. – Mikel Feb 4 '11 at 6:42

fluxbox can match windows based upon certain patterns on its own. With at least fluxbox-1.1.1 this is possible:

  Mod4 r :If {Some Matches (title=.*bash) (class=XTerm)} {NextWindow (xterm)} {Exec xterm}

(translates to: on press windows-key + r: check, if there is a xterm with its title ending in 'bash'. if there is, go to that window; if not, open a new one. With a bleeding edge version (git) you can even go to windows on a different workspace.

The only thing you have to do is to modify the title (or any other property of the window carrying the bash) depending on what you do. if you look at the prompt you have to set a property, if you launch a command, you have to take away that property. fluxbox is not able to look inside the applications, it only knows about the windows.

share|improve this answer
+1 Shorter than mine, but of course you'd still need to use my .bashrc above. – Mikel Feb 4 '11 at 8:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.