vim - 如何轉義包含單引號和雙引號混合的檔案名稱?

vim - 如何轉義包含單引號和雙引號混合的檔案名稱?

假設我用這個創建一個檔案名稱:

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.gtk3bash。 Fedora 的vimgvim有同樣的問題。

在 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.

相關內容