Book Image

Mastering Sublime Text

By : Dan Peleg
Book Image

Mastering Sublime Text

By: Dan Peleg

Overview of this book

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.
Table of Contents (15 chapters)
Mastering Sublime Text
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.