Copying image content to clipboard in vifm

Issue

I’m trying to write a command in vifm (on linux) that copies the content of an image to the clipboard, using copyq.

This is what I got so far:

I use the command xdg-mime query filetype myfile.jpg to get the mimetype.

I use qcopy to write the content of the file to the clipboard like this:
qcopy write $MIMETYPE - < myfile.jpg

The following command works fine in the shell and the content of the file gets copied to the clipboard:
qcopy write $(xdg-mime query filetype myfile.jpg) - < myfile.jpg

Now how can I rewrite this command as a vifm command in my vifmrc file?

I tried this but it doesn’t work:

command! copyf
\| let $MIMETYPE = system(expand('xdg-mime query filetype %c'))
\| execute expand("copyq write $MIMETYPE - < %c && copyq select 0")

I just get an "Invalid command name" error.

Solution

There are multiple issues here:

  1. By default command’s body is executed by the shell, if you start it with a colon, it will be processed as an internal one. See help.
  2. You need to have a space between command name and its body and writing copyf\n<spaces>\| results in first argument of :command being copyf|, hence Incorrect command name error. Help.
  3. qcopy was spelled as copyq in one place.
  4. :execute runs an internal command, internal command called ! runs a shell command, so ! needs to be used on the last line.
  5. % in a body of a command is expanded automatically, need to double it to escape so that it’s expanded later. Last line is expanded also by expand(), so double escaping of % there.

Fixed version that should work:

command! copyf
    \ : let $MIMETYPE = system(expand('xdg-mime query filetype %%c'))
    \ | execute expand('!qcopy write $MIMETYPE - < %%%%c && copyq select 0')

Answered By – xaizek

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published