2

Some file managers, such as pcmanfm or pcmanfm-qt, open terminals in the current directory by setting $PWD before opening the terminal. Most terminals behave as expected. This includes xterm, gnome-terminal, and xfce4-terminal. However, konsole always opens in a pre-specified directory (usually $HOME).

How can konsole be made to respect $PWD as other terminals do?


At the time this question was initially written, a patch had been submitted, but not accepted. However, the patch appears to have since been accepted, so that Konsole in Ubuntu 22.04 behaves as expected.

This was a long-standing issue that had been around since at least 2010.

0

1 Answer 1

4

Note: Recent versions of Konsole now respect $PWD. However, this solution may still be of use because it shows how to customize Konsole behavior. For instance, shells can be opened in tabs instead of new windows.


For several years, there has been no setting to make konsole respect $PWD. However, there is a command-line option --workdir that can be exploited by a script to make konsole function as desired.

  1. Check the path to locate a user-accessible location that takes precedence over /usr/bin by examining the output of echo $PATH. Typical options include ~/bin/ and ~/.local/bin/.

  2. Create a file named ~/bin/konsole (or ~/.local/bin/konsole) with the following contents:

     #!/usr/bin/env bash
     # make konsole respect $PWD, as other terminals do
     # save as ~/bin/konsole or ~/.local/bin/konsole
    
     # catch unset or mis-set PWD
     [[ -z $PWD ]] && PWD="$HOME"
     [[ "$PWD" == '/' ]] && PWD="$HOME"
    
     term_cls='konsole'      # window class needed for xdotool to activate window
     term_bin=`type -a -p konsole | tail -n 1`    # location of original `konsole`
     term_cmd=( "$term_bin" )
    
     [[ $PWD ]] && term_cmd+=( --workdir "$PWD" )
     term_tab=( "${term_cmd[@]}" --new-tab )
    
     case $1 in
       -h|--help|-v|--version|--author|--license|--list-profiles|--list-profile-properties)
         "$term_bin" "$1"
         ;;
       -p)
         echo 'cannot change profile properties at this time'
         ;;
       *)
         find_app=( xdotool search --onlyvisible --class "$term_cls" windowactivate exec "${term_tab[@]}" )
         "${find_app[@]}" || xdotool exec "${term_cmd[@]}"
         ;;
     esac
    
  3. Give the new script execute permissions.

     chmod +x ~/bin/konsole
    

Now, whenever this script is run, it will process $PWD and execute /usr/bin/konsole --workdir "$PWD". If put in the right folder, it will have priority over the real konsole and usage will be transparent. Similar scripts can be created for other programs that do not behave as desired.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .