Yank file name / path of current buffer in Vim

后端 未结 8 1364
死守一世寂寞
死守一世寂寞 2020-12-07 07:26

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:

相关标签:
8条回答
  • 2020-12-07 08:02

    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>
    " }}}
    
    0 讨论(0)
  • 2020-12-07 08:03

    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.

    0 讨论(0)
提交回复
热议问题