12

I got this code snippet from a tutorial and I would like to search and replace all occurrences of “ and ”, with normal double quote ". How can I efficiently do it in Vim?

{
  “runtime”: {
  “DDP_DEFAULT_CONNECTION_URL”: “http://127.0.0.1:8100”
  },
  “import”: [
    “meteor-base@1.0.4”,
    “mongo@1.1.14”,
    “reactive-var@1.0.11”,
    “jquery@1.11.10”,
    “tracker@1.1.1”,
    “standard-minifier-css@1.3.2”,
    “standard-minifier-js@1.2.1”,
    “es5-shim@4.6.15”,
    “ecmascript@0.6.1”,
    “shell-server@0.2.1”
  ]
}

Thank you.

Jesus Nana
  • 121
  • 1
  • 5
  • 1
    1. Don't copy and paste, type. 2. You should probably report the issue to the persons in charge of the site on which you copied that snippet. 3. Just do a search/replace. – romainl Apr 13 '17 at 07:33

4 Answers4

10

You can show ascii code of character using ga in normal mode.
You can also put arbitrary utf8 code using Ctrl+vuhhhh for characters with code 0000 <= hhhh <= FFFF and Ctrl+vUhhhhhhhh for the rest in insert mode and command mode so when starting in normal mode you need to type, similar to @shash678 answer:

:%s/Ctrl+vu201c/"/g

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
mucka
  • 1,286
  • 3
  • 20
  • 36
  • E486: Pattern not found: %s//"/g – Jesus Nana Apr 13 '17 at 06:36
  • means Ctrl + v, you need to put `:%s/` then Ctrl + v (you will will be not able to see anything until you finish typing hex code), then `u` then `201c`, only after typing it all you will see your character `“`. – mucka Apr 13 '17 at 06:41
  • Got it thanks! Is there a way to do the search-replace on both quotes on one shot? something like %s/“|”/"/g – Jesus Nana Apr 13 '17 at 06:54
  • 2
    Yes, you can use regex-like search patterns, is this case `:%s/[”“]/"/g` – mucka Apr 13 '17 at 06:57
  • note for myself, press CTRL and v at the same time, type u201c, it will show as ", and type " as replacement – Gang Aug 21 '20 at 20:48
9

In command-line mode you can use the following command to find each occurrence of “ and ” (in all lines), and replace them with ":

:%s/[“”]/"/g 

EDIT:

To do this, (assuming you have the file open in vim):

1) hit the ESC key to ensure you are Normal mode.

2) hit : , you will notice : has appeared in box at the bottom of vim (that is the command line).

3) type the rest of the command for finding each occurrence of “ and ” (in all lines), and replacing both with ".

4) hit the ENTER key.

5) your done! The command line will say something like made 56 substitutions on 13 lines.

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • 1
    the problem is, how do I type that kind of quotes, on the command line? – Jesus Nana Apr 13 '17 at 06:15
  • not sure what kind of keyboard you're using, but on a standard US keyboard or macbook pro those special double quotes ( “ ” ) doesn't exist. – Jesus Nana Apr 13 '17 at 06:31
  • Oh I am on a macbook too, but I am using Windows, you can use Alt + 0147 to type the curly quotation mark `“`. On OSX you can do something similar use this link: http://www.wikihow.com/Type-Symbols-Using-the-ALT-Key and scroll down to mac. – Sash Sinha Apr 13 '17 at 06:39
  • Sorry, but that's inefficient way, I was looking for a "Vim way" to solve the problem. – Jesus Nana Apr 13 '17 at 06:46
  • 1
    @JesusNana You can also yank (vim's way of copying) the curly quotation mark from the file itself into the command line but I will let you research that for yourself :) http://stackoverflow.com/questions/3997078/how-to-paste-yanked-text-into-vim-command-line – Sash Sinha Apr 13 '17 at 06:49
2

To rephrase @mucka's answer:

:%s/[Ctrl+vu201cCtrl+vu201d]/"/g

The above is similar the command below, but it allows you to retrieve the special chars using hex code with Ctrl+v:

:%s/[“”]/"/g 

To show hex code any character in your file, move your cursor over that character in normal mode and execute ga.

konyak
  • 10,818
  • 4
  • 59
  • 65
0
  1. highlight the first smartquote before runtime, then hit e twice to select the other.
  2. press y to yank. You now have a yank register with both types of smart quotes in it.
  3. type :%s/[ and then press Ctrl+r and then " to paste the yanked text. Move backwards and remove the word runtime from the search, then move to the end and type ]/"/g and hit Enter
naught101
  • 18,687
  • 19
  • 90
  • 138