Multiplexing the XFCE Terminal Emulator
Serge Y. Stroobandt
Copyright 2015–2017, licensed under Creative Commons BY-NC-SA
- Home
- IT
- Command Line
- XFCE Terminal Multiplexing
This page is still under construction. All scripts do work, however.
TODO: Write up remaining article text.
Introduction
Lately, I have become a frequent user of the xfce4-terminal
terminal emulator on Xubuntu LTS. I use it mainly to launch hg
Mercurial push & pull distributed version control commands. My text based journal and remind calendar
also automatically launch terminal emulators to keep a watchful eye on their respective hg
push & pulls whilst synchronising with my server.
However, with this increased terminal use, the overlapping pile of identically labelled «Terminal» buttons on my open windows bar started to annoy me. How am I supposed to retrieve the right terminal when they are all named the same?
I tried using more often ShiftCtrlT to open tabs in already existing terminal windows. That solution was still not good enough, though.
Consequently, I had a swift look at the established terminal multiplexers GNU screen
, tmux
and byobu
. These applications sure ought to have their merit, but in my experience, the terminal multiplexers simply get too much in my way.
Luckily, GNU/Linux applications very much resemble raw minerals; some love and a good polishing job will turn them into really shiny wannahave gems. XFCE’s default terminal emulator xfce4-terminal
is no different in that respect. It suffices to issue a quick man xfce4-terminal
to list the vast array of little used command line arguments that await discovering. This very same information is also available from the online XFCE documentation.
Screenshot
With this information, a «multiplexer» script was written; i.e. an augmented script version of the original xfce4-terminal
executable with automatic multiplexing added. Below screenshot shows what the xfce4-terminal
multiplexer looks like. Subsequent calls to xfce4-terminal
are opened in new and automatically labelled tabs in one and the same window.
Setting up ~/bin
Still interested? Fine! To proceed, we need to make sure that versions of executables in your personal ~/bin
directory are found first by the system. They need to take preference upon execution over the packaged versions bearing the same name. If you have not done so yet, create your very own ~/bin
directory. Having such a personal ~/bin
directory on your system is quite handy, also for other projects. If done properly, it will by no means upset your system. So, feel safe to go ahead.
$ cd
$ mkdir bin
If you are using a relatively modern GNU/Linux distribution, you stand a high chance it will feature a ~/.profile
file which already contains a PATH
variable putting $HOME/bin
first. If this is the case, simply disregard the remainder of this section.
If this is not the case, add the following line as the last line to your ~/.bashrc
configuration file:
PATH="$HOME/bin:$PATH"; export PATH
Required packages
xfce4-terminal
, wmctrl
, egrep
, pidof
Beware, pidof
is not available in all GNU/Linux distributions! You may try your luck with pgrep
, even though it is not a real drop-in replacement.
$ touch ~/bin/xfce4-terminal
$ chmod +x ~/bin/xfce4-terminal
The xfce4-terminal multiplexer script
#!/usr/bin/env bash
VERSION='202001'
# DESCRIPTION
# This script provides terminal emulator multiplexing
# through an xfce4-terminal replacement in ~/bin/.
# USAGE
# $ xfce4-terminal [optional arguments]
#
#
# Preferred Applications
# ~~~~~~~~~~~~~~~~~~~~~~
# In Settings → Preferred Applications → Utilities → Terminal Emulator, put:
#
# xfce4-terminal --command="%s" --title="%s"
#
#
# Launcher
# ~~~~~~~~
# In a Terminal Emulator launcher, just put:
#
# xfce4-terminal
#
# (Default is: exo-open --launch TerminalEmulator.)
#
#
# vim
# ~~~
# In .vimrc, put:
#
# command! Terminal :call system("xfce4-terminal --working-directory='".expand("%:p:h")."' &")
# noremap <silent> <F4> :Terminal<CR>
# vnoremap <silent> <F4> <ESC>:Terminal<CR>
# inoremap <silent> <F4> <ESC>:Terminal<CR>
#
#
# SpaceFM
# ~~~~~~~
# In SpaceFM → View → Preferences → Advanced → Terminal, put:
#
# xfce4-terminal --title="%D"
# REQUIRES
# awk, pidof, wmctrl, xfce4-terminal
# COPYRIGHT
# Copyright (C) 2015-2020 Serge Y. Stroobandt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# CONTACT
# xdg-open mailto:$(echo c2VyZ2VAc3Ryb29iYW5kdC5jb20K |base64 -d)
# TODO
# Some console programs should run in their own window:
# YFKTEST=$(wmctrl -lp |awk '{print$5}' |grep 'YFKtest')
if [[ $DISPLAY ]]; then
TERMINAL='/usr/bin/xfce4-terminal'
# Store the arguments for later use.
# Spaces in the arguments are to be expected.
# Hence, the internal field separator (IFS) is set to tab.
# https://stackoverflow.com/a/30439949/2192488
OLD_IFS="$IFS"
IFS=$'\t'
ARGUMENTS="$*"
# Check whether an xfce4-terminal instance is already running.
TERM_PID=$(pidof $TERMINAL) # Beware, pidof is not available in all GNU/Linux distributions!
echo $TERM_PID
# Determine the terminal instance title
# by processing the command line options
TITLE=''
WORKING_DIR=''
while [[ -n $1 ]]; do
case $1 in
*)
--working-directory=WORKING_DIR=`echo $1 |sed -e 's/^[^=]*=//g'`
;;
*)
--title=TITLE=`echo $1 |sed -e 's/^[^=]*=//g'`
;;
)
-Tshift
TITLE=$1
;;
*)
;;
esac
shift
done
if [[ -z $TITLE ]]; then
TITLE=$WORKING_DIR
fi
if [[ -z $TITLE ]]; then
TITLE='~'
fi
# If another terminal instance is running, use --tab as the first option.
# Otherwise, open a new xfce4-terminal window, which will be maximised if the file ~/.maximize exists.
# Note that repeating the same argument twice does not entail any negative consequences.
if [[ -n $TERM_PID ]]; then
$TERMINAL --tab --title=$TITLE $ARGUMENTS
elif [[ -f $HOME/.maximize ]]; then
$TERMINAL --maximize --title=$TITLE $ARGUMENTS
else
$TERMINAL --title=$TITLE $ARGUMENTS
fi
IFS="$OLD_IFS"
# Focus the last opened xfce4-terminal window
TERM_PID=$(pidof $TERMINAL)
TERM_WIN=$(wmctrl -lp |grep -E "^.+\b.+\b$TERM_PID\b" |tail -n1) # window number
wmctrl -ia $TERM_WIN
else
echo 'On this system, xfce4-terminal is not available.'
fi
Configuring the desktop
What rests to be done
Preferred terminal emulator
For new terminal tabs appropriate titles
In Settings → Preferred Applications → Utilities → Terminal Emulator
, put:
xfce4-terminal --command="%s" --title="%s"
This change takes immediately effect; logging in and out of the session is not required.
Terminal launcher
In a Terminal Emulator
launcher, just put: (Default is: exo-open --launch TerminalEmulator
.)
xfce4-terminal
SpaceFM
In Xubuntu LTS, I prefer SpaceFM as the default file manager, over Thunar. At the SpaceFM menu bar, select View → Preferences → Advanced → Terminal
, put:
xfce4-terminal --title="%D"
vimrc
As a bonus, In .vimrc
, put:
command! Terminal :call system("xfce4-terminal --working-directory='".expand("%:p:h")."' &")
noremap <silent> <F4> :Terminal<CR>
vnoremap <silent> <F4> <ESC>:Terminal<CR>
inoremap <silent> <F4> <ESC>:Terminal<CR>
Conclusion
hope that XFCE and Xubuntu developers will pick up this idea.
This work is licensed under a Creative Commons Attribution‑NonCommercial‑ShareAlike 4.0 International License.
Other licensing available on request.
Unless otherwise stated, all originally authored software on this site is licensed under the terms of GNU GPL version 3.
This static web site has no backend database.
Hence, no personal data is collected and GDPR compliance is met.
Moreover, this domain does not set any first party cookies.
All Google ads shown on this web site are, irrespective of your location,
restricted in data processing to meet compliance with the CCPA and GDPR.
However, Google AdSense may set third party cookies for traffic analysis and
use JavaScript to obtain a unique set of browser data.
Your browser can be configured to block third party cookies.
Furthermore, installing an ad blocker like EFF's Privacy Badger
will block the JavaScript of ads.
Google's ad policies can be found here.
transcoded by to make it run as secure JavaScript in the browser.