2

Is there a way to only copy/yank the file name to clipboard? According to these sources:
Yank file name / path of current buffer in Vim
How can I expand the full path of the current file to pass to a command in Vim?
Get name of the current file in Vim
it is clear that expand() is the function to use and the following could be defined:

" Full path
nnoremap <leader><leader>f :let @* = expand("%:p")<CR>
" File name, with extension.
nnoremap <leader><leader>n :let @* = expand("%:t")<CR>
" Extension.
nnoremap <leader><leader>e :let @* = expand("%:t:e")<CR>
" Parent directory, full.
nnoremap <leader><leader>p :let @* = expand("%:p:h")<CR>

However, I did not find a way to copy the file name only. I mean file name without extension. Is this possible at all? I tried "%" and "%:. Neither worked.

Community
  • 1
  • 1
llinfeng
  • 1,316
  • 1
  • 15
  • 39
  • 2
    What do you want to do on Unix if the file doesn't have an extension? What if the filename *is* the extension (e.g. the file is named `.txt`)? – Kevin Dec 12 '14 at 16:57
  • @Kevin: Well, since Vim is case sensitive, I will reserve `N` for copying the file name with extension, using `"%:t"`. This option will copy the file name that has not extension on it. (Like `_vimrc` on my Window machine) – llinfeng Dec 12 '14 at 19:45

1 Answers1

7
expand("%:t:r")

does what you want but Kevin raised very good points.

See :help filename-modifiers.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thank you for pointing this out. For @Kevin's point, as I've tested on Windows, `expand("%:t")` will copy the file name of `_vimrc`, which exactly what he has desired. For Linux, I believe so. – llinfeng Dec 12 '14 at 19:30