Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text: how to make shortcut for inserting text?

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

like image 722
skyisred Avatar asked Mar 23 '13 01:03

skyisred


People also ask

What is the keyboard shortcut for inserting text?

Insert a Text box Press and release ALT, N, and then press X. Press the arrow keys to select the Text box that you want, and then press ENTER.

How do I create a custom text shortcut?

Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign. For example, press CTRL plus the key that you want to use.


4 Answers

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!"}}
]
like image 57
Greg Sadetsky Avatar answered Oct 24 '22 11:10

Greg Sadetsky


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.

like image 37
Tyler Collier Avatar answered Oct 24 '22 13:10

Tyler Collier


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

like image 21
Narendran Parivallal Avatar answered Oct 24 '22 11:10

Narendran Parivallal


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!

like image 20
Henrique Oliveria Bodart Soare Avatar answered Oct 24 '22 13:10

Henrique Oliveria Bodart Soare