Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Sublime Text
Mastering Sublime Text

Mastering Sublime Text: When it comes to cross-platform text and source code editing, Sublime Text has few rivals. This book will teach you all its great features and help you develop and publish plugins. A brilliantly inclusive guide.

By Dan Peleg
$15.99 per month
Book Dec 2013 110 pages 1st Edition
eBook
$19.99 $13.98
Print
$32.99
Subscription
$15.99 Monthly
eBook
$19.99 $13.98
Print
$32.99
Subscription
$15.99 Monthly

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Dec 24, 2013
Length 110 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849698429
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Mastering Sublime Text

Chapter 1. Installing Sublime Text

This chapter will guide us through installing Sublime Text on all the supported platforms. We will also cover advanced installation and basic navigation around Sublime. This chapter is aimed at new users, but we recommend you flip through it even if you are already familiar with Sublime Text.

In this chapter, we will cover the following topics:

  • Installing Sublime Text on OS X

  • Installing Sublime Text on Windows 32/64 bit

  • Installing Sublime Text on Linux 32/64 bit

  • Getting to know the Data and Packages directories

  • Running Sublime for the first time

  • Installing the Package Control ASAP

Preparing for Sublime Text Installation


This chapter serves as a quick installation reference for users who are new to Sublime Text, and covers all supported operating systems. We will also cover advanced installation techniques such as adding Sublime to our Command Line Interface (CLI) and to Ubuntu's action bar. Please refer to the appropriate section depending on your operating system.

Note

At the time of writing this book, Sublime Text 3 was in Beta. Follow #sublimehq on Twitter for version updates.

Installing Sublime Text on OS X


This section will explain how to install Sublime Text on OS X 10.7 or later, as required.

First go to http://www.sublimetext.com/3 and click on the download link for OS X. A file called Sublime Text Build #.dmg will be downloaded. Open this file and we will see the following window:

Simply drag the Sublime Text icon into the Applications folder and wait for the application to be copied.

We have just installed Sublime on our OS X! Is that all? No, we can also get the bleeding edge version from here: http://www.sublimetext.com/3dev. Bleeding edge versions are available for registered users only and are more susceptible to bugs.

Working with Sublime CLI

Sublime Text ships with a CLI called subl, but this CLI isn't added to our environment by default. We want to be able to use it straight from our terminal, so we need to add a symbolic link called subl to the subl executable.

ln –s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

To see if it worked, type subl filename in the terminal where filename is the file you want to edit. Did it work? If not, then we need to add our folder that contains the new symlink to the environment. Run the following command:

open ~/.bash_profile

The first line of the file should start with export PATH=. It contains all the directories that will be looked into for executable binaries when we type a command in the terminal. Since we created a symlink to subl inside the /usr/local/bin directory, we will add it to the path by adding it to the directories:

export PATH=/usr/local/bin[…]

Note

[…] represents other directories that would be listed on the same line, separated by a colon.

Now, run the following code before continuing:

source ~/.bash_profile

This will reload your .bash_profile with the newly added directory.

Sublime CLI should work now; try one of the following commands:

subl filename ("filename" is the filename to edit)
subl foldername ("foldername" is the folder to open)
subl . (to open the current directory)

That's it! We have Sublime Text with CLI running on our OS X!

Installing Sublime Text on Windows 32/64 bit


This section will explain how to install Sublime Text on Windows 7/8, 32/64 bit. It is important to get the right version because the 64-bit version won't run on a 32-bit PC.

Go to http://www.sublimetext.com/3 and download the relevant file for 32-bit or 64-bit. A file called Sublime Text Build # Setup.exe will be downloaded. Open the file and you will be guided through the setup. Click on Next and choose setup location. Next, add Sublime to the explorer context by American English: should use "checking" Add to explorer context menu as shown in the following screenshot:

Then, finish the installation. We have just installed Sublime! Are we done? Not yet.

Adding Sublime to the environment

We want to add Sublime to our environment so we can use it straight from the command line. Open Run by pressing WinKey + R and enter sysdm.cpl, as shown in the following screenshot:

