Chapter 2: Modern text editors | Learn Enough Text Editor to Be Dangerous | Learn Enough to Be Dangerous
You have to make a choice. Choose...wisely.

Get occasional notifications about things like product discounts, blog posts, and new or updated tutorials. Unsubscribe at any time.

Quick Checkout
or Pay by Credit Card
Error processing your payment
  • You didn't choose whether or not to be added to the mailing list
Confirm
$0.00

Payments and credit card details are securely managed and protected by Learn Enough's payment processor, Stripe. More information on their site:

CART
Total
$0.00

Your Cart is Empty

$30
$300
$300
$XY
$XY
1234
Get Single Tutorial
MORE INFO

Learn Enough Text Editor to Be Dangerous is available as an ebook, an offline video series, and as a structured, self-paced online course. The course includes full online access to the book content, streaming videos, progress tracking, exercises, and community exercise answers.

All Access Subscription
MORE INFO

The Learn Enough All Access Subscription includes the entire Learn Enough introductory sequence and the full Ruby on Rails Tutorial. More than 2500 pages of book content and 53 hours of video that teach you to code from total beginner up to professional-grade web development.

Sign up for the course and get access to the full tutorial and streaming screencasts!

Chapter 2 Modern text editors

Having learned the minimal basics of text editing with Vim, we’re now in a good position to appreciate the preferred “modern” text editors mentioned at the beginning of this tutorial. These editors include cross-platform native editors such as Sublime Text, Visual Studio Code, and Atom, and editors in the cloud like Cloud9. Modern editors are distinguished by their combination of power and ease of use: you can do fancy things like global search-and-replace, but (unlike Vim) they let you just click in a window and start typing. In addition, many of them (including Atom and Sublime) include an option to run in Vim compatibility mode, so even if you end up loving Vim you can still use a modern editor without having to give Vim up entirely.

Throughout the rest of this tutorial, we’ll explore the capabilities of modern text editors. We’ll end up covering all of the topics encountered in our discussion of Vim (Chapter 1), as well as many more advanced subjects, including opening files, moving around, selecting text, cut/copy/paste, deleting and undoing, saving, and finding/replacing—all of which are important for day-to-day editing. We’ll also discuss both menu items and keyboard shortcuts, which help make your text editing faster and more efficient.

For future reference, Table 2.1 shows the symbols for the various keys on a typical Macintosh keyboard. Apply your technical sophistication (Box 1.2) if your keyboard differs.

Key Symbol
Command
Control
Shift
Option
Up, down, left, right ↑ ↓ ← →
Enter/Return
Tab
Delete
Table 2.1: Miscellaneous keyboard symbols.

2.1 Choosing a text editor

While cloud IDEs have many advantages, every aspiring computer magician should learn at least one native editor (i.e., an editor that runs on your desktop operating system). While there are many editors to choose from, probably the most promising modern text editors in use today are Sublime Text (sometimes just called “Sublime”), Visual Studio Code (VScode), and Atom.1 Each has its own advantages and disadvantages.

2.1.1 Sublime Text

  • Advantages
    1. Powerful, hackable, and easy to use
    2. Can be used for free in “evaluation mode”
    3. Fast and robust, even when editing huge files/projects
    4. Works cross-platform (Windows, macOS, Linux)
    5. Backed by a profitable company that has a good track record of support and development
  • Disadvantages
    1. Free in neither the speech nor beer sense
    2. Has a mildly annoying popup that goes away only if you buy a license
    3. Costs $70 as of this writing
    4. Setting up command-line tools takes some fiddling

2.1.2 Visual Studio Code (VSCode)

  • Advantages
    1. Powerful, with lots of packages
    2. Free to use
    3. Fast and robust, even when editing huge files/projects
    4. Works cross-platform (Windows, macOS, Linux)
    5. Backed by Microsoft
  • Disadvantages
    1. Not open-source
    2. Backed by Microsoft

2.1.3 Atom

  • Advantages
    1. Powerful, hackable, and easy to use
    2. Free in both the speech and beer senses (i.e., it both costs nothing and is open-source software)
    3. Works cross-platform (Windows, macOS, Linux)
    4. Easy to set up command-line tools
    5. Backed by collaboration powerhouse GitHub
  • Disadvantages
    1. Reports of being slower in some cases than Sublime or VSCode
    2. Since GitHub’s acquisition by Microsoft, backed by Microsoft
    3. Archived by GitHub (see details below)

