Éviter que Sublime Text n’écrase les parenthèses fermantes

Quand on tape une parenthèse fermante (ou ] voire }) juste à côté d’une AUTRE parenthèse fermante, le comportement de Sublime Text par défaut est de ne pas ajouter de parenthèse, mais d’avancer d’un caractère.

La raison est que Sublime ferme automatiquement toute parenthèse ouverte, cette fonctionnalité est donc là pour vous éviter de taper une parenthèse fermante de trop.

Je déteste cette fonctionnalité.

Je suis assez grand pour savoir quand insérer ma parenthèse ou non, et je n’ai pas Parkinson.

Heureusement, comme tout dans cet éditeur qu’il est merveilleux, c’est désactivable. Ouvrez “Preferences > Key Bindings – User” et ajoutez ceci à la configuration :

    { "keys": ["\""], "command": "insert", "args": {"characters": "\""}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true }
        ]
    },
    { "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
        ]
    },
    { "keys": ["'"], "command": "insert", "args": {"characters": "'"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true }
        ]
    },
    { "keys": ["]"],"command": "insert", "args": {"characters": "]"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
        ]
    },
    { "keys": ["}"], "command": "insert", "args": {"characters": "}"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
        ]
    }

Abracadabra !

No related posts.

flattr this!

12 comments

  1. Alors là, merci
    Je supporte ce comportement depuis toujours dans ST2, et je pense qu’il existait déjà à l’époque de TM2.

    D’ailleurs je m’aperçois que c’est pas pour les aides à la saisie, qui en l’occurrence peuvent être contre productifs, et autres code snippets que j’utilise ces éditeurs. J’arrive jamais à me rappeler le raccourcis pour fermer un tag HTML, alors je le ferme tout seul ;-)

  2. mothsART

    Excellent : le truc pour lequel on peste au début, qu’on se promet de résoudre … et que finalement on ne résoud jamais parce qu’on fini par anticiper l’éditeur, faute de mieux.

    Merci d’être différent ;)

  3. Je n’ai jamais pris le temps de chercher une solution, mais ça m’agace suffisamment pour venir poster un commentaire de remerciements !
    Et heureusement que SublimeLinter est là pour raler, sinon, bien souvent, je m’en apercevrais qu’à l’exécution…

  4. Fuck yeah!

    Je t’aime, Sam, mon sauveur !

    Plus sérieusement, ST commençait à me les hacher menu à la longue…

  5. Bon je ne connais pas ST. Perso j’utilise vi sous Unix et notepad++ sous zindow. Mais je me dépêche en ce moment même d’aller combler cette lacune.
    Ceci dit, moi non plus je n’apprécie pas qu’un outil se permette de réfléchir à ma place. Surtout qu’il existe des cas particuliers où on met des parenthèses non pairées (par exemple dans le case…esac du shell).

    Merci donc de ces infos que je lis avidement ;)

  6. jean roger

    Sinon, pour les champions, ya vim.

  7. cendrieR

    Merci !

  8. Mouais, après quelques heures d’utilisation, j’ai un nouveau problème.
    Maintenant, quand je tape une balise ouvrante, que je tape du code puis tape la balise fermante, ça m’en rajoute une deuxième (normal, en fait), et je me retrouve à avoir un nouveau problème là où je n’en avais pas pour en corriger un autre.
    Du coup, à moins de désactiver l’ajout automatique de balise fermante, c’est choisir entre la peste et le choléra.
    La peste pour la création d’une ligne de code, le choléra pour la modification de cette ligne.

  9. Sur SO, quelqu’un a posé la même question, et a une solution un chouia mieux que celle de Sam : même comportement, mais le caractère fermant reste remplacé si le caractère précédent est le caractère ouvrant :
    Lien SO

  10. Duckie

    vim c’est kiffant mais sur windows c’est crado, autant utiliser Sublime dans ce cas faute de taffer sous un Unix.

    Les gars vous m’avez plié de rire avec le trombone à côté des commentaires.

  11. :-)

  12. Pour ceux qui préfèrent la version de Groug, voici le code en entier (car sur SO il est tronqué):

        { "keys": ["\""], "command": "insert", "args": {"characters": "\""}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "[^\"]$", "match_all": true }
            ]
        },
        { "keys": ["\""], "command": "move", "args": {"by": "characters", "forward": true}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "\"$", "match_all": true }
            ]
        },
    
        { "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "[^(]$", "match_all": true }
            ]
        },
        { "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true }
            ]
        },
    
        { "keys": ["'"], "command": "insert", "args": {"characters": "'"}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "[^']$", "match_all": true }
            ]
        },
        { "keys": ["'"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "'$", "match_all": true }
            ]
        },
    
        { "keys": ["]"], "command": "insert", "args": {"characters": "]"}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "[^\\[]$", "match_all": true }
            ]
        },
        { "keys": ["]"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true }
            ]
        },
    
        { "keys": ["}"], "command": "insert", "args": {"characters": "}"}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "[^\\{]$", "match_all": true }
            ]
        },
        { "keys": ["}"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
            [
                { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true }
            ]
        }
    

Flux RSS des commentaires

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Jouer à mario en attendant que les autres répondent