KDE and .xinitrc

The Problem

On startup many X window managers look for a file called ~/.xinitrc and run it if it is present. This can be a convenient way to tweak your session preferences, eg: override your PATH, start xscreensaver etc.

KDE does not do this. A Google trawl did not yield a solution and so I had to figure it out.

The Solution

A poke around in /usr/bin/startkde revealed the following:
# Source scripts found in <localprefix>/env/*.sh and <prefixes>/env/*.sh
# (where <localprefix> is $KDEHOME or ~/.kde, and <prefixes> is where KDE is installed)
# This is where you can define environment variables that will be available to
# all KDE programs, so this is where you can run agents using e.g. eval `ssh-agent`
# or eval `gpg-agent --daemon`.
# Note: if you do that, you should also put "ssh-agent -k" as a shutdown script
# (see end of this file).
# For anything else (that doesn't set env vars, or that needs a window manager),
# better use the Autostart folder.

exepath=`kde-config --path exe | tr : '\n'`

for prefix in `echo "$exepath" | sed -n -e 's,/bin[^/]*/,/env/,p'`; do
  for file in "$prefix"*.sh; do
    test -r "$file" && . "$file"
So there's the answer: If you haven't already got one, create the directory ~/.kde/env. Any script placed into that directory will be run on startup.

For example, to set your session language environment you could do:

cat > ~/.kde/env/set_lang.sh <<EOF
Easy once you figure out where to look...