It’s hard to go wrong with any of these choices. Because of its simplicity and being 100% free, I used to recommend Atom as the best choice for new users. As discussed briefly in Chapter 1, though, in June 2022 GitHub announced plans to archive the Atom project in the blog post “Sunsetting Atom”. Because Atom is open-source, it’s likely that it will continue to be available in some form, but under the circumstances readers of this tutorial are especially encouraged to consider Visual Studio Code or Sublime Text as alternatives. Due to the difficulty of updating videos, the screencasts that accompany this tutorial still use Atom, but the screenshots and main examples in the written text now use Sublime Text, which is actually my main day-to-day editor. (Luckily, Sublime Text and Atom are nearly identical in many respects, even down to the exact keyboard shortcuts.)

The good news is that the skills in the sections that follow are near-universal; if you learn Atom but decide to switch to Sublime Text or VSCode (or even a cloud IDE) for your daily editing, most of the core ideas will translate easily. It is precisely because of unpredictable events like the sunsetting of Atom that Learn Enough Text Editor to Be Dangerous focuses on such general principles rather than on specific editors. As a result, the examples in this tutorial that happen to use Atom are still relevant despite this development. It also underscores the critical importance of technical sophistication (Box 1.2), which allows you to adapt to such changes without significant difficulty.

2.1.4 Exercises

Install and configure a text editor on your system as follows:

  1. Download and install either Sublime Text, Visual Studio Code, or Atom.
  2. If using Sublime Text, set up the subl command by Googling for “sublime text command line” and following the instructions for your system. Apply your technical sophistication (Box 1.2) if you get stuck. You might also find it helpful to skip ahead to Section 3.3 to learn about how to configure your system’s path.
  3. If using VSCode, set up the code command by Googling for “visual studio code command line” and following the instructions for your system.
  4. If using Atom, go to Atom > Install Shell Commands to enable the atom command at the command line (Figure 2.1).
images/figures/install_atom_shell_commands
Figure 2.1: Installing Atom’s shell commands.

2.2 Opening

To open files, we’re going to use the command configured in Section 2.1.4 to launch the editor and open the file at the same time (a method we used with vim in Section 1.3). In Section 3.4, we’ll cover a second method (called “fuzzy opening”) that’s useful when editing a project with multiple files. I’ll assume you’re using the subl command, but if you’re using a different editor you should make the appropriate substitution (e.g., atom or code in place of subl).

Let’s get started by downloading a sample file, README.md, from the Web. As in Section 2.7.1 and Section 1.6, we’ll use the curl command to download the file at the command line:

Listing 2.1: Downloading the README with curl.
$ curl -OL https://cdn.learnenough.com/README.md

As hinted at by the .md extension, the downloaded file is written in Markdown, a human-readable markup language designed to be easy to convert to HTML, the language of the World Wide Web.

After downloading README.md, we can open it at the command line as follows:

$ subl README.md

(If this doesn’t work, be sure you’ve installed the Sublime Text shell commands as shown in Section 2.1.4.) The result of opening README.md in Sublime Text should look something like Figure 2.2 or Figure 2.3. (If this is your first time opening Sublime Text, it’s also possible you’ll see a one-time greeting screen. As usual, apply Box 1.2.) Figure 2.2 shows the usual default, which is for “word wrap” to be off; because Markdown files are typically written using a long line for each paragraph, this setting isn’t ideal in this case, so I recommend turning on word wrap using the menu item shown in Figure 2.4.

images/figures/word_wrap_off
Figure 2.2: The sample file with word wrap off.
images/figures/word_wrap_on
Figure 2.3: The sample file with word wrap on.
images/figures/toggle_soft_wrap
Figure 2.4: The menu item to toggle word wrap.

In some editors, such as the cloud IDE at Cloud9, it’s more common to open files using the filesystem navigator (although in fact the c9 command can be used to open files at the Cloud9 command line).2 Double-clicking on README.md in the filesystem navigator (Figure 2.5) opens the file in Cloud9’s editor, as shown in Figure 2.6. (If your results don’t match, be sure you’ve run the curl command shown in Listing 2.1.) Figure 2.7 shows the file after clicking Navigate to close the filesystem navigator, and we see that, as in Figure 2.2, the line extends inconveniently off the screen. We can fix this using View > Wrap Lines as shown in Figure 2.8, with the word-wrapped result appearing as in Figure 2.9. (Figuring out that a menu item like View > Wrap Lines turns on word wrap is exactly the kind of thing you should be able to figure out using your technical sophistication (Box 1.2).)

