假设我用这个创建一个文件名:
xb@dnxb:/tmp/test$ touch '"i'"'"'m noob.mp4"'
xb@dnxb:/tmp/test$ ls -1
"i'm noob.mp4"
xb@dnxb:/tmp/test$
然后vim .
进入 Netrw 目录列表。
" ============================================================================
" Netrw Directory Listing (netrw v156)
" /tmp/test
" Sorted by name
" Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special
" ==============================================================================
../
./
"i'm noob.mp4"
然后按Enter查看文件。类型:
:!ls -l %
它将显示错误:
xb@dnxb:/tmp/test$ vim .
ls: cannot access '/tmp/test/i'\''m noob.mp4': No such file or directory
shell returned 2
Press ENTER or type command to continue
我也尝试过:
[1] :!ls -l '%'
:
Press ENTER or type command to continue
/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file
shell returned 1
Press ENTER or type command to continue
[2] :!ls -l "%"
:
Press ENTER or type command to continue
/bin/bash: -c: line 0: unexpected EOF while looking for matching `''
/bin/bash: -c: line 1: syntax error: unexpected end of file
shell returned 1
Press ENTER or type command to continue
[3] :!ls -l expand("%")
:
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `ls -l expand(""i'm noob.mp4"")'
shell returned 1
Press ENTER or type command to continue
[4] !ls -l shellescape("%")
:
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `ls -l shellescape("/tmp/test/"i'm noob.mp4"")'
shell returned 1
Press ENTER or type command to continue
[5] !ls -l shellescape(expand("%"))
:
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `ls -l shellescape(expand("/tmp/test/"i'm noob.mp4""))'
shell returned 1
Press ENTER or type command to continue
我的最终目标是rsync
通过Ctrl+执行c,例如:
nnoremap <C-c> :!eval `ssh-agent -s`; ssh-add; rsync -azvb --no-t % [email protected]:/home/xiaobai/storage/
我的平台是Kali Linux的vim.gtk3
bash。 Fedora 的vim
也gvim
有同样的问题。
在 vim 中转义包含单引号和双引号的文件名的正确语法是什么?
[更新]
exec '!ls -l' shellescape(expand('%'))
可以工作,但我仍然不知道如何使rsync
上述工作。我不知道应该在哪里为这个更复杂的命令添加引号rsync
。
答案1
建立在通配符的答案使用文件名修饰符,:S
可以给你你想要的。根据文档 ( :h %:S
),
:S Escape special characters for use with a shell command (see
|shellescape()|). Must be the last one. Examples:
:!dir <cfile>:S
:call system('chmod +w -- ' . expand('%:S'))
使用你的例子:
$ touch '"I'\''m also a n00b.txt"'
$ ls
"I'm also a n00b.txt"
然后vim '"I'\''m also a n00b.txt"'
,瞧:
:!ls %:S
"I'm also a n00b.txt"
文件:S
名修饰符是在 Vim 7.4 中可用。
答案2
从:help filename-modifiers
:
The file name modifiers can be used after "%", "#", "#n", "<cfile>", "<sfile>",
"<afile>" or "<abuf>". ...
...
:s?pat?sub?
Substitute the first occurrence of "pat" with "sub". This
works like the |:s| command. "pat" is a regular expression.
Any character can be used for '?', but it must not occur in
"pat" or "sub".
After this, the previous modifiers can be used again. For
example ":p", to make a full path after the substitution.
:gs?pat?sub?
Substitute all occurrences of "path" with "sub". Otherwise
this works like ":s".
因此,不仅仅是处理双引号或单引号,让我们只是反斜杠转义一切异常:
:!ls -l %:gs/[^0-9a-zA-Z_-]/\\&/
与您提供的测试文件名完美配合。
要使用您可能需要的绝对路径rsync
,您可以:p
在末尾添加:
:!ls -l %:gs/[^0-9a-zA-Z_-]/\\&/:p
实际上,如果您对每个字符进行反斜杠转义,它也可以正常工作,并且键入的时间会更短:
:!ls -l %:gs/./\\&/:p
所以,在你的rsync
命令下,而不是%
使用%:gs/./\\&/:p
.