My settings, preferences and tricks for Sublime Text

  • Posted on: 9 June 2019
  • By: stillfinder

Sublime Text is an incredible text editor for developers. I work mostly with Laravel and here are my preferences and settings.

Package Control

It is the Sublime Text package manager. I would say that this is a must have if you use Sublime Text. To configure, follow this guide: https://packagecontrol.io/installation.

Material Theme

https://github.com/equinusocio/material-theme

snimok_ekrana_2019-06-10_v_14.39.00.png

I really like this theme. It can be installed using Package Control.

After the theme is installed, add the following lines to user settings so as to activate:

 
  "theme": "Material-Theme.sublime-theme",
  "color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",

Roboto Mono font

I use Roboto Mono font. It is a monospaced addition to the Roboto type family. Monospaced fonts are used for programming because they are widely believed to be better due to common treatment of skinny letters and punctuation, and alignment.

Setup Roboto Mono font from https://fonts.google.com/specimen/Roboto+Mono.

My settings:

  "font-face": "Roboto Mono",
  "font_size": 14,

Also, I like to increase line spacing:

    "line_padding_bottom": 6,
    "line_padding_top": 6,

The most useful Keyboard shortcuts

⌘ + P - Goto Anything. To navigate to a specific method, you can use the next format: "ClassName@methodName".
⌘ + R - Goto Symbol
⇧ + ⌘ + R - Goto Symbol in Project

⌘ + L - Goto Line

⌥ + ⌘ + ↓ - Goto definition
⌥ + ⌘ + ↓ + ⇧- Goto reference

