As you may know, the alias
alias -g v='"$(xclip -selection c -o)"'
means that, when you invoke
v, the shell tries to execute a command named as the content of the clipboard. This is what happens in your first code snippet. However, invoking
v and observing its effects is misleading because the intended use for this global alias is to be an argument to other commands.
sed-based solution will work if you properly use command substitution (
$(...)) and quoting:
alias -g v='$(xclip -selection clipboard -o | sed '"'"'s|^file://||'"'"')'
xclip pipes the content of the clipboard to
sed, which removes the
file:// part at its beginning (the pattern is anchored, note the
/ as the
s command separator in the
sed script makes it more readable and removes the need to escape
/ in the pattern.
The command substitution means that the output of the pipeline is substituted where
v is invoked.
You can then use it as:
cp -- v /path/to/destination
Beware that this will make it harder to act upon a file named
v in the current working directory, because the literal
v will be always substituted. As a workaround, you will be able to use the relative path
(Also, consider that some programs accept non prefixed single-letter options, e.g. the perfectly legal (but non POSIX)
ps v. Single-letter global aliases are just unsafe).
And, last but not least, such a global alias won't work with files whose name contains space characters, because they will be split by the shell after the alias is substituted.
Alternatively, you may avoid a global alias and define:
alias v="xclip -selection clipboard -o | sed 's|^file://||'"
which can be used as:
cp -- "$(v)" /path/to/destination
This will also work with file names with space characters.
Note, however, that:
- Leveraging the substitution capabilities of the shell, as explained in thrig's answer, would be more efficient.
File names displayed by Dolphin and copied to the clipboard are encoded, and thus not suitable for being consumed by the shell as long as they include uncommon characters. For instance:
touch $'file with a\nnewline'
# Copied in Dolphin
% echo "$(v)"
file with a%0Anewline
% cat "$(v)"
cat: '/path/to/file with a%0Anewline': No such file or directory