shyiko/commacd

Save and recover IFS variable before calling _command_cd

javier-lopez opened this issue · 1 comments

Hello,

90% of the time after I change directories I execute ls to see what's inside, so I've created a script called cd_and_ls.sh on /usr/local/bin

#!/bin/sh

cd "${@}" && ls

I used to set it as a mycd alias. Upon discovering commacd, I added source commacd.bash at the end of my ~/.bashrc file and exported my previous script as the COMMACD_CD variable:

  export COMMACD_CD='.   /usr/local/bin/cd_and_ls.bash'

However it seems like the IFS variable is creating issues with the _commacd_forward and _commacd_backward_forward functions.

$ mkdir -p one/two/three
$ cd one/two/three
$ ,,  one #it works
#however if I run
$ , one #it fails with the following message
bash: .   /usr/local/bin/cd_and_ls.bash: No such file or directory
#this is due to bash trying to execute '. /usr/local/bin/cd_and_ls.bash' as a single file instead of sourcing  /usr/local/bin/cd_and_ls.bash
# the same issue is seen with the ,,, alias (_commacd_backward_forward)

Saving the old ifs and restoring it before calling _command_cd fix the issue

_commacd_forward() {
  if [[ -z "$*" ]]; then return 1; fi
  OLDIFS="${IFS}"
  local IFS=$'\n'
  local dir=($(_commacd_forward_by_prefix "$@"))
  if [[ "$COMMACD_NOTTY" == "on" ]]; then
    printf "%s\n" "${dir[@]}"
    return
  fi
  if [[ ${#dir[@]} -gt 1 ]]; then
    dir=$(_commacd_choose_match "${dir[@]}")
  fi
  IFS="${OLDIFS}"
  _command_cd "$dir"
}

Thank you for the great tool =)