Replace certain characters when typing (YAML)



  • Good evening,

    I just came across a problem in a tutorial (or the writer of the tutorial) where I imagine you can get a handle on it with notepad++.

    It’s about the reverse proxy “Traefik V2”. So here there are configuration files YAML files, which have a small peculiarity.

    There must not be used the superscript ( ’ ), but this character ( ` ). Unfortunately it is so that one must press here first the SHIFT key, then the actual key and afterwards still the blank.

    Is there a possibility to teach notepad++, if YAML is selected as language, that the apostrophe key ’ is pressed, but this character ` is generated.

    Thanks a lot



  • @Holger-Hasenmüller ,

    Sorry, not with any built-in Notepad++ tool. Normally, the keyboard settings (what character gets typed when a specific key is pressed) are up to the OS, not to the application.

    Those who are experts with the PythonScript plugin might be able to hack up something that will add a handler-function which verifies YAML and then makes that substitution automatically when the “wrong” character is typed. But you would have to be willing to install the PythonScript plugin.



  • isnt it that autohotkey can handle such type of solution ?



  • @PeterJones said in Replace certain characters when typing (YAML):

    Those who are experts with the PythonScript plugin might be able to hack up something that will add a handler-function which verifies YAML and then makes that substitution automatically when the “wrong” character is typed. But you would have to be willing to install the PythonScript plugin.

    Ahem…

    hack up something

    That sounds like unfortunate act that my cat performs, on occasion.
    Pythonscript solutions are always professionally architected and developed solutions, not “hacks”. :-)

    So this sounds like a job for the “char added” notification message from Scintilla, which could handle a replacement dynamically as the user types.

    But, what about pasting data – if the wrong character happens to be in a block of pasted data, when does it get corrected?
    Due to this, it seems like a fuller solution is needed than just an on-the-fly correction of one character.

    Still, nothing a script can’t handle, just need to know what the spec is, for whomever might try to write it.
    I don’t know if I can find time, I think I have to go see what my cat is up to in the other room…I hear something…

    a4362267-44a1-4825-84e9-c1c8e87fed77-image.png



  • hacks … tststs … never thought this day would come … but…

    ;-D

    ZnJvbSBOcHAgaW1wb3J0IGVkaXRvciwgU0NJTlRJTExBTk9USUZJQ0FUSU9OLCBNT0RJRklDQVRJT05GTEFHUw0KDQpkZWYgb25fbW9kaWZ5KGFyZ3MpOg0KICAgIGlmIChlZGl0b3IuZ2V0TGV4ZXJMYW5ndWFnZSgpID09ICd5YW1sJyBhbmQgDQogICAgICAgIGFyZ3NbJ21vZGlmaWNhdGlvblR5cGUnXSA9PSBNT0RJRklDQVRJT05GTEFHUy5JTlNFUlRDSEVDSyk6DQogICAgICAgIGlmICInIiBpbiBhcmdzWyd0ZXh0J106DQogICAgICAgICAgICBlZGl0b3IuY2hhbmdlSW5zZXJ0aW9uKGFyZ3NbJ3RleHQnXS5yZXBsYWNlKCInIiwgJ2AnKSkNCg0KZWRpdG9yLmNhbGxiYWNrU3luYyhvbl9tb2RpZnksIFtTQ0lOVElMTEFOT1RJRklDQVRJT04uTU9ESUZJRURdKQ
    


  • @Ekopalypse

    Nice one. Not sure why you chose to “hide” it, but far from me to “unhide” it. :-)



  • @Alan-Kilborn

    because I didn’t want to spoil if you decided to go for it :-D



  • @Ekopalypse

    Oh, no. If you already have a solution handy, post it ASAP.
    I’ve got better things to spend my time on than reinventing the wheel.
    :-)



  • @Holger-Hasenmüller

    a python script which would translate ' to `
    But I’m not sure if this is the right way to go, as it means you
    can no longer use the single quote in yaml documents.

    from Npp import editor, SCINTILLANOTIFICATION, MODIFICATIONFLAGS
    
    def on_modify(args):
        if (editor.getLexerLanguage() == 'yaml' and 
            args['modificationType'] == MODIFICATIONFLAGS.INSERTCHECK):
            if "'" in args['text']:
                editor.changeInsertion(args['text'].replace("'", '`'))
    
    editor.callbackSync(on_modify, [SCINTILLANOTIFICATION.MODIFIED])
    


  • @Alan-Kilborn ,

    hack up something

    Okay, not the best choice of words in the context of cats.

    I was thinking of the old paper-and-scissors “hack” algorithm: taking an already existing solution, copy/paste the simple guts in the right order, tweak a few things. (I am guessing that Eko took one of his existing scripts, and started from that… though, with as simple as it was, maybe he really did start from scratch.)

    And yes, what @Ekopalypse came up with followed my mental outline; I just haven’t had time recently to hack engineer such solutions when the questions are first posted.

    … real life getting in the way of the forum, grumble, grumble …



  • @PeterJones

    That was quite a lengthy process.
    First I read through the question.
    Then I tried to understand the question and what a possible solution might look like.
    Then I had coffee and cake and had to start all over again.
    In no time at all, 2 hours went by :-D
    In the end I did a copy and paste so as not to miss dinner.

    :-D


Log in to reply