Sublime Text: how to make shortcut for inserting text?

后端 未结 4 1596
灰色年华
灰色年华 2020-12-13 13:50

I need make a shortcut that will be adding certain text at the cursor, eg {sometext}, how can this be done?

相关标签:
4条回答
  • 2020-12-13 14:34

    Select the Key Bindings - User item under Sublime's Preferences, then add the following example line:

    {"keys": ["ctrl+shift+c"], "command": "insert_snippet", "args": {"contents": "hello!"}}
    

    This will add a CTRL+SHIFT+C shortcut to insert the hello! snippet.

    By the way, don't forget to add a comma to the previous key binding hash so that all but the last line end with a comma. i.e.:

    [
        {"keys": ["..."], "command": "..." },
        {"keys": ["..."], "command": "..." },
        {"keys": ["..."], "command": "..." },
        {"keys": ["ctrl+shift+c"], "command": "insert_snippet", "args": {"contents": "hi!"}}
    ]
    
    0 讨论(0)
  • 2020-12-13 14:43

    @skyisred I think the question is already very well answered to the point. But web-developers often will need to generate dummy content to fill the elements. Say "Lorem ipsum".

    Sublime's dummy text generator

    Type lorem and hit Tab

    Sublime will generate the complete Lorem ipsum dummy text for you.

    Creating shortcut keys to do it

    If you really want to do it with a shortcut

    Go to
    [On Windows] Preferences > Key Bindings - User
    [On OS X] Sublime Text > Preferences > Key Bindings - User

    [
         {"keys": ["ctrl+alt+i","ctrl+alt+s"], "command": "insert_snippet","args": {"contents": "A small snippet"}},
         {"keys": ["ctrl+alt+i","ctrl+alt+m"], "command": "insert_snippet","args": {"contents": "A medium-sized snippet, just good enough to fill in a normal span element."}},
         {"keys": ["ctrl+alt+i","ctrl+alt+l"], "command": "insert_snippet","args": {"contents": "A large snippet, a little more than the medium snippet and just good enough to fill in a paragraph element.This will make your work so much more easier."}}
         ]
    

    I have written an article on creating custom keyboard shortcuts in Sublime Text which might be helpful to you to create shortcuts for other common operations in Sublime Text.

    0 讨论(0)
  • 2020-12-13 14:48

    If you already have a snippet file written, say at Packages/User/myFunction.sublime-snippet, you can use

    Add this to Preferences > Key Bindings - User

    { "keys": ["ctrl+1"], "command": "insert_snippet", "args": {"name": "Packages/User/myFunction.sublime-snippet"} }
    

    This example binds the snippet to CTRL + 1.

    I found the info on Christopher Millward's blog.

    0 讨论(0)
  • 2020-12-13 14:55

    I did something a little bit more complex and complete too. Like \emph{} when you press ctrl+l, ctrl+e ... My job here is to write \textit{} when you press ctrl+shift+i.

    Go to Preferences > Browse Packages. There inside you should save a file named "Text ital.sublime-snippet" for example. Inside of this file put this code:

    <snippet>
        <description>Italic text</description>
        <content><![CDATA[
    \\textit{${1:$SELECTION}}
    ]]></content>
        <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
        <!-- <tabTrigger>hello</tabTrigger> -->
        <scope>text.tex.latex</scope>
    </snippet>
    

    Then go to Preferences > Key Bindings. There inside you will write this:

    [
    { "keys": ["ctrl+shift+i"],  
        "context":  [
            {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}],
        "command": "insert_snippet", "args": {"name":"Packages/LaTeXTools/Text ital.sublime-snippet"}}
    ]
    

    Remind that Packages/LaTeXTools/ is the Location of Preferences > Browse Packages. Remind too that you should insert a comma between two different shortcuts you create.

    Hope it works!

    0 讨论(0)
提交回复
热议问题