Skip to content

Instantly share code, notes, and snippets.

@sudharsans
Created March 10, 2018 21:10
Show Gist options
  • Save sudharsans/1aacd723868315d30dd31543d6081c36 to your computer and use it in GitHub Desktop.
Save sudharsans/1aacd723868315d30dd31543d6081c36 to your computer and use it in GitHub Desktop.
Sublime Plugin to convert frequent edits
[
{
"caption": "Convert",
"id": "convert",
"children":
[
{
"caption": "Convert",
"command": "convert"
},
{
"caption": "Undo Convert",
"command": "undo"
}
]
}
]
# Sublime Plugin created for stackoverflow answer.
# https://stackoverflow.com/questions/49213489/sublime-text-and-python-plugin-how-to-incorporate-a-python-code-with-a-for-loop
import sublime, sublime_plugin, re, string #import the required modules
class ConvertCommand(sublime_plugin.TextCommand): #create Text Command
def run(self, edit): #implement run method
for region in self.view.sel(): #get user selection
if not region.empty(): #if selection not empty then
s = self.view.substr(region) #assign s variable the selected region
replace = '\n'.join([' '.join(para.splitlines()) for para in s.split('\n\n')])
self.view.replace(edit, region, replace) #replace content in view
class UndoCommand(sublime_plugin.TextCommand): #create Text Command
def run(self, edit): #implement run method
for region in self.view.sel(): #get user selection
if not region.empty(): #if selection not empty then
s = self.view.substr(region) #assign s variable the selected region
replace = '\n'.split([' '.join(para.splitlines()) for para in s.split('\n\n')])
self.view.replace(edit, region, replace) #replace content in view
https://i.stack.imgur.com/kzvIn.png
Just save it in ~/Library/Application Support/Sublime Text 3/Packages/User/convert.py.
menu should be saved as context.sublime-menu in the same path
Select the text to Convert:
Right Click -> Select "Convert"
Right Click -> Select "Undo" to Undo convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment