Skip to content

Instantly share code, notes, and snippets.

@codemedic
Forked from chetanmeh/.bashrc
Last active October 3, 2021 11:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codemedic/18589cbe2adef370c9638b463688af67 to your computer and use it in GitHub Desktop.
Save codemedic/18589cbe2adef370c9638b463688af67 to your computer and use it in GitHub Desktop.
Script to launch commands in multiple tabs in KDE Konsole
[ -r ~/path/to/konsole-tabs.sh ] &&
. ~/path/to/konsole-tabs.sh
# Run multiple commands in individual tabs
commands_in_tabs() {
local cmd profile clear_first clear_cmd="" exit_new_tabs_afterwards exit_current_tab_afterwards title i
local sessions=()
: "${profile:="$(qdbus org.kde.konsole /Konsole defaultProfile)"}"
: "${clear_first:=1}"
: "${exit_new_tabs_afterwards:=0}"
: "${exit_current_tab_afterwards:=0}"
: "${reuse_current_tab:=0}"
# title_in_args set to 1 to load even args as title for the tab
# Example:
# commands_in_tabs command1 title1 command2 title2 ...
: "${title_in_args:=0}"
if [ $# -gt 0 ] && [ "$reuse_current_tab" = 1 ]; then
# wouldn't close the current tab if asked to reuse it
exit_current_tab_afterwards=0
# create a background task to inject the first command into the current tab
# Double shell so that bash's job-control wouldn't polute the shell
( (
if [ "$title_in_args" = 1 ]; then
qdbus org.kde.konsole "$KONSOLE_DBUS_SESSION" setTitle 0 "$2" </dev/null &>/dev/null
sleep 0.1
qdbus org.kde.konsole "$KONSOLE_DBUS_SESSION" setTitle 1 "$2" </dev/null &>/dev/null
sleep 0.1
fi
# prefix with space to avoid an entry in the history
qdbus org.kde.konsole "$KONSOLE_DBUS_SESSION" sendText " $1"$'\n' </dev/null &>/dev/null
)& )
if [ "$title_in_args" = 1 ]; then
shift 2
else
shift
fi
fi
if [ $# -eq 0 ]; then
return;
fi
if [ "$clear_first" = 1 ]; then
if [ "$exit_new_tabs_afterwards" = 1 ]; then
clear_cmd="HISTFILE=/dev/null; clear; "
else
clear_cmd="clear; "
fi
fi
# Format '<Tab Name/Title> <Profile Name> <Command>'
for ((i=1; i<=$#; i++)); do
cmd="${!i}"
if [ "$title_in_args" = 1 ]; then
let i++
title="${!i}"
else
title="${cmd%% *}"
fi
if [ "$exit_new_tabs_afterwards" = 1 ]; then
cmd="exec ${cmd}"
fi
sessions+=("${title}" "$profile" "${clear_cmd}${cmd}")
done
konsole_start_sessions "${sessions[@]}"
if [ "$exit_current_tab_afterwards" = 1 ]; then
# close the current shell
exec sleep 0.1
elif [ "$reuse_current_tab" = 1 ]; then
qdbus org.kde.konsole /Konsole prevSession </dev/null &>/dev/null
fi
}
# SSH on to multiple hosts in individual tabs
multi_ssh() {
local i ssh_cmds=()
for ((i=1; i<=$#; i++)); do
ssh_cmds+=("ssh ${!i}" "${!i}")
done
local clear_first exit_new_tabs_afterwards exit_current_tab_afterwards reuse_current_tab title_in_args
clear_first="${clear_first:-1}" \
exit_new_tabs_afterwards="${exit_new_tabs_afterwards:-1}" \
exit_current_tab_afterwards="${exit_current_tab_afterwards:-1}" \
reuse_current_tab="${reuse_current_tab:-1}" \
title_in_args=1 \
commands_in_tabs "${ssh_cmds[@]}"
}

Script to launch commands in multiple tabs in KDE Konsole.

It is based on script provided in Linux Journal.

# Based on http://www.linuxjournal.com/content/start-and-control-konsole-dbus
# Start sessions in konsole.
function konsole_start_sessions()
{
local nsessions=0
local i session_num
for ((i = 1; i <= $#; )); do
local name="${!i}"
let i++
local profile="${!i}"
let i++
local cmd="${!i}"
let i++
session_num=$(qdbus org.kde.konsole /Konsole org.kde.konsole.Window.newSession "$profile" "$HOME")
sleep 0.1
qdbus org.kde.konsole "/Sessions/$session_num" setTitle 0 "$name" </dev/null &>/dev/null
sleep 0.1
qdbus org.kde.konsole "/Sessions/$session_num" setTitle 1 "$name" </dev/null &>/dev/null
sleep 0.1
qdbus org.kde.konsole "/Sessions/$session_num" sendText "$cmd"$'\n' </dev/null &>/dev/null
sleep 0.1
let nsessions++
done
# Activate first session.
for ((; nsessions > 1; nsessions--)); do
qdbus org.kde.konsole /Konsole prevSession </dev/null &>/dev/null
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment