Sublime Forum

Straighten Quotes option?

#1

Hello. I’m pretty new to Linux and it was recommended to give Sublime Text 2 a try for my text editing needs. I downloaded/installed it in my Ubuntu 13.10 system and it seems to work great so I’m now on the learning curve. I come from a Mac and Bare Bones’ TextWrangler. There is an option/feature in TextWrangler that I use extensively due to the nature of many text docs I receive:

Straighten Quotes

Text menu > Straighten Quotes

This quickly converts the curly quotes (Smart Quotes) within the entire document to straight quote marks, be it the double quote or single quote mark.

I was looking for something similar in Sublime Text 2 but didn’t find anything. Searching the forums I found some discussion and programming suggestions so I thought I’d first ask before taking that on, is there a feature within Sublime Text 2 that will convert quote marks to straight quote marks?

Thanks,
Bob

0 Likes

How to Copynpaste ″ character from web browser?
Turn straight quotes into smart quotes?
#2

Welcome Bob. Sublime Text doesn’t have this baked in, but the magic is in how easy it is to extend its features via plugins. This one’s especially easy to do via some regex’s:

  1. Make a folder under your Data/Packages folder, call it “Straight Quotes”

  2. Create a file in there called “Straight Quotes.py” and paste in the following:

import sublime, sublime_plugin

class StraightQuotesCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		for rgn in self.view.find_all("“”]"):
			self.view.replace(edit, rgn, "\"")
		for rgn in self.view.find_all("‘’]"):
			self.view.replace(edit, rgn, "\'")
  1. Create another file in there called “Main.sublime-menu” and paste in the following:

	{
	    "id": "edit",
	    "children":
	    
	        {"id": "wrap"},
	        { "command": "straight_quotes" }
	    ]
	}
]
  1. Done. You now have a new option under the edit menu that will straighten quotes for the current file!

You could also easily bind it to a keyboard shortcut, or add it to the command palette if you wish :smile:

0 Likes

#3

Wow, I’m surprised there isn’t a package already for for this. I just made my own. This forum thread really saved me! Thank you so much!!

0 Likes

#4

Greetings. This looks very handy indeed for some of the copy/paste Word documents I have to work with. I am running into a slight glitch in that I followed the instructions exactly and the Straight Quotes option shows up in my Edit menu when I have text loaded but the Straight Quotes menu item is grayed out and thus not clickable.

As this is my first added plugin, does it have to be initialized or the like before it can be used? I’ve been reading up on plugins for ST and that doesn’t seem to be the case so I’m a little lost with getting off the ground working with plugins. Suggestions welcomed. :smile:

I am using ST on Ubuntu 13.10 and it otherwise works great so I believe it is installed correctly. And I tried the same, installing it on my iMac (running the new Mavericks OS) with the same results; i.e., Straight Quotes appears in the Edit menu but is grayed out. :frowning:

Thank you for sharing the code.

Samantha

0 Likes

#5

If it’s greyed out it can’t find the command, which probably means you’ve put the .py file in a subdirectory of your User (./Packages/User) folder, and not a direct subdirectory of ./Packages. For some reason though, sublime-menu files are picked up from subdirectories of User. Weird I know.

So, you need the two files either in a direct subdirectory of ./Packages, or in the Packages/User folder, but not in a subdirectory of Packages/User.

So on Linux, the directory will be directly under ~/.config/sublime-text-?/Packages
or you can dump the files into ~/.config/sublime-text-?/Packages/User (with no subdirectory)
where ? is 2 or 3

@MKANET, thanks for the thanks :smile:

0 Likes

#6

I’ve been using this new menu option regularly. I noticed that there is one more issue when copying scripts from a web browser. The minus symbol is also incompatible. So, the below lines of code:

[code]$ints = @( 1, 2, 3, 4, 5)

for ($i=0; $i -le $ints.Length – 1; $i++)
{Write-Host $ints$i]}[/code]

