Search code examples
replacesublimetext3keymapping

keymap to replace letter in Sublime text 3


I am trying to make a keymap to replace some letter combination to fix certain special Latin characters. Example:

I am trying to make a Spanish ñ with gn:

{ "keys": ["gn"], "command": "insert_snippet", "args": {"contents": "\\\~n"} },

My expected result with this is:

agno ---> a~no ----> año

but it lamentably doesn't work. Do you know how to fix it?


Solution

  • This binding doesn't work because the keys key is not valid; it needs to be either a key on the keyboard, a key with modifiers, or a list of consecutive keys (with or without modifiers). With the binding as you have it currently, if you check the Sublime console (View > Show Console) you'll see a message like this every time you save the file as an indication of this:

    Unknown key gn
    Unable to parse binding {args: {contents: \~n}, command: insert_snippet, keys: [gn]}
    

    Assuming the the idea is that you want the snippet to insert when you press those two keys in sequence, you can do that by specifying both keys one after the other:

    { 
        "keys": ["g", "n"], 
        "command": "insert_snippet", 
        "args": {"contents": "\\~n"} 
    },
    

    Something to keep in mind is that a binding like this will trigger every time these two characters are typed—unless you manually wait a bit before you press the second key; for example, you can't spell ignominious without pausing between the g and the n to let Sublime realize you mean the two keys should be considered to be distinct instead.

    As a side note, I assume this sequence of keys (\~n) is what you'd manually type in order to generate the ñ character. If so, I'm not sure if this will do what you want; it may just insert those three literal characters. If that's the case, you can replace them with the ñ character you want to be inserted instead.