images/figures/cloud9_filesystem_navigator
Figure 2.5: The Cloud9 filesystem navigator.
images/figures/cloud9_post_doublclick
Figure 2.6: Cloud9 after double-clicking on README.md.
images/figures/cloud9_word_wrap_off
Figure 2.7: Cloud9 with word wrap off.
images/figures/cloud9_wrap_lines
Figure 2.8: Turning word wrap on in Cloud9.
images/figures/cloud9_word_wrap_on
Figure 2.9: Cloud9 with word wrap on.

2.2.1 Syntax highlighting

One thing you may have noticed from inspecting Figure 2.3 and Figure 2.9 is that both Sublime Text and Cloud9 display different aspects of the file in different colors. For example, Sublime Text shows characters inside square brackets [] (which represent text for HTML links) in a lighter color than the rest of the text, while Cloud9 shows the same text in green. This is a practice known as syntax highlighting, which makes special text formatting much easier to identify visually. It’s essential to understand that this practice is strictly for our benefit; as far as the computer is concerned, the document in question is still plain text.

You might wonder how Sublime Text and Cloud9 knew which highlighting scheme to use. The answer is that they infer the document format from the file type extension (in this case, .md for Markdown). The highlighting in Cloud9’s case is quite high-contrast, but in Sublime Text’s case it isn’t particularly prominent; the most significant things are the different colors for the heading

# Sample document

and for links like

