Sunday, August 23, 2015

Cheat Sheets in Sublime Text

Sublime is a lightweight free text editor.  I started using Sublime Text, when version 2.0 came out.  The more I use it, the more it grows on me.  Sublime Text 3 (beta) is available for OS X, Windows, or Linux (both 32 or 64 bit versions).

Besides being lightweight, having a built in regular expressions engine, a console, built in syntax coloring for a variety of file formats and programming languages; it's also extensible.

One of my favorite extensions is the Package Control extension, by which you can add packages.  One package that I found helpful was the Cheat Sheet package.

It allows you to use keyboard shortcuts to access existing cheat sheets that have already been created for you and allows you to create your own:

CommandKeyboard Shortcut
Bash *Ctrl + Shft + C, S, H
Git *Ctrl + Shft + C, G, I, T
Github Flavored Markdown *Ctrl + Shft + C, G, F, M
Go *Ctrl + Shft + C, G, O
KDE *Ctrl + Shft + C, K, D, E
Regular ExpressionsCtrl + Shft + C, R, X
Sublime Text *Ctrl + Shft + C, S, T
* Incomplete sheet.




Create Your Own Cheat Sheet

Use the following for the package path:

Windows 7 -  C:\Users\<user name>\AppData\Roaming\<your version of sublime text>\Packages\Cheat Sheets (e.g. C:\Users\jsmith\AppData\Roaming\Sublime Text 3\Packages\Cheat Sheets).  

Mac OSX -  /Users/<user>/Library/Application Support/Sublime Text 3/Packages or within Sublime Text navigate via the menu: Sublime Text 3 > Preferences > Browse Packages


Use the following for the menu path:

Windows 7 -  C:\Users\<user name>\AppData\Roaming\<your version of sublime text>\Packages\Cheat Sheets\Main.sublime-menu  

Mac OSX -  /Users/<user>/Library/Application Support/Sublime Text 3/Packages/Cheat Sheets/Main.sublime-menu


Alternatively you can following to get the path: view.run_command("cheat_sheet_tester", {"cheatsheet": "$filename"})

Next follow the steps in How to Add Your Own Cheat Sheets or here:
How to add your own Cheat Sheets
  1. Add your cheat sheet to $ST/Packages/User/cheat-sheets/$filename.cheatsheet.
  2. Add a keyboard shortcut by adding the following line to $ST/Packages/User/Default ($OS).sublime-keymap and change the keys and $filename:
    [
        { "keys": ["ctrl+shift+c", "n", "s"], "command": "cheat_sheet", "args": {"cheatsheet": "$filename"} }
    ]
    
  3. Add a menu entry by adding the following to $ST/Packages/User/Main.sublime-menu and change both instances of $filename:
    [
        { "id": "tools", "children": [
            { "id": "cheat-sheets", "caption": "Cheat Sheets", "children": [
                { "caption": "$filename", "command": "cheat_sheet", "args": {"cheatsheet": "$filename"} }
            ]}
        ]}
    ]
    
  4. Add a palette item to $ST/Packages/User/Default.sublime-commands and change both instances of $filename.
    [
        { "caption": "Cheat Sheet: $filename", "command": "cheat_sheet", "args": {"cheatsheet": "$filename"} }
    ]
    
    To add multiple cheat sheets copy and paste just the keys or caption line and add a comma in between each entry to all the above files.
  5. Highlighting follows this format:
    >\t Header
    >\t\t Subtext
    Text
    Command \t Text
    Command \s\s Text
    \tCommand \t Text # Comments
    
    Where \t means tab and \s means space.
  • If there's a problem, you can use the cheat_sheet_tester command. The tester command will print in the console the file paths where it expected to find your $filename. The console can be opened with Ctrl + ` or View > Show Console.
    The tester command can be run directly in the console with:
    view.run_command("cheat_sheet_tester", {"cheatsheet": "$filename"})
    
    The tester command can also be run as a keyboard shortcut with:
    { "keys": ["ctrl+shift+c", "r", "y"], "command": "cheat_sheet_tester", "args": {"cheatsheet": "$filename"} }
    


No comments: