Replacing quote marks around strings in Vim?

前端 未结 8 1587
遇见更好的自我
遇见更好的自我 2021-01-30 11:09

I have something akin to and need to change the single quotation marks to double quotation marks. I tried :s/\\\'.*\\\'/\

相关标签:
8条回答
  • 2021-01-30 11:33

    There's also surround.vim, if you're looking to do this fairly often. You'd use cs'" to change surrounding quotes.

    0 讨论(0)
  • 2021-01-30 11:45

    You need to use groupings:

    :s/\'\(.*\)\'/\"\1\"
    

    This way argument 1 (ie, \1) will correspond to whatever is delimited by \( and \).

    0 讨论(0)
  • 2021-01-30 11:46

    You've got the right idea -- you want to have "\1" as your replace clause, but you need to put the "Hello There" part in capture group 1 first (0 is the entire match). Try:

    :%/'\(.*\)'/"\1"

    0 讨论(0)
  • 2021-01-30 11:52

    You need to put round brackets around the part of the expression you wish to capture.

    s/\'\(.*\)\'/"\1"/
    

    But, you might have problems with unintentional matching. Might you be able to simply replace any single quotes with double quotes in your file?

    0 讨论(0)
  • 2021-01-30 11:56

    Just an FYI - to replace all double quotes with single, this is the correct regexp - based on rayd09's example above

    :%s/"\([^"]*\)"/'\1'/g
    
    0 讨论(0)
  • %s/'\([^']*\)'/"\1"/g

    You will want to use [^']* instead of .* otherwise

    'apples' are 'red' would get converted to "apples' are 'red"

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