Home Download Buy Blog Forum Support

Straighten Quotes option?

Straighten Quotes option?

Postby funops on Mon Oct 21, 2013 7:28 pm

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
funops
 
Posts: 1
Joined: Mon Oct 21, 2013 7:23 pm

Re: Straighten Quotes option?

Postby qgates on Tue Oct 22, 2013 1:23 pm

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:
Code: Select all
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, "\'")


3. Create another file in there called "Main.sublime-menu" and paste in the following:
Code: Select all
[
   {
       "id": "edit",
       "children":
       [
           {"id": "wrap"},
           { "command": "straight_quotes" }
       ]
   }
]


4. 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 :)
qgates
 
Posts: 209
Joined: Mon Nov 05, 2012 4:33 pm

Re: Straighten Quotes option?

Postby MKANET on Wed Oct 23, 2013 6:03 pm

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!!
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: Straighten Quotes option?

Postby zambucca on Sat Oct 26, 2013 12:47 am

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. :)

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. :(

Thank you for sharing the code.

Samantha
zambucca
 
Posts: 1
Joined: Sat Oct 26, 2013 12:31 am

Re: Straighten Quotes option?

Postby qgates on Sat Oct 26, 2013 3:16 am

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 :)
qgates
 
Posts: 209
Joined: Mon Nov 05, 2012 4:33 pm

Re: Straighten Quotes option?

Postby MKANET on Sun Nov 03, 2013 3:27 am

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: Select all
$ints = @( 1, 2, 3, 4, 5)

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


...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
with -

I think after I add this fix, I'll rename this menu option to "WebCode Normalize" :)
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: Straighten Quotes option?

Postby MKANET on Tue Nov 05, 2013 6:29 pm

Anyone? I'm guessing anyone who knows python would know this. Unfortunately, I dont.
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: Straighten Quotes option?

Postby quodlibet on Tue Nov 05, 2013 9:33 pm

Add the following lines to the plugin above:

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


Untested, but surely you get the gist? Find this, replace that.
quodlibet
 
Posts: 525
Joined: Sat Jun 18, 2011 5:53 pm

Re: Straighten Quotes option?

Postby MKANET on Sat Nov 09, 2013 11:27 pm

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!

quodlibet wrote:Add the following lines to the plugin above:

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


Untested, but surely you get the gist? Find this, replace that.
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: Straighten Quotes option?

Postby citronica on Fri Aug 29, 2014 10:28 pm

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:
Code: Select all
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/
citronica
 
Posts: 1
Joined: Fri Aug 29, 2014 10:19 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 7 guests