Sublime Text: how to make shortcut for inserting text?

33,197

Solution 1

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!"}}
]

Solution 2

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.

Solution 3

@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.

Solution 4

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!

Share:
33,197
skyisred
Author by

skyisred

Updated on June 29, 2020

Comments

  • skyisred
    skyisred almost 4 years

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

  • Paul
    Paul over 7 years
    Putting "$0" in the contents will specify the cursor position. For example, "h$0i!" will place the cursor after the "h" once the snippet is inserted.