Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does anyone know the vi command to replace curly quotes with straight quotes?

507 views
Skip to first unread message

harry newton

unread,
Oct 9, 2017, 3:36:22 AM10/9/17
to
Does anyone know the vi command to replace curly quotes with straight
quotes?

This is one of the many ways in vi to replace "x" and "y" with "z":
<Escape>:%s/[x,y]/z/gc

Which, in my vi-speak, interprets to something like:
<Escape>:%s/ => In command mode, search the whole file for
[x,y] => either "x" or "y"
/z/ => and replace what it found with "z"
gc => doing so for every instance found in each line & confirming each

Do you know how to identify, in vi, the "66" & "99" curly quotes?
<https://practicaltypography.com/straight-and-curly-quotes.html>

Here is a sample text file: <https://www.sendspace.com/file/bhgsl8>
Which is pictured here: <http://wetakepic.com/images/2017/10/09/sample.jpg>

--
Yes, Marek, I'm on both Linux & on Windows, but I'm using "vi" (actually
gvim) on both - and where only the Linux users really know vi well.

harry newton

unread,
Oct 9, 2017, 4:06:56 AM10/9/17
to
> Do you know how to identify, in vi, the "66" & "99" curly quotes?

Apparently, I'm told the opening "66" curly quotes is decimal character
codes 147 and the closing "99" curly quote is decimal character code 148
while the straight doublequote is apparently character code 34.

Does that help?

All I want is to replace all curly quotes in the following vi text file
with straight quotes so that they show up in vi NOT as a black box.
<https://pastebin.com/BFRYNs1c>

harry newton

unread,
Oct 9, 2017, 5:43:54 AM10/9/17
to
> All I want is to replace all curly quotes ...

I just found how to figure out the non-printable character in vi!
<https://durgaprasad.wordpress.com/2007/09/25/find-replace-non-printable-characters-in-vim/>
<https://vi.stackexchange.com/questions/13379/how-to-interpret-ascii-codes-returned-by-ga-command>

All I needed to do in vi to figure out what the non-printing characters
were was to sidle my cursor up to the non-printable character and just
press "ga" as shown in this screenshot below.
<http://wetakepic.com/images/2017/10/09/hex.jpg>

The "66" curly quotes showed up as ~S, ^S, [147], Hex 93, Octal 223
The "99" curly quotes showed up as ~T, ^T, [148], Hex 94, Octal 224

The funny thing is that this works fine individually:
:%s/\%x93/"/g (search for hex93 and replace it with straight quotes)
:%s/\%x94/"/g (search for hex94 and replace it with straight quotes)

But this syntax isn't working for the whole file yet:
:%s/[\%x93,\%x94]/"/g

