I install xclip to access system clipboard, my setting and some operations as below:

➜  ~ cat .zshrc | grep xclip
alias c="xclip -selection c"
alias v="xclip -selection c -o"
➜  ~ which dict | c
➜  ~ v       
➜  ~ sudo vim ??  

/usr/bin/dict is a python script. And what I want to know is what can substitute ?? on my last command can use vim open file /usr/bin/dict?

  • 1
    Do you want vim to open the file named in the clipboard, or do you want vim to open with the contents of the clipboard in the editing buffer? – wurtel Jan 8 at 8:31
  • @wurtel the first, I want vim to open the file named in the clipboard! But the second is also a thing I don't know I will search it, if I don't find out it will be a new question to be asked. – roachsinai Jan 8 at 8:43
  • @wurtel Is there a way to directly open with the contents of the clipboard in the editing buffer? In my mind, I have to run vim and then use "+p to paste it to vim. If there is a method, I could ask a new question at Vim Stack Exchange. – roachsinai Jan 8 at 8:50
  • 1
    You can run some-command | vim - and vim will read its input and place that in the buffer. – wurtel Jan 8 at 8:52
  • @wurtel thanks a lot, grep and - cool, will check the function of -. Cool! – roachsinai Jan 8 at 8:55

Given your current setup

sudo vim "$(v)"

will use command substitution to run your alias v and insert its output into the command line to be run before executing it, so it would run sudo vim /usr/bin/dict in the end. The quotes make sure it comes out as a single argument, and $(...) handles running the command and capturing its output.

This will only work from your interactive shell, since aliases aren't used elsewhere. If you want to access it from a script, you could make a small script somewhere in your PATH variable with the same command inside it.

You've tagged this but then shown an extract from .zshrc, so note also that zsh has "global aliases" that expand anywhere:

alias -g v='"$(xclip -selection c -o)"'

and then just run sudo vim v and have that happen for free. Global aliases are almost always more trouble than they're worth, so I don't recommend that, but depending on your usage patterns some global alias might be convenient (I'd recommend a longer and more unusual name if you do: putting some odd punctuation in can help avoid running it by mistake, so alias -g v# ... or @v or ^v or something).

  • Wow, thanks, global seems very convenient for me. And the reason I use zsh but tagged bash is I think what works in zsh should be worked in bash, so I want a more general method. – roachsinai Jan 8 at 8:46

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.