Power of the Keyboard
When working with a text editor like Sublime Text, you will gain the most power and speed by staying away from the mouse. In order to do that you need to learn some handy keyboard shortcuts.
You should already know the common ones for saving, copying, cutting, undoing, and redoing. Those commands are all the same in Sublime Text, but Sublime offers a lot of custom shortcuts for working with text.
Don’t worry, you won’t need to remember all of them. All the shortcuts are available in the Sublime Text menu or visit the documentation for a list. Below we are just going to go over some important ones that you will want to remember or take note of.
Open a File
The first thing we need is some text to work with. I have created a text file with a bunch of data for us to practice with. Download it here.
If you have any file tabs open, we can close all those to start with a clean slate.
Now, use the keyboard shortcut CTRL+ O. This will open the file explorer for your computer. Navigate to where you download the sample.txt file and open it. You should just have one tab in Sublime that reads “sample.txt”.
Bulk Updating the End
The document you have has 40 lines of semi-random text. Let’s start working with this data. We will use scenarios to guide what we do to the text.
Scenario: I want an additional period at the end of each line of text.
So the obvious way to do this is to put the cursor at the beginning of each line of text and press the dash key. But, I don’t want to do that 40 times. And what if next time there are 100 lines or a thousand.
- CTRL + A
- CTRL + SHIFT + L
- RIGHT ARROW KEY
The first shortcut should be one you are familiar with, CTRL + A. This shortcut just selects all text in the document.
The second shortcut turns on the “multi-cursor” feature of Sublime Text. That is correct, you now have a cursor placed on each line of text. This enables you to add, delete, or navigate multiple lines at a time. Make sure you hit that right arrow key or you will delete all your text since everything is currently selected. See the difference in the images below.
We can finish up the scenario now by simply typing a period. You should now see two periods at the end of each line.
You have just saved a lot of time and effort. Let’s compare the effort. If you didn’t use the shortcuts you would have had to click at the end of each line, for a total of 40 clicks. Then you would have had to enter 40 periods. So the hard way would have caused you to make 80 actions in order to update the data.
Using the shortcut, you only had 3 actions to update all the data. OK, I think you get the point so I will not mention the savings again. Let’s do some more scenarios.
Bulk Updating the Start
Scenario: I want to add a dash and space at the end of each line of text.
- CTRL + A
- CTRL + SHIFT + L
- LEFT ARROW KEY
This one is basically the same as the last one, but you are putting the cursors on the far left instead of the far right. Go ahead and type the dash with a space to finish up.
Find and Replacing Text
Scenario: I want to replace all occurrences of the word Laboris
- CTRL + H
So you may be familiar with the CTRL + F command that finds text. CTRL + H is similar but it adds the ability to find and replace. Go ahead and type in “Laboris” in the top text field. Let’s replace it with “Labrador”.
You have some options when it comes to find and replace.
On the left side:
- Regular Expression (ALT + R)
- Case Sensitive (ALT + C)
- Whole Word (ALT + W)
- Preserve Case (ALT + A)
On the right side:
- Find (F3 or Enter)
- Find All (ALT + ENTER)
- Replace (CTRL + SHIFT + H)
- Replace All(CTRL + ALT + ENTER)
Try replacing one match at a time. Cycle through the matches with F3 or Enter. Use CTRL + SHIFT + H to replace that match. Notice that you are automatically taken to the next match.
Now try replacing all at once. Easy right? Feel free to play around with the other options to be more precise of what you replace.
I’m sure by now you can see the benefits of using Sublime Text and the shortcut keys to quickly find and edit data. Obviously the bigger the file, the more time you will have saved.
Look through the other shortcuts and find ones that you think will help you optimize your work.