Click on OK, the System Properties window should open. Now, go to the Advanced tab and click on the Environment Variables… button at the bottom-right corner. Environment variables should open, look for the Path variable in System variables, double-click it to open the Edit System Variable window, and add your Sublime installation path to the end of the Variable value field prefixed with a semicolon, as shown in the following screenshot:

As we can see, my installation path is D:\Program Files\Sublime Text 3. Click on OK. Now we can run Sublime from the command line by typing sublime_text filename where filename is the file to edit. But the sublime_text command is too long to type every time we want to open a file with Sublime. Go to your installation directory and create a new file named subl.bat. Paste the following code into the file:

@echo off
start sublime_text.exe %*

The first line turns off the echo of the .bat file. This means that we won't see any output when we run the file. The second line will start the sublime_text executable with the given parameters.

Save the .bat file and open a new command line. We can now use the following commands in our command line:

subl filename ("filename" is the filename to edit)
subl foldername ("foldername" is the folder to open)
subl . (to open the current directory)

That's it; we have Sublime on our Windows PC!

Installing Sublime Text on Linux


This section will explain how to install Sublime Text on different Linux distributions.

Installing Sublime Text on Ubuntu 32/64 bit

This section will explain how to install Sublime Text on Ubuntu 32/64 bit.

There are a few different options for installing Sublime Text on your Ubuntu; we will use the Personal Package Archive (PPA) one. For this, we need to add the PPA that contains the Sublime Package. PPA is a software repository that contains packages that can be installed by Ubuntu's Launchpad.

To add the repository, run the following from the terminal:

sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer

To install Sublime Text 3 on our Ubuntu, we can now use the following commands:

subl filename ("filename" is the filename to edit)
subl foldername ("foldername" is the folder to open)
subl . (to open the current directory)

We can also see the Sublime icon on Ubuntu's action bar, which is typically on the left-hand side, as shown in the following screenshot:

Setting Sublime Text as the default editor

After we have installed Sublime, we want to set it as the default editor for everything! To do that, simply open the defaults.list file of Ubuntu by using the following command:

sudo subl /usr/share/applications/defaults.list

And replace all occurrences of gedit.desktop with sublime-text.desktop.

Installing Sublime Text on other Linux distributions

Installing Sublime on a Linux other than Ubuntu takes a little longer, but we will do it as fast as possible! We start by going to http://www.sublimetext.com/3 and downloading the desired tarball for 32-bit or 64-bit.

Tip

Notice that we do not download the Ubuntu one but the tarball.

After downloading, let's open our terminal and navigate to the Downloads folder:

cd ~/Downloads

The downloaded file is compressed using TAR. We will have to NTAR it first by running the following command:

tar xf sublime_text_3_build_3047_x64.tar.bz2

Note

Your filename might be different depending on your build and architecture.

Move the extracted folder to /op:

sudo mv sublime_text_3 /opt/

We want to make a symbolic link so that we can run Sublime straight from the terminal:

sudo ln -s /opt/sublime_text_3/sublime_text /usr/bin/subl

Now, we have Sublime installed and can use the following commands to open the directory:

subl filename ("filename" is the filename to edit)
subl foldername ("foldername" is the folder to open)
subl . (to open the current directory)

Adding a desktop file

Some distributions such as OpenSUSE, Ubuntu, or GNOME, use .desktop files. These files are for the desktop/action bar launch icons.

Let's add Sublime's .desktop file to the environment. It's good for us that Sublime comes with the file already and we don't need to write it. Just copy the file to the right location using the following command:

sudo cp /opt/sublime_text/sublime_text.desktop /usr/share/applications/

Your distribution may not provide /usr/share/applications, in which case you'll have to copy the file to ~/.local/share/applications.

Getting to know the Data and Packages directories


After the successful installation of Sublime on our system, we can begin to understand what was actually being installed and how can we take advantage of it.

In this section I will use Windows shortcuts. So when I use Ctrl+?, it will be the same as using Command+? on a Mac.

The Data directory

From the official Sublime docs:

Nearly all of the interesting files for users live under the Data directory.

The location of the Data directory is platform-dependent:

  • Windows: %APPDATA%\Sublime Text 3

  • OS X: ~/Library/Application Support/Sublime Text 3

  • Linux: ~/.config/sublime-text-3

