Sublime Forum

Automatic Text Substitution

#1

I looking for something in Sublime to automatically replace certain strings as I type with other strings. For example, I would like each instance of “cliche” as I type to be automatically replaced by “cliché.” I have experimented a little with snippets to accomplish this, but is there a way to do it without pressing tab and simply having it happen in the background as I type? It also seems like overkill to have a separate snippet file for each word I would like to automatically replace.

0 Likes

#2

I also find it unnecessary to have a load of separate snippet files, when a ‘sublime-completions’ file can be both a simple list of words and snippets. Have a look at ‘PHP.sublime-completions’ to give you the idea. But accented characters may require a snippet to embed their hex character code.

However, they would still require pressing Tab, or Enter, to complete the word. “Auto-correction”, similar to MS Word, could be achieved using the ‘on_modified’ event to monitor, and correct, text as you type (I’m not sure I’d fancy the overhead involved with this). Or, less intrusive, would be to create a TextCommand that looks for and replaces a list of words that you maintain on pressing a keyboard-combination.

Anyway, you could create a ‘yourname.sublime-completions’ file and store it in your ‘Packages\User’ folder that looks like this:

[code]{
// “scope”: “source.php - variable.other.php”, // remove this line if you want it to work for all files

"completions":

	"cliché", "wibble", "nee",

	{ "trigger": "cliche", "contents": "clich\x{C3A9}" },
	{ "trigger": "acos", "contents": "acos(${1:arg})" }
]

}[/code]
I haven’t tested whether C3A9 is the correct code-number, or whether it requires double-escaping ‘\’. The inner curly brackets may even need to be escaped :smile:. That is, using ‘{C3A9}’ or double . (Sorry, being a bit lazy…). Andy.

0 Likes

#3

Don’t know what OS you’re running, but it sounds to me like this would work better for you on the OS level. On the Mac, there are tools like TextExpander that you can use to automatically replace what you type with other words (and fix common misspellings) - I’m sure other OSes have comparable applications that do the same thing.

0 Likes