Is there different between single quote and double quote in vim command mode?

笑着哭i 提交于 2020-01-02 15:45:49

问题


In my vim, I can use :%!sed "s/^/ /", got the wrong output when I use :%!sed 's/^/ /' .

sed: -e expression #1, char 0: no previous regular expression

Is there differences between single quote and double quote in vim command mode? In my sed, single quote is the same as double quote.

$ echo "wha012" | sed  's/w/haha/'
hahaha012

$ echo "wha012" | sed  "s/w/haha/"
hahaha012

my system is xp+vim 7.3 for windows.

In my system:
[1] "c://cygwin/bin/ash.exe"
[2] "c://cygwin/bin/bash.exe"
[3] "c://cygwin/bin/dash.exe"
[4] "c://cygwin/bin/sh.exe"

if i set set shell=\"c:\cygwin\bin\sh.exe"\ -f in _vimrc,i get the new wrong messages:

sed command can not found.


回答1:


Funny, when I try :%!sed "/^/ /" I get the same error message as when I use single quotes:

sed: 1: "/^/ /": invalid command code /

(This line replaces the content of my file.) I expect to get an error message there because, as @Birei pointed out, you left out the sed s command. This works as expected, with either single or double quotes:

:%!sed "s/^/ /"

@Birei is also right that you can use vim to do things like this, but I assume you have simplified the example from what you were really trying to do.

To answer the original question, Vim uses single quotes for literal strings. The only special character in a literal string is ' itself. Strings delimited with double quotes use \ to denote special character, such as `"\<Esc>".

:echo 'a''b' == "a'b"
:help expr-string
:help literal-string



回答2:


my system is xp+vim 7.3 for windows

By default Vim uses cmd.exe to run :! commands on Windows, which behaves differently with regard to quoting from the POSIX shell that your s/w/haha/ examples suggest you've been testing with. Try something like

:set shell=\"C:\path\to\sh.exe\"\ -f

to tell it to use your POSIX shell instead. Or if you're using cygwin then try the cygwin version of vim instead of the Windows native one.




回答3:


The difference is in the sed command, that lets interpolate variables when you execute it directly from the shell, like:

sed "s/$pattern/$replacement/"

but your problem is that you have to use a substitution command that begins with letter s, like:

:%!sed "s/^/ /"

Also you can have same behaviour inside vim without an external command, like:

:%s/^/ /


来源:https://stackoverflow.com/questions/20824878/is-there-different-between-single-quote-and-double-quote-in-vim-command-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!