We should see at least three directories inside the Data directory:

  • Installed Packages: This contains a copy of every sublime-package installed. It's used to restore packages.

  • Local: This stores all the information about our current and previous session. This is used to restore Sublime to the stage we were in, when we last quit Sublime.

  • Packages: This contains all package folders that Sublime will load.

The Packages directory

The following is written on the official Sublime docs:

"This is a key directory"

This directory contains all the resources for supported programming, markup languages, and custom plugins. We will refer to this folder as Packages.

Tip

We can also access the Packages directory from Sublime's main menu by navigating to Preferences | Browse Packages… on Windows or Linux, and by navigating to Sublime Text | Preferences | Browse Packages… on OS X.

The User package

This package is present at Packages/User and contains all custom plugins, snippets, macros, and user preferences. Let's make our first tweak!

Open Packages/User/Preferences.sublime-settings. We should see the following code:

{
   "font_size": 14,
   "ignored_packages":
   [
      "Vintage"
   ]
}

Try changing the font size and click on Save or Ctrl + S. Our font size has changed! We can also achieve this effect by pressing Ctrl + - to decrease the font size and Ctrl + + to increase the font size. In Linux or Windows, this can also be achieved by holding the Ctrl key while zooming in and out.

Delving into packages, plugins, snippets, and macros

Almost every corner and pixel of Sublime Text can be tweaked, extended, or customized. All this customization is based on JSON, XML, Python, and Sublime files. A package is basically a folder that contains all resources that belong together, and it gives Sublime a new functionality or customization. This is all we need to know for now.

Running Sublime for the first time


Open a random code project. We can do it by typing the following command:

subl projectfolder

In the preceding command, projectfolder is the path of the folder we wish to open. We can also open an empty Sublime window and drag the desired folder into it. The Sublime window is shown in the following screenshot:

In the preceding screenshot, on the left-hand side we can see the File Navigator side bar that contains all the open folders in a hierarchy. At the top, we can see the tabs that are currently open, with the selected one highlighted. On the right-hand side, we can see the Mini Map. In Sublime 3, the Mini Map viewport that indicates the current position in the file has been removed. Let's bring it back!

Press Ctrl + O and open the user preferences, or go to Preferences | Settings-User. As you probably noticed, the file is written in the JSON format.

Tip

If you are not familiar with JSON, please go to http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example or http://JSON.org.

We need to add the following key value to the file:

"always_show_minimap_viewport": true,

Press Ctrl + S to save. Our file should now look like the following:

{
   "font_size": 14,
   "always_show_minimap_viewport": true,
   "ignored_packages":
   [
      "Vintage"
   ]
}

Because Sublime parses things as soon as they get saved, we should be able to see the Mini Map viewport immediately!

Simple navigation

Let's go back to our file using Goto Anything (shown in the following screenshot), one of Sublime's best features, by pressing Ctrl + P:

Tip

We can even highlight the Mini Map by setting "draw_minimap_border": true in our settings file.

As you can see, the Go To window opens after indexing all files that are on the side bar. We can navigate by typing the file name, acronyms, extension, or prefix.

This search feature is called Fuzzy Text Search or Approximate string matching.

Press Esc to close the Go To window. We are going to use this a lot while developing a large project, so we should feel comfortable with it before moving on.

Try closing the current tab by pressing Ctrl + W. Now if no tab is left open, you can open a new one by pressing Ctrl + N, or even open a new window by pressing Ctrl + Shift + N.

Note

If you press Ctrl + W on Windows or Linux when no tabs are open, the window itself will close.

To jump between open tabs in Windows or Linux, simply press Alt + # (where # is the sequential number of the tab from left to right), or press Command + # for OS X. In Windows or Linux, we can also go to the Next or Previous tab by pressing Ctrl + PageUp and Ctrl + PageDown respectively. The same can be done by pressing Option + Command + or Option + Command + in OS X. Another nice navigation feature is the tab stack. To go forward in the stack, press Ctrl + Tab, and press Ctrl + Shift + Tab to go backwards. On OS X also, it's Control and not Command this time.

There are more nice Go To features such as Go To line by pressing Ctrl + G, and Go To matching bracket with Ctrl + M.

Sublime's command palette

One of the most important features is the command palette where all the custom features or plugins can be accessed. Open the command palette by pressing Ctrl + Shift + P, or Command + Shift + P on OS X. We should see the following screenshot:

