Assuming the current buffer is a file open for edit, so :e
does not display E32: No file name
.
I would like to yank one or all of:
Here is my solution:
" filename / dirname of the current file {{{
" copy result to the system clipboard and echo the result
" the cb> prompt means the clipboard
" *f*ile *n*ame, ex. init.vim
map <Leader>fn :let @+ = expand("%:t") \| echo 'cb> ' . @+<CR>
" *f*ile *p*ath, ex. /home/user/nvim/init.vim
map <Leader>fp :let @+ = expand("%:p") \| echo 'cb> ' . @+<CR>
" *d*irectory *p*ath, ex. /home/user/nvim
map <Leader>dp :let @+ = expand("%:p:h") \| echo 'cb> ' . @+<CR>
" *d*irectory *n*ame, ex. nvim
map <Leader>dn :let @+ = expand("%:p:h:t") \| echo 'cb> ' . @+<CR>
" }}}
If you do :reg
you will see the name of the current file in the %
register. You can paste it with "%p
, for example.
If, like me, you often switch to the 'alternate' buffer, it is very handy that its full path-and-file-name are put in the #
register. You can paste it with "#p
, for example.
Note (just in case this is behaviour specific to my setup): I am using VIM 7.4.52 on Ubuntu 14.04.4 LTS.