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.

Gift Delivery Options
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
  • Text Editor

Learn Enough Society

Certificate of Course Completion

This page certifies that mhartl has completed Learn Enough Text Editor to Be Dangerous! 🎉

About the tutorial
Learn Enough Text Editor to Be Dangerous by Michael Hartl is designed to help you learn to edit plain text files using a text editor, arguably the most important tool in the software developer’s toolkit. Unlike other text editor tutorials, which are typically tied to a specific editor, this tutorial is designed to introduce the entire category of application—a category many people don’t even know exists. Read full post
14 Dec 12:49, 2015
11 Oct 21:17, 2016
Exercise Answers
Exercise Question:
  • Start Vim in a terminal, then run the Most Important Vim Command™.
  • mhartl's Answer:
    Exercise Question:
  • Restart Vim in a terminal. Before typing anything else, type the string “This is a Vim document.” What happened? Confusing, right?
  • mhartl's Answer:
    Exercise Question:
  • Use the Most Important Vim Command™ to recover from the previous exercise and return to the normal command-line prompt.
  • mhartl's Answer:
    Exercise Question:
  • Use the arrow keys to navigate to Line 4 in the file from Listing 1.3.
  • mhartl's Answer:
    Exercise Question:
  • Use the arrow keys to go to the end and then the beginning of Line 4. Cumbersome, eh?
  • mhartl's Answer:
    Exercise Question:
  • Go to the beginning of Line 4 by using the command mentioned in the text.
  • mhartl's Answer:
    Exercise Question:
  • Go to the end of Line 4 using the command mentioned in the text.
  • mhartl's Answer:
    Exercise Question:
  • Define an alias g for the commonly used case-insensitive grep grep -i. What happens if, after making your changes and hitting ESC, you issue the command :wq instead of :w and :q separately?
  • mhartl's Answer:
    Exercise Question:
  • You may recall the curl command from Learn Enough Command Line to Be Dangerous, which lets us interact with URLs via the command line. Define get as an alias for curl -OL, which is the command to download a file to the local disk (while following any redirects encountered along the way).
  • mhartl's Answer:
    Exercise Question:
  • Use the alias from the previous exercise to execute the command shown in Listing 1.6, which downloads a longer text file for use in Section 1.6.
  • mhartl's Answer:
    Exercise Question:
  • Using Vim, open a new file called foo.txt.
  • mhartl's Answer:
    Exercise Question:
  • Insert the string “A leopard can’t change it’s spots.” (Figure 1.10).19
  • mhartl's Answer:
    Exercise Question:
  • Using the x key, delete the character necessary to correct the mistake in the line you just entered. (If you can’t find the error, refer to Table 1.1.)
  • mhartl's Answer:
    Exercise Question:
  • Use dd to delete the line, then use p to paste it repeatedly into the document.
  • mhartl's Answer:
    Exercise Question:
  • Save the document and quit using a single command. Hint: See the first exercise in Section 1.4.1.
  • mhartl's Answer:
    Exercise Question:
  • With sonnets.txt open in Vim, move down three screens and then back up three screens.
  • mhartl's Answer:
    Exercise Question:
  • Go to the end of the file. What is the last line of the final sonnet?
  • mhartl's Answer:
    Exercise Question:
  • Navigate back to the top to change the old-style name “Shake-speare” on Line 1 of sonnets.txt to the more modern “Shakespeare”, and save the result.
  • mhartl's Answer:
    Exercise Question:
  • Use Vim’s search feature to discover which sonnet contains references to Cupid, the Roman god of love.
  • mhartl's Answer:
    Exercise Question:
  • Confirm that 18G goes to the final line of the first sonnet. What do you suppose that command does? Hint: Recall that 1G goes to the beginning of the file, i.e., Line 1.
  • mhartl's Answer:
    Exercise Question:
  • Open sonnets.txt.
  • mhartl's Answer:
    Exercise Question:
  • Go to the last line.
  • mhartl's Answer:
    Exercise Question:
  • Go to the end of the last line.
  • mhartl's Answer:
    Exercise Question:
  • Make a new line that says “That’s all, folks! Bard out. <drops mic>”. Make sure to move the cursor one space to the right so you don’t drag the final period along.
  • mhartl's Answer:
    Exercise Question:
  • Write out the file.
  • mhartl's Answer:
    Exercise Question:
  • Undo your changes.
  • mhartl's Answer:
    Exercise Question:
  • Write out and quit the file.
  • mhartl's Answer:
    Exercise Question:
  • Reopen the file and type 2620dd.
  • mhartl's Answer:
    Exercise Question:
  • Realize that you just deleted the entire file contents, and apply the Most Important Vim Command™ to ensure that no damage is done.
  • mhartl's Answer:
    Exercise Question:
  • Download and install either Sublime Text, Visual Studio Code, or Atom.
  • mhartl's Answer:
    Exercise Question:
  • If using Atom, go to Atom > Install Shell Commands to enable the atom command at the command line (Figure 2.1).
  • mhartl's Answer:
    Exercise Question:
  • 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.
  • mhartl's Answer:
    Exercise Question:
  • Open a new document called lorem.txt and fill it with the text shown in Listing 2.2. Does the result have syntax highlighting?
  • mhartl's Answer:
    Exercise Question:
  • Open a new document called test.rb and fill it with the text shown in Listing 2.3. Does the result have syntax highlighting?
  • mhartl's Answer:
    Exercise Question:
  • 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?
  • mhartl's Answer:
    Exercise Question:
  • 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.
  • mhartl's Answer:
    Exercise Question:
  • 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.
  • mhartl's Answer:
    Exercise Question:
  • 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?
  • mhartl's Answer:
    Exercise Question:
  • 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?
  • mhartl's Answer:
    Exercise Question:
  • Select Shakespeare’s second sonnet by clicking at the beginning and then Shift-clicking at the end.
  • mhartl's Answer:
    Exercise Question:
  • Select the first line in the file by moving to the beginning with ⌘↑ and pressing ⇧⌘→ (or the equivalent for your system).
  • mhartl's Answer:
    Exercise Question:
  • Delete the selection in the previous exercise (using the Delete key).
  • mhartl's Answer:
    Exercise Question:
  • Select the word “document” in README.md and replace it with “README”.
  • mhartl's Answer:
    Exercise Question:
  • Select the entire document, Copy it, and Paste several times. The result should look something like Figure 2.27.
  • mhartl's Answer:
    Exercise Question:
  • Select the entire document and Cut it. Why might this be preferable to deleting it?
  • mhartl's Answer:
    Exercise Question:
  • 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?
  • mhartl's Answer:
    Exercise Question:
  • Use Undo repeatedly until all the changes you’ve made to README.md have been undone.
  • mhartl's Answer:
    Exercise Question:
  • Using any technique you want from Section 2.4, select the word “written” in README.md and delete it, then undo the change.
  • mhartl's Answer:
    Exercise Question:
  • Redo the change from the previous exercise, then undo it again.
  • mhartl's Answer:
    Exercise Question:
  • 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.
  • mhartl's Answer:
    Exercise Question:
  • Undo the pasting in of source code to restore the file to its original state.
  • mhartl's Answer:
    Exercise Question:
  • Figure out how to “Save As”, then save README.md as code_example.md, paste in the code example, and save the file.
  • mhartl's Answer:
    Exercise Question:
  • 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”.)
  • mhartl's Answer:
    Exercise Question:
  • 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?
  • mhartl's Answer:
    Exercise Question:
  • 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.)
  • mhartl's Answer:
    Exercise Question:
  • What is the keyboard shortcut in your editor for finding the previous match?
  • mhartl's Answer:
    Exercise Question:
  • What is the keyboard shortcut to replace in the current buffer (file)? How does this differ from the keyboard shortcut for simply finding?
  • mhartl's Answer:
    Exercise Question:
  • Add some more lorem ipsum text to README.md using a tab trigger.
  • mhartl's Answer:
    Exercise Question:
  • Add another occurrence of the word “consectetur” using autocomplete.
  • mhartl's Answer:
    Exercise Question:
  • Write the sentence “As Cicero once said, ‘quis nostrud exercitation ullamco laboris’.” with the help of as many uses of autocomplete as you want.
  • mhartl's Answer:
    Exercise Question:
  • Create the file foo.rb, then define the class FooBar (Listing 3.3) using a tab trigger. Hint: Chances are the trigger is something like cla⇥.
  • mhartl's Answer:
    Exercise Question:
  • Referring to Listing 3.4, add the definition of bazquux using the def⇥ trigger, then add the final line shown by using autocomplete to type FooBar and bazquux. (Type the interstitial .new. by hand.)
  • mhartl's Answer:
    Exercise Question:
  • Using tab triggers and autocomplete, make a file called greeter.rb with the contents shown in Listing 3.5.
  • mhartl's Answer:
    Exercise Question:
  • By cutting and pasting the text for the hello definition and indenting the block, transform Listing 3.5 into Listing 3.6.
  • mhartl's Answer:
    Exercise Question:
  • Let’s test the functionality of ekill by making a process that hangs and applying the lessons from grepping processes in Learn Enough Command Line to Be Dangerous. We’ll start by opening two terminal tabs. In one tab, type tail to get a process that just hangs. In the other tab, use ps aux | grep tail to find the process id, then run ekill <pid> (substituting the actual id for <pid>). In the tab running tail, you should get something like “Terminated: 15” (Figure 3.20).
  • mhartl's Answer:
    Exercise Question:
  • Write an executable script called hello that takes in an argument and prints out “Hello” followed by the argument. Be sure to chmod the script so it can run properly. Hint: Use the echo command. Bigger hint: Bash scripts interpolate dollar-sign variables into strings, so the $1 variable from Listing 3.8 can be used in a string like this: "Hello, $1".
  • mhartl's Answer:
    Exercise Question:
  • What is the keyboard shortcut in your editor for toggling the tree view?
  • mhartl's Answer:
    Exercise Question:
  • What is the keyboard shortcut in your editor for splitting panes horizontally?
  • mhartl's Answer:
    Exercise Question:
  • In the Rails Tutorial sample app project, open the file static_pages_controller.rb using fuzzy opening.
  • mhartl's Answer:
    Exercise Question:
  • Use global find to find all occurrences of the string @user.
  • mhartl's Answer:
    Exercise Question:
  • Use global replace to change all occurrences of @user to @person.
  • mhartl's Answer:
    Exercise Question:
  • Figure out how to change the syntax highlighting theme in your editor. Use the file from Listing 3.6 to confirm the change.
  • mhartl's Answer:
    Exercise Question:
  • Use a regex match to annotate all function definitions with # function definition as described in the text.
  • mhartl's Answer:
    Exercise Question:
  • If using VSCode, set up the code command by Googling for “visual studio code command line” and following the instructions for your system.
  • mhartl's Answer:

    Join the Mailing List

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