This window is using the same search algorithm that the Go To Anything window uses. We can see that I typed CA and I get the Close All command. Pressing Enter will close all open tabs. Don't worry, it will notify if a file we are trying to close has not been saved. We can see any keyboard shortcuts to the right of each command.

The Python console

Sublime comes with an embedded Python interpreter. It's a useful tool to inspect the editor's settings, quickly test API calls while developing plugins, or just do simple math. To open the Python console, press Ctrl+` or go to View | Show Console from the main menu. The following screenshot shows a Python console:

It's important to know that Sublime comes with its own Python interpreter on Windows and Linux, and it's separate from your system's Python installation. Modifying your system's version of python, such as replacing it with the MacPorts version, can cause problems with Sublime Text.

Installing the Package Control ASAP


We learned that Sublime has Packages that help us customize our Sublime experience. But how can we find the packages that suit our needs? And how do we install different packages? That's why we have Package Control https://sublime.wbond.net/The Package Control is a non-official open source plugin that lets us navigate through thousands of mainly open source packages ready to install! There are some closed source (commercial) plugins available, such as Sublimemerge and SFTP.

Let's start by installing the Package Control plugin into Sublime. We do it by opening the console Ctrl + ` and copying the following code:

import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read()) 

Tip

If you are having trouble installing the Package Control ASAP using the preceding code, please visit http://sublime.wbond.net/installation.

This code will download the Package Control package and place it inside the Installed Packages directory. After it has finished installing, open the command palette and navigate to Package Control | Install Package. Then press Enter and you will notice Loading repositories [ = ] in the status bar.

After it finishes loading, a new window will open with all the packages available for instant installation!

What we see in this screenshot is the package title, description, version, and the repository link. To install, simply press Enter and the new installed features will appear in the command palette.

Summary


By the end of this chapter we should have Sublime Text with Package Control installed on our system, and all the necessary shortcuts for Sublime commands in the CLI. We have also learned how to navigate the user interface and had a sneak peak of some of the cool features of Sublime.

In the next chapter, we are going to touch some code and learn more advanced techniques for navigating and code editing in Sublime.

Left arrow icon Right arrow icon

Key benefits

  • Discover efficient, keyboard-driven text editing
  • Recognize the latest build of Sublime Text 3
  • Full of illustrations and diagrams, with clear, step-by-step instructions and practical examples that will help you in building cross-platform applications using Sublime Text

Description

Sublime is the leading platform for developing websites, applications, and software. Sublime Text is a sophisticated, cross-platform text and source code editor. It supports a number of different programming languages and is extremely efficient and feature rich. With Sublime Text, programmers can develop their web applications faster and with more efficiency. This book will put you at the frontier of modern software development. It will teach you how to leverage Sublime for anything from mobile games to missile protection. Above all, this book will help you harness the power of other Sublime users and always stay on top. This book will show you how to get started, from basic installation through lightning fast code navigation and up to the development of your own plugins. It takes you from the early stages of navigating through the platform and moves on by teaching you how to fully customize your platform, test, debug, and eventually create and share your own plugins to help and lead this community forward. The book will then teach you how to efficiently edit text, primarily by using the keyboard. You will learn how to interact with the Sublime Text community using the mailing lists and IRC.

What you will learn

Understand the concepts of advanced search and replace techniques Exercise the use of Code Intelligence in Sublime Apply and control live Linting in Sublime Discover overriding and maintaining key shortcuts Learn to test and debug PHP, JavaScript, Python, and Ruby code using Sublime Develop plugins and publish them

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Dec 24, 2013
Length 110 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849698429
Category :
Concepts :

Table of Contents

15 Chapters
Mastering Sublime Text Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
Installing Sublime Text Chevron down icon Chevron up icon
Code Editing Chevron down icon Chevron up icon
Snippets, Macros, and Key Bindings Chevron down icon Chevron up icon
Customization and Theme Development Chevron down icon Chevron up icon
Unravelling Vintage Mode Chevron down icon Chevron up icon
Testing Using Sublime Chevron down icon Chevron up icon
Debugging Using Sublime Chevron down icon Chevron up icon
Developing Your Own Plugin Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.