⌘ + [ - Jump Back
⌘ + [ - Jump Forward

AdvancedNewFile

This plugin allows faster file creation within a project.
More information: https://github.com/skuroda/Sublime-AdvancedNewFile.

snimok_ekrana_2019-06-10_v_14.37.23.png

Use ⌘ + ⌥ + n shortcut to create new files. Or if you need to create a file in the current folder, start your typing with ':'.
Also, you can rename and delete files. Use ⇧ + ⌘ + P, type 'ANF' to open AdvancedNewFile actions and select what would you like to do with current file.

Add the next lines to user specific settings of the plugin in order to set current directory for renaming a file:

"rename_default": "<filepath>",

Multi-select

multiselect_0.gif

⇧ + ⌘ + D - select word, keep typing to select additional occurensies of the word
⌃ + ⌘ + G - select all occurrences of the word

Snippets

snippets.gif

You can add new snippets using Tools->Developer->New Snippet. If you want to insert snippet's code, just start enter snippet shortcut and press [tab].

Examples of the snippets that I use:

  • met - create new method

          <snippet>
            <content><![CDATA[
              public function ${1}()
              {
                ${0}
              }
     
              ]]>
            </content>
            <tabTrigger>met</tabTrigger>
            <description>PHP Public Method</description>
            <scope>embedding.php, text.html.basic, meta.embedded.block.php, source.php, meta.class.php, meta.block.php</scope>
          </snippet>
     
  • pmet - create new protected method

          <snippet>
            <content><![CDATA[
              protected function ${1}()
              {
                ${0}
              }
     
              ]]>
            </content>
            <tabTrigger>pmet</tabTrigger>
            <description>PHP Protected Method</description>
            <scope>embedding.php, text.html.basic, meta.embedded.block.php, source.php, meta.class.php, meta.block.php</scope>
          </snippet>
     
  • $ generate '$this->'

          <snippet>
            <content><![CDATA[
              \$this->
              ]]>
            </content>
            <tabTrigger>$</tabTrigger>
            <description>this</description>
            <scope>embedding.php, text.html.basic, meta.embedded.block.php, source.php, meta.class.php, meta.block.php, meta.function.php, meta.block.php, variable.language.php, punctuation.definition.variable.php</scope>
          </snippet>
     
  • class - generate class code

          <snippet>
            <content><![CDATA[
              <?php
     
              namespace App\\${1};
     
              class ${2}
              {
                ${0}
              }
              ]]>
            </content>
            <tabTrigger>class</tabTrigger>
            <description>New Class Fill</description>
            <scope>embedding.php, text.html.basic, meta.embedded.block.php, punctuation.section.embedded.begin.php</scope>
          </snippet>
     
  • ddl - generate dd('file_name:line class@method');

          <snippet>
            <content><![CDATA[
              dd(__FILE__ . ':' . __LINE__ . ' ' . __CLASS__ . '@' . __FUNCTION__);
     
              ]]>
            </content>
            <tabTrigger>ddl</tabTrigger>
            <description>dd() with current position information</description>
            <scope>embedding.php, text.html.basic, meta.embedded.block.php, source.php, meta.class.php, meta.block.php, meta.function.php, meta.block.php, meta.function-call.php, variable.function.php</scope>
          </snippet>
     
  • tmet - generate test method.

          <snippet>
            <content><![CDATA[
              /** @test */
              public function ${1}()
              {
                ${0}
              }
              ]]>
            </content>
            <tabTrigger>tmet</tabTrigger>
            <description>Public Test Method</description>
            <scope>embedding.php, text.html.basic, meta.embedded.block.php, source.php, meta.class.php, meta.block.php</scope>
          </snippet>
     

PHP getters and setters plugin

https://github.com/francodacosta/sublime-php-getters-setters
Type ⇧ + ⌘ + P and enter generate getter(or setter) to call the plugin.

PHP companion plugin

https://github.com/erichard/SublimePHPCompanion

My key bindings:
⌥ + E - expand_fqcn, expands the class under the cursor to its FQCN (Fully Qualified Class Name).
⇧ + F6 - expand without the leading namespace separator \ .
⌥ + i - find_use, It will show you the different namespace that match your class. Bring your cursor hover a class name and hit the shortcut.
F4 - import_namespace, it will add the namespace definition based on the absolute filename of the current file.
⇧ + F12 - goto_definition_scope, search for a method definition based on the current scope. It will fallback to the "goto_definition" command if a match was not found.
⌥ + C - insert_php_constructor_property, insert both a constructor argument and its according property.

User's key binding settings:

  { "keys": ["alt+e"], "command": "expand_fqcn" },
  { "keys": ["shift+f6"], "command": "expand_fqcn", "args": {"leading_separator": true} },
  { "keys": ["alt+i"], "command": "find_use" },
  { "keys": ["f4"], "command": "import_namespace" },
  // { "keys": ["f3"], "command": "implement" },
  { "keys": ["shift+f12"], "command": "goto_definition_scope" },
  { "keys": ["alt+c"], "command": "insert_php_constructor_property" },

User's settings:

  plugin settings
  {
    "use_sort_length": true,
    "visibility": "protected",
  }

PHPUnit

SimplePHPUnit plugin https://github.com/evgeny-golubev/SimplePHPUnit-for-Sublime-Text.

Trigger Artisan From Sublime

https://packagecontrol.io/packages/Laravel%205%20Artisan
It's Laravel specific plugin. To install use Package Control (⌘ + ⇧ + P, Install Package) and enter 'Artisan'. Then select appropriate version of Laravel.

Code Formatting

PHP CS Fixer

https://github.com/FriendsOfPHP/PHP-CS-Fixer

I'm using PHP CS Fixer for formatting code. To install PHP CS Fixer run:
composer global require friendsofphp/php-cs-fixer

Example of usage:
php-cs-fixer fix app/Conversations/AskStartEnglishTestConversation.php --rules=@PSR2 // Fix all issues using PSR-2 coding standard
php-cs-fixer fix app/Conversations/AskStartEnglishTestConversation.php --config=.php_cs // Fix all issues using custom configuration file

To call from the Sublime Text you need to add a new build system and setup the next parameters for PSR-2 coding standard:

  {
    "shell_cmd": "php-cs-fixer fix $file --rules=@PSR2"
  }

Or these parameters for the Laravel coding standards:

{
    "shell_cmd": "php-cs-fixer fix $file --config=\\${HOME}/code/.php_cs"
}

--config=\\${HOME}/code/.php_cs - it's a path to your configuration. Simply place this file in your project folder or wherever you prefer.

Example of my '.php_cs' configuration for Laravel projects:

Laravel PHP CS Fixer

https://github.com/stechstudio/Laravel-PHP-CS-Fixer
This package makes it easier than ever to use PHP CS Fixer to maintain your laravel code by providing access to it via tools that you are already familiar with. An artisan command to fix the code, and manage the configuration the way you do all the other laravel packages you use.

Example of usage:
php artisan fixer:fix

Sublime linter

http://www.sublimelinter.com

My configuration files for Sublime Text 3

Default (OSX).sublime-keymap

Preferences.sublime-settings

SyncedSideBar

https://github.com/TheSpyder/SyncedSideBar

As you switch tabs Sublime highlights only files in folders that are already expanded. This plugin makes that work for all files. It accomplishes this through use of the "reveal in side bar" command from the default context menu.

Related articles