假设当前缓冲区是一个打开编辑的文件,那么:e不会显示E32: No文件名。
我想要扯掉其中一个或全部:
文件名与状态行中显示的完全一致,例如~\myfile.txt 文件的完整路径,例如c:\foo\bar\myfile.txt 只是文件名,例如myfile.txt
假设当前缓冲区是一个打开编辑的文件,那么:e不会显示E32: No文件名。
我想要扯掉其中一个或全部:
文件名与状态行中显示的完全一致,例如~\myfile.txt 文件的完整路径,例如c:\foo\bar\myfile.txt 只是文件名,例如myfile.txt
当前回答
我把它放在我的vimc中:
nnoremap yl :let @" = expand("%:p")<cr>
其他回答
如果你想把当前的缓冲区文件名放在你的系统级剪贴板中,尝试将寄存器更改为@+:
" relative path
:let @+ = expand("%")
" full path
:let @+ = expand("%:p")
" just filename
:let @+ = expand("%:t")
编辑20140421: 我经常使用这些,所以我创建了一些快捷方式。Linux Vims的操作显然与Mac Vims略有不同,因此也有一个特殊情况。如果您在~/.vimrc中放入以下代码:
" copy current file name (relative/absolute) to system clipboard
if has("mac") || has("gui_macvim") || has("gui_mac")
" relative path (src/foo.txt)
nnoremap <leader>cf :let @*=expand("%")<CR>
" absolute path (/something/src/foo.txt)
nnoremap <leader>cF :let @*=expand("%:p")<CR>
" filename (foo.txt)
nnoremap <leader>ct :let @*=expand("%:t")<CR>
" directory name (/something/src)
nnoremap <leader>ch :let @*=expand("%:p:h")<CR>
endif
" copy current file name (relative/absolute) to system clipboard (Linux version)
if has("gui_gtk") || has("gui_gtk2") || has("gui_gnome") || has("unix")
" relative path (src/foo.txt)
nnoremap <leader>cf :let @+=expand("%")<CR>
" absolute path (/something/src/foo.txt)
nnoremap <leader>cF :let @+=expand("%:p")<CR>
" filename (foo.txt)
nnoremap <leader>ct :let @+=expand("%:t")<CR>
" directory name (/something/src)
nnoremap <leader>ch :let @+=expand("%:p:h")<CR>
endif
然后,例如<leader>cf将复制当前缓冲区的相对路径(默认的前导是反斜杠(\))。我经常使用它们在文件上运行命令或在命令行上执行其他操作。我并不经常使用最后一个文件名/目录名。
您可以考虑更直观的映射,例如<leader>cfr表示相对映射,<leader>cfa表示绝对映射,<leader>cff表示文件名映射,<leader>cfd表示目录映射。
几乎就是你想要的,它可能会这样做:在INSERT模式下,Ctrl+R %将当前文件名拉到你所在的位置(命令提示符,编辑缓冲区,…)更多信息请看Vim提示。
如果您希望简单地拉出当前打开的文件名的路径,那么只需按:
step1: ctrl + g[你会在窗口底部看到整个根路径] Step2:用鼠标选择路径 用鼠标中间的滚轮粘贴
以下是我的解决方案:
" 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>
" }}}
我把它放在我的vimc中:
nnoremap yl :let @" = expand("%:p")<cr>