1

Currently I am using zsh and every time I use tab completion for directories it will add a forward slash at the end. This creates a problem when using commands like rsync, which differenciates directory arguments that have a forward-slash. So every time I use tab-completion I need to go back and delete the forward slash.

milarepa
  • 759
  • 9
  • 28

1 Answers1

-2

Not my code, I got this from zshwiki (link is now dead: ln -s wayback machine), but you can put this in your .zshrc file:

remove-slash () {
    if [[ $LBUFFER = */ ]] && [[ $LBUFFER != *' '/ ]]; then
        LBUFFER="${LBUFFER[0,-2]} "
    else
        LBUFFER+=' '
    fi
}

no-remove-slash () {
    LBUFFER+=' '
}

zle -N remove-slash
zle -N no-remove-slash
bindkey " " remove-slash
bindkey "^x " no-remove-slash

Every time you press the space key, because of the bindkey " " remove-slash line, the function remove-slash is called, which manipulates the string of characters left of the cursor, LBUFFER to remove a trailing slash.

Note, however, that this function call is made every time you press the space key at the command line, so if you were to type sed s/ /_/g for example, you will end up with sed s /_/g because remove-slash will kill the first slash. So, the other bindkey line allows you to deal with this situation: you press Ctrl-x and then Space and the slash will not be swallowed up.

This solution is a bit hack-ish, though, and there might just be some zsh option for your problem, but you have this in the meantime.

I encourage you to play around with this code and see what it does. Open up a terminal and put these lines in at the prompt and see what they do. You may mess up your ability to enter input at that particular prompt, but you can just close the terminal and start over: nothing gets saved, as it were. Also take a look at the zle documentation, dense though it sometimes is.

Zorawar
  • 6,505
  • 2
  • 23
  • 41