pfr I have it set to emacs. Why? I dont know.. I initially copied my .shrc from @pin and never gave it another thought. However, I dont use (or know how to use) emacs so should I change it to vi or can I simply set it to use vim ?

As nia said, mode affects the way the interactive shell will respond to user's input, for it may have vi or emacs like bindings and behavior. Emacs mode is more intuitive (standard navigation with arrow keys, Delete erases backwards).

This has nothing to do with your editor of choice however and doesn't affect the way the shell parses input and scripts.

ksh93 also provides gmacs mode as a third alternative.

As the sh(1) man page suggests, you can further adjust interactive shell bindings by configuring the editline library in your ~/.editrc (NetBSD doesn't use readline, so a Linux inputrc file is not going to work).

  • pfr likes this.

pfr what shell are you using? Those options are only supported by NetBSD's sh and won't work on FreeBSD's sh, dash, busybox ash. Also, as far as I remember promptcmds was introduced in NetBSD -8, so it won't be recognized on legacy systems. If you're not using command substitution in your PS1/PS2, you may as well avoid using it. cdprint on the other hand comes always in handy.
If you plan on keeping your ~/.shrc or ~/.profile unaltered across systems, for portability you can also quietly test for options to be supported and only enable them in the latter case:

( set -o cdprint 2>/dev/null ) && set -o cdprint
( set -o promptcmds 2>/dev/null ) && set -o promptcmds
  • pfr replied to this.
  • pfr likes this.

    Rather than start a new thread... Can anyone hep me with setting a nicer prompt in ksh?

    I'd like the current directory in the prompt. Dont need hostname or username.

    Something like ~ $ for home and ~/Downoads $ & /usr/local $

    Not sure if it's possible to replace /home/user with ~ ? I'd also still just settle for a function that runs pwd after I cd to another directory.

      pfr Rather than start a new thread... Can anyone hep me with setting a nicer prompt in ksh?

      Try with this:

         function _cd
          {
      	# change directory
      	dir=$1
         	arg=$2
         	if [ "$arg" ]
         	then
             	  \cd "$dir" "$arg"
         	elif [ "$dir" ]
         	then
             	  \cd "$dir"
         	else
             	  \cd
         	fi
      	# set prompt
      	typeset dir="${PWD:-$(command pwd -L)}"
      	dir="${dir#$HOME/}"
      	case $dir in
      	    "$HOME")
      		PS1=$(printf "\033[35m%s\033[00m \033[37m$\033[00m " "\~");;
      	     /*)
      		PS1=$(printf "\033[35m%s\033[00m \033[37m$\033[00m " "${PWD}");;
      	    *)
      		PS1=$(printf "\033[35m%s\033[00m \033[37m$\033[00m " "\~/$dir");;
      	    esac
          }
          alias cd='_cd'
          eval _cd
      

      I have set the colors of the working directory and the dollar sign to be whatever your terminal assigns to 'white' and 'magenta' respectively. Remember you can escape the alias with a backslash, so type \cd for standard cd

      • pfr replied to this.
      • pfr likes this.

        JuvenalUrbino
        Cheers. That works well, except it still print's the full path ie. /home/user rather than ~
        Another thing that factors in is that I often use zoxide instead of cd so ideally I would like to have the shell's PS1 recognise the working directory (and print it) regardless of the tool used to change directories.

        Perhaps it isn't possible, but I just wondered.

          pfr
          EDIT: made function more robust.

          My version:

          # test:
          #
          # mkdir $'/tmp/rvp'\'$'s\n"home" dir'
          # HOME="/tmp/rvp's
          # \"home\" dir/////////////"
          #
          cd() {
          	# set -x				# tron
          	command cd -- "$@"
          
          	# realpath(1) not available everywhere so,
          	# turn: ///some//path///// -> /some/path
          	#       / -> ///			# side-effect :(
          	#
          	local S="$(dirname "$PWD")/$(basename "$PWD")"
          	local R="$(dirname "$HOME")/$(basename "$HOME")"
          	S=$(echo "$S" | tr -s /)		# /// -> /
          	R=$(echo "$R" | tr -s /)		# /// -> /
          
          	# tack on `/' so /home/davey !~ /home/dave
          	#
          	if [[ "$S/" == "$R/"* ]]		# ksh(1), bash(1) ver.
          	# if echo "$S/" | grep -q -- "^$R/"	# grep(1) ver.
          	# if expr "$S/" : "$R/" >/dev/null	# expr(1) ver.
          	then	PS1=$(printf '\033[35m%s\033[0m \033[1;37m$\033[0m ' "~${S#$R}")
          	else	PS1=$(printf '\033[35m%s\033[0m \033[1;37m$\033[0m ' "$S")
          	fi
          	# set +x				# troff
          }
          cd "$PWD"

          Shorter if ksh had bash's ${var/pattern/string}:

          cd()
          {
          	command cd -- "$@"
          	PS1="${PWD/#$HOME/\~}$ "
          }
          • pfr replied to this.
          • pfr likes this.

            pfr Cheers. That works well, except it still print's the full path ie. /home/user rather than ~

            Have you aliased cd to _cd? And executed _cd upon startup? On my side it works as expected, on ksh93 (or, to be more precise ksh2020), which I tend to use outside of NetBSD as it is the default system intrerpreter on illumos. Edit: indeed I didn't ask you what sort of ksh implementation are you on; I've never used pdksh and derivatives (oksh, mksh).
            Opening a terminal:

            ~ $ pwd                                                                                            
            /home/vincent ~ $ cd Documents ~/Documents $ cd /usr/lib /usr/lib $ cd .. /usr $ cd - /usr/lib /usr/lib $ cd ~ ~ $ cd Pictures/Wallpapers ~/Pictures/Wallpapers $ cd ~/.config/mc ~/.config/mc $

            By, the way, you came up with a cool suggestion. I think I'm sticking with this :3

              Got inspired 😉

              PS1="$(printf "\033[32m[$(date +%a\ %b\ %d\ %H:%M)]\033[00m") ${USER}@${HOST%%.*}$(printf "\033[33m:\${PWD##/}/\033[00m") $PS1"

                pin \${PWD##/}

                Typo?: \${PWD##/} => \${PWD##*/}

                • pin replied to this.

                  rvp I'm not a programmer, got the pieces together from the ksh man page and StackOverflow. It's working, although I've reverted it to the simple prompt I had before.

                  Would you care to elaborate the difference?

                  If I change it to what you wrote, I get

                  [Wed Sep 01 22:43] pin@mybox:pin/ $ cd /usr/pkgsrc
                  [Wed Sep 01 22:43] pin@mybox:pkgsrc/ $

                  i.e. not the full path

                  • rvp replied to this.

                    pin i.e. not the full path

                    No, you get the last component of a full path with that, same as basename:

                    $ echo "${PWD##*/}"
                    $ basename "$PWD"

                    Not what you wanted?

                    ${PWD##/} will remove one /, same as ${PWD#/}:

                    $ pwd
                    /home/rvp/work
                    $ echo "${PWD##/}"
                    home/rvp/work
                    $ X="/$PWD"
                    $ echo "$X"
                    //home/rvp/work
                    $ echo "${X#/}"
                    /home/rvp/work
                    $ echo "${X##/}"
                    /home/rvp/work
                    $
                    • pin replied to this.

                      rvp Not what you wanted?

                      Actually, I'm happy with pin@mybox $ but, if I was to use the long one I'd want the full path. You see, I have two pkgsrc trees in my system, so simply printing pkgsrc could cause some issues.

                      Anyway, would you happen to know how you can make xterm display more than 76 characters and not shifting the line while tab-completing at the shell? Such a long prompt becomes annoying when this happens.
                      Actually, this happens on other terminals as well on NetBSD but, on Void I can have really long lines and it will display them in their full-lenght.

                        pin Anyway, would you happen to know how you can make xterm display more than 76 characters and not shifting the line while tab-completing at the shell? Such a long prompt becomes annoying when this happens.
                        Actually, this happens on other terminals as well on NetBSD but, on Void I can have really long lines and it will display them in their full-lenght.

                        I recall ksh shifts long lines horizontally; bash, and NetBSD sh if set -o emacs is set, wrap (and back-up) over multiple lines just fine. But, 76 cols. is odd. Does stty size print the correct vals. for your terminal? Or, is COLUMNS being set anywhere? If COLUMNS is set by the user, shells take that as gospel and no longer update it for window size changes.

                          So, I decided to implement the same concept on sh and tcsh

                          • Jay likes this.

                          pin Actually, this happens on other terminals as well on NetBSD but, on Void I can have really long lines and it will display them in their full-lenght.

                          I agree with rvp; something is definitely setting the number of columns to 76 (less than 80, how inconvenient 😆 !). May also be a stty cols setting in your ~/.profile. I don't think xterm has a Xresources options to define the number of columns (outside of the geometry itself)

                          • pin replied to this.

                            pin Actually, I'm happy with pin@mybox $ but, if I was to use the long one I'd want the full path.

                            For you (not perfect--won't work on FreeBSD):
                            (Moved here)

                            • pin replied to this.

                              rvp For you (not perfect--won't work on FreeBSD):

                              Thanks, I don't use FreeBSD 😁

                              Anyway @rvp and @JuvenalUrbino this thing with the shifting lines on the shell...

                              JuvenalUrbino something is definitely setting the number of columns to 76 (less than 80, how inconvenient 😆 !).

                              rvp Does stty size print the correct vals. for your terminal?

                              pin@mybox $ stty size
                              43 166

                              rvp Or, is COLUMNS being set anywhere? If COLUMNS is set by the user, shells take that as gospel and no longer update it for window size changes.

                              The issue is xterms default geometry. I did not set it anywhere since my window manager takes care of it but, internally xterm is still set to 80 columns.
                              The 76 I mentioned was my mistake as I was using tab-completion, if I write it letter by letter towards the end, it only happens going from column 79 to 80.

                              Sorry for the noise 😒