For some reason, that syntax only works on the current line. :(

I'm working on why that multiple-search-and-replace syntax failed.

harry newton

unread,
Oct 9, 2017, 7:03:57 AM10/9/17
to
> I'm working on why that multiple-search-and-replace syntax failed.

I don't know why the [\%d,\%d] syntax failed, but I found another way to do
it by using [control+-x,control+-y] instead.
<http://wetakepic.com/images/2017/10/09/solved.jpg>

Here is a summary of the problem set and the solution:

Good: (") <== straight quotes
Good: (") <== straight quotes
Good: (") <== straight quotes
Bad: (+IBw-)(+IB0-) <== curly quotes
Bad: (+IBw-)(+IB0-) <== curly quotes
Bad: (+IBw-)(+IB0-) <== curly quotes

:set encoding=cp1252 --> tells vim to use Windows-1252 character encoding
:set coding=utf-8 --> tells vim to return to UTF-8 encoding.

Typing "ga":
The '66' curly quotes show up as ~S, ^S, [147], Hex 93, Octal 223
The '99' curly quotes show up as ~T, ^T, [148], Hex 94, Octal 224

Typing \%d147 & \%d148 works alone but not in a dual search:
:s/\%d147/"/g ==> replaces all '66' curly quotes with straight quotes
:s/\%d148/"/g ==> replaces all '99' curly quotes with straight quotes
But they don't work together for some reason:
:s/[x,y]/z/g ==> works fine to replace x and/or y with z throughout the file
:s/[\%d147,\%d148]/"/g ==> fails to replace for every line of the file

Likewise:
:s/x\|y/z/g ==> works fine to replace x and/or y with z throughout the file
:s/\%d147\|\%d148/"/g ==> fails to replace for every line of the file

Typing "vy" will copy the non-printing character into the vi register.
Control +- V +- [char] = paste works on Linux (use Control+-Q for Windows)
:%s/[control+-v+-147,control+-v+-148]/"/g ==> replaces with straight quotes on Linux
:%s/[control+-q+-147,control+-q+-148]/"/g ==> replaces with straight quotes on Windows
<http://wetakepic.com/images/2017/10/09/solved.jpg>

Marek Novotny

unread,
Oct 9, 2017, 11:22:21 AM10/9/17
to
Start with this...

:%s///g

Fill in the codes you want to replace...

[\x93\x94]

The whole thing would look like this...

:%s/[\x93\x94]/"/g

And then this one...

:%s/[\x91\x92]/'/g

--
Marek Novotny
https://github.com/marek-novotny

harry newton

unread,
Oct 9, 2017, 12:17:16 PM10/9/17
to
He who is Marek Novotny said on Mon, 09 Oct 2017 10:22:16 -0500:

> Start with this...
>
>:%s///g
>
> Fill in the codes you want to replace...
>
> [\x93\x94]
>
> The whole thing would look like this...
>
>:%s/[\x93\x94]/"/g
>
> And then this one...
>
>:%s/[\x91\x92]/'/g

Thanks Marek, that vim syntax worked to replace smartquote digraphs.
:diagraphs

I know how to do a search & replace but I had entered in the backslash
thinking that the backslash was needed to delimit the percent sign which I
also had thought was needed!

This is a normal search and replace for multiple items:
:%s/[x,y]/z/g => search for x and/or y, and replace all with z

Given the "ga" command tells me the following decimal values:
'66' smartquote is decimal 147 (aka %d147)
'99' smartquote is decimal 148 (aka %d148)

This worked for entering the curly quote digraphs:
:%s/\%d147/"/g ==> replaces all '66' curly quotes with straight quotes
:%s/\%d148/"/g ==> replaces all '99' curly quotes with straight quotes

And this worked for entering the curly quote digraphs:
:%s/[control+q+147,control+q+148]/"/g ==> for Windows
:%s/[control+v+147,control+v+148]/"/g ==> for Linux

But this did not work:
:%s/[\%d147,\%d148]/"/g ==> fails to replace for every line of the file

I didn't realize the digraph percent sign was not always needed!
:%s/[\d147,\d148]/"/g

So the syntax problem of replacing curly quotes is now solved!
The same method can be used to replace any non-printable character.

Thanks!

harry newton

unread,
Oct 9, 2017, 2:24:57 PM10/9/17
to
He who is harry newton said on Mon, 9 Oct 2017 16:17:14 +0000 (UTC):

> So the syntax problem of replacing curly quotes is now solved!
> The same method can be used to replace any non-printable character.

> SOLVED!
> <http://wetakepic.com/images/2017/10/09/samplede854.jpg>

SOLVED ANOTHER WAY!
<http://wetakepic.com/images/2017/10/09/digraphs98b40.jpg>

I was trying to show the Euro symbol in gvim, and when I figured it out
(using my newly found keyword "digraph"), I found yet another and far
easier way to solve the smartquote digraphs being non-viewable characters!
<http://vimdoc.sourceforge.net/htmldoc/digraph.html>

All I needed to do, in gvim, was change the font from the default to
something that recognized the Euro (and smartquote) digraphs, such as
"Courier New".
<http://vim.1045645.n5.nabble.com/Solved-Display-Euro-character-in-gvim-7-2-td1176874.html>

Our combined tribal knowledge now records TWO answers to the OP question!

Marek Novotny

unread,
Oct 9, 2017, 2:52:06 PM10/9/17
to
On 2017-10-09, harry newton <ha...@is.invalid> wrote:
Or if you know the encoding type:

:e ++enc=cp1252

harry newton

unread,
Oct 9, 2017, 3:50:34 PM10/9/17
to
He who is Marek Novotny said on Mon, 09 Oct 2017 13:52:00 -0500:

> Or if you know the encoding type:
>
>:e +-+-enc=cp1252

I don't understand encoding but I do know what doesn't work empirically.
<http://wetakepic.com/images/2017/10/09/encoding.jpg>

I had tried encoding loooooong ago, and I found encoding does nothing
(AFAICT).

Here's what I just tried:
1. I created an empty text file.
2. I checked the default encoding, which turned out to be "latin1".
:encoding
3. I pasted a problematic set of smartquote text from:
https://practicaltypography.com/straight-and-curly-quotes.html
Namely this section:
Curly quotes are the quo+AK0-ta+AK0-tion marks used in good ty+AK0-pog+AK0-ra+AK0-phy.
There are four curly quote char+AK0-ac+AK0-ters: the open+AK0-ing sin+AK0-gle quote
(+IBg-), the clos+AK0-ing sin+AK0-gle quote (+IBk-), the open+AK0-ing dou+AK0-ble quote (+IBw-),
and the clos+AK0-ing dou+AK0-ble quote (+IB0-).
4. I noted that, in vi, the curly quotes show up as black boxes.
5. I changed the vi encoding to cp1252
:set encoding=cp1252
6. Nothing changed, even though it said "translated".
7. I doublechecked the encoding and it was now cp1252
:encoding
8. So I gave up on encoding.
<http://wetakepic.com/images/2017/10/09/encoding.jpg>

harry newton

unread,
Oct 9, 2017, 4:47:20 PM10/9/17
to
He who is harry newton said on Mon, 9 Oct 2017 07:36:20 +0000 (UTC):

> Does anyone know the vi command to replace curly quotes with straight
> quotes?

> Here is a sample text file: <https://www.sendspace.com/file/bhgsl8>
> Which is pictured here: <http://wetakepic.com/images/2017/10/09/sample.jpg>

Solved. Simply by changing the font from the default to "Courier New"!

I'm confused because the encoding test failed empirically and the
font-changing test worked empirically.

Here's my report on how I tested encoding, but failed:
<http://wetakepic.com/images/2017/10/09/curly_quotes.jpg>

I don't understand encoding but I do know what doesn't work empirically.
<http://wetakepic.com/images/2017/10/09/encoding.jpg>

I had tried encoding loooooong ago, and I found encoding does nothing
(AFAICT).

Here's what I just tried:
1. I created an empty text file.
2. I checked the default encoding, which turned out to be "latin1".
:encoding
3. I pasted a problematic set of smartquote text from:
https://practicaltypography.com/straight-and-curly-quotes.html
Namely this section:
http://wetakepic.com/images/2017/10/09/curly_quotes.jpg
4. I noted that, in vi, the curly quotes show up as black boxes.
5. I changed the vi encoding to cp1252
:set encoding=cp1252
6. Nothing changed, even though it said "translated".
7. I doublechecked the encoding and it was now cp1252
:encoding
8. So I gave up on encoding.
<http://wetakepic.com/images/2017/10/09/encoding.jpg>


For fonts, here's my initial test (from the OP), which works with Courier:

1. I created an empty text file.
2. I checked the default font which was "Fixedsys" (Regular, 9 point).
gvim: Edit > Select font
:set guifont
Reported: guifont=Fixedsys:h9:cANSI:qDRAFT
3. I pasted a problematic set of smartquote text from:
https://practicaltypography.com/straight-and-curly-quotes.html
Namely this section:
<http://wetakepic.com/images/2017/10/09/curly_quotes.jpg>
4. I noted that, in vi, the curly quotes show up as black boxes.
5. I changed the vi font to "courier new"
gvim: Edit > Select font > Courier New (Regular, 9 point).
:set guifont
Reported: guifont=Courier_New:h9:cANSI:qDRAFT
6. Bingo! Problem solved!
All the curly quotes now show up.

FAILS: Changing encoding from "latin1" to "cp1252".
WORKS: Changing font from "Fixedsys" to "Courier_New".

While I admit ignorance of encoding, I will only assert strongly that
changing the character encoding did nothing while just changing the font
fixed everything.

Why?
I do not know why.
0 new messages