[Michael Hartl](https://www.michaelhartl.com/)

We’ll see more dramatic examples of syntax highlighting in Section 2.7 and especially in Section 3.2.

2.2.2 Previewing Markdown

As a final trick, I’d like to note that some editors can preview Markdown as HTML. This section uses Atom because its Markdown previewer is especially easy to use, but see Section 3.5 for information on how to configure Sublime Text to do the same thing.

We can figure out how to preview Markdown with Atom using our technical sophistication (Box 1.2), in this case by clicking on the Help menu and searching for “Preview” (Figure 2.10). The result is a built-in package called Markdown Preview, which converts Markdown to HTML and shows the result, as seen in Figure 2.11. In this context, it’s convenient to work with an expanded width so that both the source and the preview are wide enough to view easily, as seen in Figure 2.12. This is accomplished by mousing over the side of the Atom window to get a double-arrow icon and then dragging to increase the size. We’ll see another example of this “double-paned” setup in a more general setting starting in Section 3.4.

images/figures/help
Figure 2.10: Using the Help menu to learn how to preview Markdown.
images/figures/markdown_preview
Figure 2.11: A Markdown preview in Atom.
images/figures/wider_preview
Figure 2.12: Using a wider window for the source and preview.

2.2.3 Exercises

  1. By applying the methods in Box 1.2, find an online Markdown previewer (i.e., one that runs in a web browser), and use it to look at a preview of README.md. How do the results compare to Figure 2.11?
  2. Open a new document called lorem.txt and fill it with the text shown in Listing 2.2. Does the result have syntax highlighting?
  3. Open a new document called test.rb and fill it with the text shown in Listing 2.3. Does the result have syntax highlighting?
Listing 2.2: Some lorem ipsum text. ~/lorem.txt
Lorem ipsum dolor sit amet
Listing 2.3: A test file. ~/test.rb
puts "test"

2.3 Moving

Unlike the commands for moving around in Vim (Chapter 1, summarized in Table 1.1), the commands for moving around in modern editors generally match the techniques used in other programs such as word processors, email programs, and web browsers. As a result, it’s possible you may already know some or all of these techniques; if you don’t, by following the steps in this section you’ll get better at navigating other programs as a side effect.

To get started, let’s open the large file from Section 1.6 consisting of the full text of Shakespeare’s Sonnets:

$ subl sonnets.txt

(If this doesn’t work, you may need to run the command in Listing 1.7, and you should also verify that you’re in the right directory.) The result appears in Figure 2.13. Note that Figure 2.13 shows sonnets.txt in its own tab, with README.md from Section 2.2 occupying the other tab. Your result may vary; in any case, we’ll discuss tabs further in Section 3.4.

images/figures/atom_sonnets
Figure 2.13: Opening Shakespeare’s Sonnets in a text editor.

As with most other native programs such as word processors, web browsers, etc., you can move around a modern editor using the mouse or trackpad. You can click to place the cursor, scroll using a scroll wheel or multi-touch gestures, or click and drag the scrollbar. Figure 2.14 shows the scrollbar for Sublime Text. Figure 2.14 also shows the sort of two-pane view mentioned briefly in Figure 2.12, which we’ll discuss more in Section 3.4.

images/figures/scroll_bar
Figure 2.14: The Sublime Text scrollbar.

In addition to using the mouse or trackpad, I also like using the arrow keys to move around, typically in concert with the Command key ⌘ (Table 2.1). (In Linux, Command is typically replaced with the Function key fn, and in Windows it’s usually Ctrl, but you’ll have to apply Box 1.2 to figure out the details.) My text editing typically involves lots of ⌘← and ⌘→ to move to the beginnings and ends of lines, and ⌘↑ and ⌘↓ to move to the top and bottom of the file. An example of moving to the end of the line in README.md appears in Figure 2.15, and an example of moving to the end of the file in sonnets.txt appears in Figure 2.16.

images/figures/move_end_of_line
Figure 2.15: Moving to the end of a line with ⌘→.
images/figures/move_end_of_file
Figure 2.16: Moving to the end of the file with ⌘↓.

2.3.1 Exercises

  1. In your text editor, how do you move left and right one word at a time? Hint: On some systems, the Option key ⌥ might prove helpful.
  2. In README.md, move to the second-to-last nonblank line using whatever technique you wish. Then move to the third word from the beginning of that line.
  3. What is the command to go to a particular line number? Use this command to go to line 293 of sonnets.txt. What do rough winds do?
  4. By moving to the last nonblank line of sonnets.txt and pressing ⌘→ followed by ⌘←, show that ⌘← actually stops as soon as it reaches whitespace, with the result shown in Figure 2.17. How do you get to the true beginning of the line?
images/figures/stop_on_whitespace
Figure 2.17: When using ⌘←, the cursor stops on whitespace.

2.4 Selecting text

Selecting text is an important skill that is particularly useful for deleting or replacing content, as well as for cutting, copying, and pasting (Section 2.5). Many of the techniques in this section make direct application of the commands to move around covered in Section 2.3. As in that section, the ideas here are quite general, applying to a wide variety of applications, not just to text editors.

In much the same way that modern editors make it easy to use the mouse to move the cursor, they also make it easy to use the mouse to select text. Simply click and drag the mouse cursor, as shown in Figure 2.18. Another closely related technique is to click on one location, and then Shift-click on another location to select all the text in between.

images/figures/mouse_drag_select
Figure 2.18: The result of clicking and dragging the mouse cursor.

2.4.1 Selecting a single word

When selecting text, there are some special cases that are useful enough to consider individually. We’ll start with some techniques for selecting a single word:

  • Click and drag the mouse cursor over the word.
  • Double-click the word with the mouse.
  • Press ⌘D (system-dependent; see Box 1.2).

2.4.2 Selecting a single line

Another technique, especially important when editing line-based text like computer code (or sonnets), involves selecting a full line or collection of lines. We start with ways to highlight a single line:

  • Click the beginning of the line and drag the cursor to the end.
  • Click the end of the line and drag the cursor to the beginning
  • Press ⌘← (twice) to get to the beginning of line, then press ⇧⌘→ to select to the end of line.
  • Press ⌘→ to get to the end of line, then press ⇧⌘← (twice) to select to the beginning of line.

2.4.3 Selecting multiple lines

A comparably important technique is selecting multiple lines:

  • Click and drag the mouse cursor over the words/lines.
  • Hold down the Shift key and move the up- and down-arrow keys (⇧↑ and ⇧↓).

This latter technique is one of my personal favorites, and one of my most common editing tasks involves hitting ⌘← to go to the beginning of the first line I want to select and then hitting ⇧↓ repeatedly until I’ve selected all the lines I want (Figure 2.19). (As noted in Section 2.3.1, in many editors ⌘← stops on whitespace, so moving to the beginning of the line actually requires two uses of ⌘← in succession. Being able to figure out details and edge cases like this is a hallmark of growing technical sophistication (Box 1.2).)

images/figures/down_arrow_multiple_lines
Figure 2.19: Selecting a Shakespearean couplet using ⌘← and ⇧↓.

2.4.4 Selecting the entire document

Finally, it’s sometimes useful to be able to select the entire document at once. For this, there are two main techniques:

  • Use a menu item called “Select All” or something similar. The specifics are editor-dependent; Figure 2.20 shows the use of the Selection menu in Sublime Text.
  • Press ⌘A.

Note from Figure 2.20 that the menu actually shows the corresponding command (⌘A); bootstrapping your knowledge using the menu items is a great way to learn keyboard shortcuts, which over time will make your text editing significantly more efficient.

images/figures/selection_menu_select_all
Figure 2.20: Selecting the entire document using the Selection menu (Sublime Text).

2.4.5 Exercises

  1. Select Shakespeare’s second sonnet by clicking at the beginning and then Shift-clicking at the end.
  2. Select the first line in the file by moving to the beginning with ⌘↑ and pressing ⇧⌘→ (or the equivalent for your system).
  3. Delete the selection in the previous exercise (using the Delete key).
  4. Select the word “document” in README.md and replace it with “README”.

2.5 Cut, copy, paste

The Cut/Copy/Paste triumvirate is one of the most useful sets of operations when editing text, especially when executed via the conveniently located keyboard shortcuts ⌘X/⌘C/⌘V. (Cut/Copy/Paste are available as menu items (Figure 2.21), but the operations are so common that I strongly recommend learning and using the keyboard shortcuts right away.) Although only ⌘C is mnemonic (“C” for “Copy”), the keys are conveniently located three in a row on the bottom row of a standard QWERTY keyboard, which makes it easy to use them in combination or in quick succession (Figure 2.22).

images/figures/ccp_menu
Figure 2.21: The Cut/Copy/Paste menu items (which you should never use).
images/figures/keyboard_xcv
Figure 2.22: The XCV keys on a standard QWERTY keyboard.

Applying either Cut or Copy involves first selecting text (Section 2.4), and then hitting either ⌘X to Cut or ⌘C to Copy. When using ⌘C to Copy, the selected text is placed in a buffer (temporary memory area); moving to the desired location (Section 2.3) and hitting ⌘V lets you Paste the content into the document at the location of the cursor. ⌘X works the same way as ⌘C, except the text is removed from the document as well as being copied into the buffer.

As a concrete example, let’s select a Markdown link from the sample README file, README.md, as shown in Figure 2.23. After copying with ⌘C, we can then paste the link several times (with returns in between) by repeatedly hitting ⌘V and the Enter key, as shown in Figure 2.24. Finally, Figure 2.25 shows the result of cutting README from the main text and pasting it in at the end of the file.

images/figures/cut_and_paste
Figure 2.25: The result of cutting “README” and pasting at the end of the file.

2.5.1 Jumpcut

Although Cut/Copy/Paste is all that’s strictly necessary for everyday editing, there is one big downside, which is that there is only room in the buffer for a single string. Among other things, this means that if you Cut something and then accidentally hit “copy” instead of “paste” (which is easy since the letters are adjacent on the keyboard), you overwrite the buffer, and the text you Cut is gone forever (unless you undo as described in Section 2.6). If you happen to be developing on a Mac, there’s a solution to this problem: a free program called Jumpcut. This remarkable little utility app expands the buffer by maintaining more than one entry in the history. You can navigate this expanded buffer using either the Jumpcut menu (Figure 2.26) or the keyboard shortcuts ⌃⌥V (cycle forward) and ⇧⌃⌥V (cycle backward). I use Jumpcut dozens or even hundreds of times a day, and I strongly suggest giving it a try.

images/figures/jumpcut
Figure 2.26: Jumpcut expands the copy-and-paste buffer to include a longer history.

2.5.2 Exercises

  1. Select the entire document, Copy it, and Paste several times. The result should look something like Figure 2.27.
  2. Select the entire document and Cut it. Why might this be preferable to deleting it?
  3. Select and copy the couplet at the end of Sonnet 1 and paste it into a new file called sonnet_1.txt. How do you create a new file directly in your editor?
images/figures/many_pastes
Figure 2.27: The result of pasting the whole document several times.

2.6 Deleting and undoing

We mentioned deleting before in Section 2.4.5 (the exercises for Section 2.4), which of course simply involves pressing the Delete key, sometimes written as ⌫ (Table 2.1). As with Cut/Copy/Paste (Section 2.5), deletion is especially useful when combined with the selection techniques from Section 2.4.

In addition to the obvious technique of selecting and deleting text, on a Mac I especially like using ⌥⌫ to delete one word at a time. I’ll frequently use this combination if I need to delete a medium number of words (say 2–5) to restart a phrase when writing. For shorter deletion tasks, such as one word, it’s usually faster to hit ⌫ repeatedly, as context-switching to use ⌥⌫ incurs some overhead that makes it faster to just delete directly. Don’t worry too much about these micro-optimizations, though; with experience, as a matter of course you’ll come up with your own set of favorite techniques.

Paired with deletion is one of the most important commands in the history of the Universe, Undo. In modern editors, Undo uses the native keybinding, typically ⌘Z or ⌃Z. Its inverse, Redo, is usually something like ⇧⌘Z or ⌘Y. You can also use the menu (typically Edit, as seen in Figure 2.28), but, as with Cut/Copy/Paste (Section 2.5), Undo is so useful that I recommend memorizing the shortcut as soon as possible. Without Undo, operations like deletion would be irreversible and hence potentially harmful, but with Undo it’s easy to reverse any mistakes you make while editing.

images/figures/undo_redo
Figure 2.28: Undo and Redo in the editor menu.

One practice I recommend is using Cut instead of Delete whenever you’re not 100% sure you’ll never want the content again. Although you can usually Undo your way to safety if you accidentally delete something important, putting the content into the buffer with Cut gives you an additional layer of redundancy. (Using Jumpcut (Section 2.5.1) gives you another layer still.)

Finally, Undo provides us with a useful trick for finding the cursor, a common task when editing larger files. The issue is that you’ll be writing some text and then need to move (Section 2.3) or find (Section 2.8) elsewhere in the document. On these occasions, it can be hard to relocate the cursor. There are several ways around this problem—you can move the arrow keys, or just start typing—but my favorite technique is to Undo and then immediately Redo (⌘Z/⇧⌘Z or ⌘Z/⌘Y), which is guaranteed to find the cursor without making any undesired changes.

2.6.1 Exercises

  1. Use Undo repeatedly until all the changes you’ve made to README.md have been undone.
  2. Using any technique you want from Section 2.4, select the word “written” in README.md and delete it, then undo the change.
  3. Redo the change from the previous exercise, then undo it again.
  4. Make an edit somewhere in sonnets.txt, then scroll around so you get lost. Use the Undo/Redo trick to find the cursor again. Then keep using Undo to undo all your changes.

2.7 Saving

Once we’ve made some edits to a file, we can save it using the menu or with ⌘S. I strongly recommend using the keyboard shortcut, which among other things makes it easier to save the file whenever you reach a temporary pause in your writing or coding—a valuable habit to cultivate. Basically, if you’re not doing something else, you should be hitting Save. This habit goes a long way toward preventing lost work (and, as discussed in Learn Enough Git to Be Dangerous, is especially powerful when combined with version control).

As an example, we can add some source code to our README file and save the result. We start by pasting in the code from Listing 2.4, as shown in Figure 2.29 (which includes some nice high-contrast syntax highlighting). As you can see from the circled indicator in Figure 2.29, Sublime Text (as with most modern editors) includes a subtle indicator that the file is unsaved, in this case a small open circle. After running Save (via ⌘S, for example), the circle disappears, to be replaced with an X (Figure 2.30).

Listing 2.4: A code snippet.
```ruby
def hello
  puts "hello, world!"
end
```
images/figures/unsaved_file
Figure 2.29: An unsaved file.
images/figures/saved_file
Figure 2.30: The file from Figure 2.29 after saving.

2.7.1 Exercises

  1. Undo the pasting in of source code to restore the file to its original state.
  2. Figure out how to “Save As”, then save README.md as code_example.md, paste in the code example, and save the file.
  3. The default Bash prompt for my command-line terminal appears as in Listing 2.5, but I prefer the more compact prompt shown in Listing 2.6. In Learn Enough Command Line to Be Dangerous, I promised to show how to customize the prompt in Learn Enough Text Editor to Be Dangerous. Fulfill this promise by editing the .bashrc file to include the lines shown in Listing 2.7. Source the Bash profile as in Listing 1.5 and confirm that the prompt on your system matches the one shown in Listing 2.6. (To learn how to customize the prompt using Z shell, the current default shell on macOS, see the Learn Enough blog post “Using Z Shell on Macs with the Learn Enough Tutorials”.)
Listing 2.5: The default terminal prompt on my system.
MacBook-Air:~ mhartl$
Listing 2.6: My preferred, more compact prompt.
[~]$
Listing 2.7: The Bash lines needed to customize the prompt as shown in Listing 2.6. ~/.bashrc
alias lr='ls -hartl'
# Customize prompt to show only working directory.
PS1='[\W]\$ '

2.8 Finding and replacing

One of the most powerful features of every good text editor is the ability to find and optionally replace text. In this section we’ll learn how to find and replace in a single file; in Section 3.4 we’ll discuss the more powerful (and much more dangerous) method of finding and replacing across multiple files.

To find inside a file, you can use the Find menu, shown in Figure 2.31, which also shows that ⌘F is the corresponding keyboard shortcut. Either one brings up a modal window where you can type in the string you’re searching for (Figure 2.32).

images/figures/find_menu
Figure 2.31: Finding using the menu.
images/figures/modal_window
Figure 2.32: A modal window for finding and replacing.

For example, suppose we search for the string “sample”. As seen in Figure 2.33, both “Sample” and “sample” are highlighted. The reason our search finds both is because we’ve opted to search case-insensitively (which is usually the default).

images/figures/find_sample
Figure 2.33: Finding the string “sample”.

Figure 2.34 shows how to use the modal window to find “sample” and replace with “example”. In order to avoid replacing “Sample”, we first click on Find to select the next match, and then click on Replace to replace the second match (Figure 2.35). (Changing to case-sensitive search would also work in this case; learning how to do this is left as an exercise (Section 2.8.1).)

images/figures/find_replace
Figure 2.34: Finding and replacing.
images/figures/replaced
Figure 2.35: The result of replacing “sample” with “example”.

As seen in Figure 2.31, you can also type ⌘G to find the next match using a keyboard shortcut. This ⌘F/⌘G combination also works in many other applications, such as word processors and web browsers.

Finally, note that we have now changed “a sample” to “a example”, so as a final step we’ll change this to “an example” (Figure 2.36).

images/figures/an_example
Figure 2.36: Making “an example” of it.

2.8.1 Exercises

  1. In Section 2.3.1, we found Sonnet 18 by going directly to line 293, but of course I didn’t search the file line by line to write the exercise. Instead, I searched for “shall I compare thee”. Use your text editor to search for this string in sonnets.txt. On what line does “rosy lips and cheeks” appear?
  2. The example in this section shows one of the pitfalls of mechanically finding and replacing text: we’ve ended up with the ungrammatical result “a example” instead of “an example”. Rather than fix this by hand, use find and replace to replace “a example” with “an example” in your document. (Although in the present case there’s only one occurrence, this more general technique scales up to documents much longer than our toy example.)
  3. What is the keyboard shortcut in your editor for finding the previous match?
  4. What is the keyboard shortcut to replace in the current buffer (file)? How does this differ from the keyboard shortcut for simply finding?

2.9 Summary

  • Atom, Sublime Text, and VSCode are all excellent choices for a primary modern text editor.
  • One common way to open files is to use a command at the command line.
  • For files containing things like prose with long lines, it’s a good idea to turn on word wrap.
  • Moving around text files can be accomplished many different ways, including using the mouse and arrow keys (especially in combination with the Command/Control key).
  • One convenient way to select text is to hold down Shift and move the cursor.
  • The Cut/Copy/Paste triumvirate is incredibly useful.
  • Undo can save your bacon (Figure 2.37).3
images/figures/bacon
Figure 2.37: Undo can save your bacon.

Important commands from this chapter are summarized in Table 2.2.

Command Description
⌘← Move to beginning of line (stops on whitespace)
⌘→ Move to end of line
⌘↑ Move to beginning of file
⌘↓ Move to end of file
⇧-move Select text
⌘D Select current word
⌘A Select All (entire document)
⌘X/⌘C/⌘V Cut/Copy/Paste
⌘Z Undo
⇧⌘Z or ⌘Y Redo
⌘S Save
⌘F Find
⌘G Find next
Table 2.2: Important commands from Chapter 2.

Join the Mailing List

Get occasional notifications about things like product discounts, blog posts, and new or updated tutorials. Unsubscribe at any time.