…will cause this kind of an error:
At Z:\Desktop\dev\temp.ps1:3 char:34

  • for ($i=0; $i -le $ints.Length – 1; $i++)

Could someone please kindly tell me how to enhance the Staighten Quotes code so that it automatically replaces
[size=200]–[/size] with [size=200]-[/size]

I think after I add this fix, I’ll rename this menu option to “WebCode Normalize” :smile:

0 Likes

#7

Anyone? I’m guessing anyone who knows python would know this. Unfortunately, I dont.

0 Likes

#8

Add the following lines to the plugin above:

      for rgn in self.view.find_all("–"):
         self.view.replace(edit, rgn, "-")

Untested, but surely you get the gist? Find this, replace that.

0 Likes

#9

Thank you. I now know how to add more things to replace!

Could you also please tell me how to rename the plugin and menu command to: “Web Code Normalize”? I already tried renaming, but every time I did it the menu item would be greyed out/disabled.

Thank again, you’re very helpful!

[quote=“quodlibet”]Add the following lines to the plugin above:

      for rgn in self.view.find_all("–"):
         self.view.replace(edit, rgn, "-")

Untested, but surely you get the gist? Find this, replace that.[/quote]

0 Likes

How to turn straight to smart quotes?
#10

I tried adding the custom plugin described above to ST3 but ran into the same problem as zambucca. I am no programmer but I did a little Googling and got it to work. The code for main.sublime-menu remains the same, but here’s the new code for Straight Quotes.py.

Paste into Straight Quotes.py:

import sublime
import sublime_plugin

class StraightQuotesCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      for rgn in self.view.find_all("“”]"):
         self.view.replace(edit, rgn, "\"")
      for rgn in self.view.find_all("‘’]"):
         self.view.replace(edit, rgn, "\'")

(The difference between the ST2 and ST3 versions is the import syntax.)

I am running Mac OS Mavericks and saved the Straight Quotes directory (with the two files inside) to /Users/[username]/Library/Application Support/Sublime Text 3/Packages/

0 Likes

#11

That seems strange. I’m no Python programmer, but the Python 3 docs say that

import sublime, sublime_plugin

is exactly the same as

import sublime import sublime_plugin

See docs.python.org/3/reference/sim … tml#import:

0 Likes

#12

I’m trying to get this customization to also replace all occurrences of the following string:
"

with
"

I tried adding the below code to the script; however, I’m not sure how to tell it to search for the entire string between the two quotes (instead of each character in between the quotes). Thanks very much in advance!

[code] for rgn in self.view.find_all("""):
self.view.replace(edit, rgn, “”")

[/code]

0 Likes

#13

Same here! This almost worked for me

I don’t know if it’s a issue with the version of Sublime I have but I had to use the code below to get it to work.

import sublime, sublime_plugin    
class StraightQuotesCommand(sublime_plugin.TextCommand):
       def run(self, edit):
          for rgn in self.view.find_all("[“”]"):
             self.view.replace(edit, rgn, "\"")
          for rgn in self.view.find_all("[‘’]"):
             self.view.replace(edit, rgn, "\'")

The missing [ around the quotations caused it not run.

0 Likes

#14

The issue isn’t the import statement, it’s the spacing. The spacing is important for python to work. Also, Main.sublime-menu needs to be valid json and both brackets are required. Here are final ones that work for me.

Main.sublime-menu:

[
  {
    "id": "edit",
    "children": [
      {
        "id": "wrap"
      },
      {
        "command": "straight_quotes"
      }
    ]
  }
]

Straight Quotes.py:

import sublime, sublime_plugin    
class StraightQuotesCommand(sublime_plugin.TextCommand):
       def run(self, edit):
          for rgn in self.view.find_all("[“”]"):
             self.view.replace(edit, rgn, "\"")
          for rgn in self.view.find_all("[‘’]"):
             self.view.replace(edit, rgn, "\'")
0 Likes