2

From the the Vifm wiki and using xclip, they show how to copy the filename of a selected file:

nnoremap yn :!echo -n %c | xclip -selection clipboard %i<cr>:echo expand('%"c') "is yanked to clipboard"<cr>

But it copies it with the extension, how can I copy the name of a file without it's extension to the clipboard?

GhostOrder
  • 157
  • 1
  • 7
  • The secret is to realise that there is not extension: file-name extensions where is CPM. And MS-Windows. However they don't exist in Unix or NT. The dot and the rest of the name, is just part of the name. However you can use the `basename` tool to remove them (or at least what looks like one). – ctrl-alt-delor Aug 06 '20 at 14:51
  • Note your code have many bugs, you need to use the quotes. E.g. `<` and `>` have special meaning in the shell. You will get some very unexpected behavior. – ctrl-alt-delor Aug 06 '20 at 14:52
  • from the `basename` man page: "Print NAME with any leading directory components removed. If specified, also remove a trailing SUFFIX." – ctrl-alt-delor Aug 06 '20 at 15:49
  • `basename "$(basename "$fileName" .sh)" .txt` — OK this dose not scale to any "extension". — use `sed`. – ctrl-alt-delor Aug 06 '20 at 19:51

1 Answers1

4

Use sed to remove everything after and including the first dot:

:nnoremap yn :!sed "s/\..*//"<<<%c|xclip -selection clipboard %i<cr>

If your shell does not support herestrings (<<<), use printf (Why is printf better than echo?) to pipe into sed:

:nnoremap yn :!printf '%%s' %c|sed "s/\..*//"|xclip -selection clipboard %i<cr>

Vifm processes the macros before passing the command to the shell. %c becomes the file name (properly escaped); %s would also be substituted, thus %%s is needed so that printf gets a %s. Likewise, <cr> is the "Enter equivalent" and is required, otherwise the command whole simply pops up in the command-line.

This has been tested. Even the exquisitely named file t*.a .<!e>& passed the test, the clipboard gets t*.

If you want to remove everything after and including the last dot (not the first), use sed "s/\.[^.]*$//".


To display the copied string in the statusbar, repeat the command up to sed and use the %S macro.

:nnoremap yn :!printf '%%s' %c|sed "s/\..*//"|xclip -selection clipboard %i<cr>:!printf '%%s' %c|sed "s/\..*//;s/$/ is yanked to clipboard/" %S<cr>
Quasímodo
  • 18,625
  • 3
  • 35
  • 72