2

In Windows paths are (unreasonably) long, so it is common to drag and drop from some links to the terminal or copy and paste paths from file managers. It is possible to put in the shell init file a function like

cdw() { cd "`cygpath -u $1`"  }

Now we have a Windows version of cd. Assuming that "C:\Program Files" is in your clip, you can type:

cdw "CTRL+V"

and CD there. I agree with you, quoting is boring: cdw CTRL+V (no double quotes) would be the killer cd.
This is easy in Bash exploiting the history command, as shown here. But I use zsh, where issuing history does not return the very last command (i.e. history itself). By trial and errors I came up with this function:

cdw(){
  print -s 
  set $(fc -l -1 | tail -2 | head  -1)
  shift 2
  p=`cygpath -u "$*"`
  cd "$p"
}

It works but seems too convoluted. I wonder if you can find a solution more elegant than mine.

1

That history hack is a really strange way to solve this problem, and it's pretty fragile. It won't work with some characters that are valid in Windows file names such as parentheses.

There's a much easier way to use to a pasted Windows path. Instead of pasting it into the line editor, call the getclip utility (which is in cygutils-extra in recent versions of Cygwin).

cdw () {
  cd "$(getclip)"
}
cd "`getclip`"/../foo

You can sometimes get away without the double quotes, but not always, e.g. if a file name contains two consecutive spaces.

If you want to be able to edit the path, bind a key to expand-or-complete-prefix, e.g. type "`getclip`" and press Esc Tab:

bindkey '\e\t' expand-or-complete-prefix

Another approach is to bind a key to insert the quoted content of the clipboard.

insert-quoted-clipboard-content () {
  local text=${(q)$(getclip)}
  BUFFER=${BUFFER:0:$CURSOR}$text${BUFFER:$CURSOR:}
  ((CURSOR += #text))
}
zle -N insert-quoted-clipboard-content
bindkey '^X^V' insert-quoted-clipboard-content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.