├── toggle_quotes.py ├── Default.sublime-keymap └── README.md /toggle_quotes.py: -------------------------------------------------------------------------------- 1 | import sublime_plugin 2 | import re 3 | from sublime import Region 4 | 5 | re_quotes = re.compile("^(['\"`])(.*)\\1$") 6 | quoteList = ['\'', '"', '`'] 7 | 8 | 9 | class ToggleQuotesCommand(sublime_plugin.TextCommand): 10 | def run(self, edit, **kwargs): 11 | v = self.view 12 | if v.sel()[0].size() == 0: 13 | v.run_command('expand_selection', {'to': 'scope'}) 14 | 15 | for sel in v.sel(): 16 | text = v.substr(sel) 17 | res = re_quotes.match(text) 18 | if not res: 19 | # the current selection doesn't begin and end with a quote. 20 | # let's expand the selection one character each direction and try again 21 | sel = Region(sel.begin() - 1, sel.end() + 1) 22 | text = v.substr(sel) 23 | res = re_quotes.match(text) 24 | if not res: 25 | # still no match... skip it! 26 | continue 27 | oldQuotes = res.group(1) 28 | if 'key' in kwargs: 29 | newQuotes = kwargs['key'] 30 | else: 31 | newQuotes = quoteList[(quoteList.index(oldQuotes) + 1) % len(quoteList)] 32 | text = res.group(2) 33 | text = text.replace(newQuotes, "\\" + newQuotes) 34 | text = text.replace("\\" + oldQuotes, oldQuotes) 35 | text = newQuotes + text + newQuotes 36 | v.replace(edit, sel, text) 37 | 38 | 39 | # 'te\'st' 40 | # 'te"st' 41 | # 'test' 42 | # 'te\'st' 43 | # 'te"st' 44 | # "te\"st" 45 | # "te'st" 46 | # `te"s't` 47 | -------------------------------------------------------------------------------- /Default.sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["'"], "command": "toggle_quotes", "args": { "key": "'" }, "context": 3 | [ 4 | { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, 5 | { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }, 6 | { "key": "text", "operator" : "regex_contains", "operand": "^(\".*\"|`.*`)$", "match_all": true} 7 | ] 8 | }, 9 | { "keys": ["\""], "command": "toggle_quotes", "args": { "key": "\"" }, "context": 10 | [ 11 | { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, 12 | { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }, 13 | { "key": "text", "operator" : "regex_contains", "operand": "^('.*'|`.*`)$", "match_all": true} 14 | ] 15 | }, 16 | { "keys": ["`"], "command": "toggle_quotes", "args": { "key": "`" }, "context": 17 | [ 18 | { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, 19 | { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }, 20 | { "key": "text", "operator" : "regex_contains", "operand": "^(\".*\"|'.*')$", "match_all": true} 21 | ] 22 | }, 23 | { "keys": ["ctrl+'"], "command": "toggle_quotes", "context": 24 | [ 25 | { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, 26 | { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true } 27 | ] 28 | }, 29 | { "keys": ["ctrl+'"], "command": "toggle_quotes", "context": 30 | [ 31 | { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true, "match_all": true }, 32 | { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, 33 | { "key": "selector", "operator": "equal", "operand": "string", "match_all": true} 34 | ] 35 | } 36 | ] 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ToggleQuotes is a [Sublime Text][sublime] plugin which lets you easily toggle quote marks in your code. 2 | 3 | ## Installation ## 4 | 5 | ### With Package Control ### 6 | 7 | If you have the [Package Control][package_control] package installed, you can install ToggleQuotes from inside Sublime Text itself. Open the Command Palette and select "Package Control: Install Package", then search for ToggleQuotes and you're done! 8 | 9 | ### Without Package Control ### 10 | 11 | Go to your Sublime Text 2/3 Packages directory and clone the repository using the command below: 12 | 13 | git clone https://github.com/spadgos/sublime-ToggleQuotes.git ToggleQuotes 14 | 15 | Don't forget to keep updating it, though! 16 | 17 | ## Example ## 18 | 19 | Make one or more selections of strings including their quotes and then press the opposite quote mark. That is, if you have `"test"` press `'`, if you have `'test'`, press `"`. 20 | 21 | ```javascript 22 | // With this selected: 23 | "test" 24 | // Press: ' 25 | // Result is: 26 | 'test' 27 | ``` 28 | 29 | Internal quotes are escaped, and already-escaped quotes are unescaped for you: 30 | 31 | ```javascript 32 | // With this selected: 33 | "hello 'world'" 34 | // Press: ' 35 | // Result is: 36 | 'hello \'world\'' 37 | 38 | // With this selected: 39 | 'hello \'world\'' 40 | // Press: " 41 | // Result is: 42 | "hello 'world'" 43 | ``` 44 | 45 | If you have multiple selections which have different quote marks, you can toggle them all by pressing `Ctrl+'` 46 | 47 | You can also toggle the quote marks of a string by simply putting your cursor inside the string and pressing `Ctrl+'`. The selection will expand to the entire string automatically. 48 | 49 | ## Version History ## 50 | 51 | - **1.2.0** *December 2, 2016* 52 | - Added ability to toggle backquotes by pressing `` ` `` with string selected, or `Ctrl+'` to cycle through quote types. 53 | - **1.1.0** *January 15, 2012* 54 | - Added `Ctrl+'` hotkey to expand to selection inside of strings. 55 | - **1.0.0** *November 8, 2011* 56 | - First release 57 | 58 | [sublime]: http://www.sublimetext.com/ 59 | [package_control]: http://wbond.net/sublime_packages/package_control 60 | --------------------------------------------------------------------------------