KDE4 Bash Functions

I’m in the mood for sharing my tips and tricks. TechBase has some nice bash functions for the user you built KDE as (kde-devel for most people). However, if you installed as another user, there are two common tasks: running a KDE 4 app from your normal user’s account, and switching to your kde-devel user and running a KDE 4 app from there.

A few functions in your main user’s .bashrc and in kde-devel’s .bashrc make all this simple.

Disclaimer: these functions are tailored to my set up (Arch Linux) and will probably need adapting for your system. Especially the kde3 and qt3 functions.

First of all: running KDE 4 apps as your normal user. Put the following in your user’s ~/.bashrc (you just insert removefrom, qt4-copy and kde4 if you like):

function removefrom()
{
    local varname=$1
    local entry="$2"

    unset newvar
    for value in `IFS=: eval echo \\$$varname`; do
	if [ "$value" != "$entry" ]; then
	    newvar="${newvar}${newvar+:}${value}"
	fi
    done

    # Set it again
    eval $varname="$newvar"
    unset newvar
}

function qt3()
{
    removefrom PATH $QTDIR/bin
    removefrom LD_LIBRARY_PATH $QTDIR/lib
    removefrom PKG_CONFIG_PATH $QTDIR/lib
    removefrom MANPATH $QTDIR/man

    source /etc/profile.d/qt.sh

    echo "Using QT 3 ($QTDIR)"
}

function qt4()
{
    removefrom PATH $QTDIR/bin
    removefrom LD_LIBRARY_PATH $QTDIR/lib
    removefrom PKG_CONFIG_PATH $QTDIR/lib
    removefrom MANPATH $QTDIR/man

    source /etc/profile.d/qt4.sh

    echo "Using system QT 4 ($QTDIR)"
}

function qt4-copy()
{
    removefrom PATH $QTDIR/bin
    removefrom LD_LIBRARY_PATH $QTDIR/lib
    removefrom PKG_CONFIG_PATH $QTDIR/lib
    removefrom MANPATH $QTDIR/man

    export QTDIR=/home/kde-devel/build/qt-copy
    export PATH=$QTDIR/bin:$PATH
    export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
    export PKG_CONFIG_PATH=$QTDIR/lib:$PKG_CONFIG_PATH
    export MANPATH=$MANPATH:$QTDIR/man
    export QMAKESPEC=$QTDIR/mkspecs/linux-g++

    echo "Using QT 4 from KDE SVN ($QTDIR)"
}

function kde3()
{
    qt3

    # Remove the current KDE from variables
    removefrom PATH $KDEDIR/bin
    removefrom LD_LIBRARY_PATH $KDEDIR/lib
    unset QT_PLUGIN_PATH
    unset KDEHOME
    unset KDETMP
    unset KDEVARTMP

    # Add the new ones
    source /etc/profile.d/kde.sh

    echo "Using KDE 3 ($KDEDIR)"
}

function kde4()
{
    qt4-copy

    # Remove the current KDE from variables
    removefrom PATH $KDEDIR/bin
    removefrom LD_LIBRARY_PATH $KDEDIR/lib
    removefrom PKG_CONFIG_PATH $KDEDIR/lib/pkgconfig

    # Add the new one
    export KDEDIR=/home/kde-devel/kde
    export KDEDIRS=$KDEDIR
    export KDEHOME=$HOME/.kde4
    export KDETMP=${TMPDIR-/tmp}/kde4-$USER
    export KDEVARTMP=/var/tmp/kde4cache-$USER
    export KDE_DATA_DIRS=$KDEDIR/share
    export PATH=$KDEDIR/bin:$PATH
    export XDG_CONFIG_DIRS=$KDEDIR/etc/xdg
    export QT_PLUGIN_PATH=$KDEDIR/lib/plugins
    export LD_LIBRARY_PATH=$KDEDIR/lib:$LD_LIBRARY_PATH
    export PKG_CONFIG_PATH=$KDEDIR/lib/pkgconfig:$LD_LIBRARY_PATH
    export KDE_COLOR_DEBUG=1

    # Ensure that they exist
    mkdir -p $KDEDIR $KDETMP $KDEVARTMP

    echo "Using KDE 4 ($KDEDIR)"
}

Now you can type “kde4″ at a prompt, and thereafter you are in a KDE4 environment. So typing konqueror will give you Konqi from KDE 4 (exactly how I’m typing this post). “kde3″ will get you back to a KDE 3 session.

Now for switching to kde-devel. The problem is that you want kde-devel to have the magic cookie that allows it to connect to your X server. Your normal user needs to give it out like so:

function go-kde()
{
    xauth list $DISPLAY | sed "s/^.*$DISPLAY/$DISPLAY/" > /tmp/x.auth
    sudo su - kde-devel
    rm -f /tmp/x.auth
}

Typing “go-kde” should now take you to a kde-devel session, with your magic cookie saved in /tmp/x.auth. But how do applications find out about it? Put the following in kde-devel’s ~/.bashrc:

if [ -f /tmp/x.auth ]; then
	xauth add $(cat /tmp/x.auth)
fi

Easy, huh?

About these ads

6 Responses to “KDE4 Bash Functions”

  1. koko Says:

    Isn’t it easier to use Sux ( http://fgouget.free.fr/sux/sux-readme.shtml ) ? For me it is easier ;]

  2. koko Says:

    edit:// just “sux – kde4user” and it work

  3. Fred Says:

    Great! i’ll test that right out when i come home. I already had kde-devel and everything, but switching profile is such an easiest way for testing!
    KMines, here i come!!!

  4. randomguy3 Says:

    koko: I’d never come across sux before – thanks for the link.

  5. jospoortvliet Says:

    Yeah, I use sux too, it rocks ;-)

  6. Chani Says:

    I use ssh to switch to my kde4 user. here’s the settings in my .ssh/config:
    Host kde4
    HostName localhost
    ProxyCommand none
    Port 22
    ForwardAgent yes
    ForwardX11 yes
    User kde4

    then whenever I want to use it, I just ssh kde4. :)
    the proxy & port are probably unnecessary for other people; I have a rather crazy net setup.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: