Exercise: Deleting Text

The course is part of this learning path

Vim Masterclass
11
6
3
Start course
Overview
Difficulty
Beginner
Duration
42m
Students
80
Ratings
5/5
starstarstarstarstar
Description

In this course, we look at the essential commands for navigating around a file and then we will work through an exercise that you can also follow along with. Then we move on to how to delete text in Vim, covering how to delete text through a range of commands, and then once again looking at a real-world exercise to give you a practical understanding of the concepts covered.

Learning Objectives

  • Learn how to navigate around files
  • Learn how to delete text

Intended Audience

This course is ideal for anyone who needs to edit files in a command-line environment.

Prerequisites

If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.

Resources

If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.

Transcript

The goal of this exercise is to practice deleting text in a file. You'll also practice the count operator motion pattern. First off start a command line session on your local machine, and then next, open practice deleting.text. It's a file that came in the course downloads. And to do that navigate to the location of the file. Now remember that this could be different for you depending on where you extracted the contents of the file. This example assumes that the course download was saved into your downloads folder just like me, and extracted from there. So first off, I'm going to CD, and to my downloads directory and then into the vimclass directory, and finally open, practicedeleting.txt. The first thing you're going to do is move to the third line now you can do this in several different ways. You can use three GGG three capital G colon, three Enter, or just like me, I'm just gonna press J a couple of times since it's so close. Now if I was going to line 44 or 138, then I'm definitely gonna use like 138GG, instead of just pressing and holding down the J key. at any rate, we are on the line that we wanna be in. And the first thing we want to do here is remove the extra K, and the word mistake, do this by positioning your cursor under one of the additional case, and just hit the X key. Now, what I'm going to do is position my cursor at the end of the line with Shift Four, which is the dollar sign. Towards of the end of the line, I'm gonna H back a couple of characters and type X to remove that extra K in that word mistake. Now we're going to move to the fourth line of the file and I'll just press J to get there. And now what we wanna do is remove the repeated letters from each of the words. There's a couple of ways to do this. I'm going to jump to the beginning of the line with zero, and just L over to the first set of characters here, press X to delete that, move over to the extra Es, delete that extra E. Finally move over to an extra L, and delete that as well with the X command. Let's move down to the next line with J and we're going to delete all the characters on this line using capital X. Now let's position our cursor at the end of the line. Again, use the dollar sign command, and now let's attempt to delete as much texts on this line as possible by using the capital X command. So really I'm just going to go Shift, capital X and just hold it down, let it repeat. And now we're at the beginning of the line, and you'll find that that last character remains on that line, and how to delete that character is to press X because that command deletes the character under the cursor. Let's position our cursor down to the next line and start working there. I'll hit J once. Now you want to position your cursor just one character right of the question mark. Now there are a few different ways to do this. I'm just gonna use the W command, and move us to the beginning of cats and then press H one time, to move back one character. Again I could, let me put my cursor at the beginning of the line here with zero, I could have easily done dollar sign to get to the end of the line, and then B to move back one word, and then H to move back one character. The goal here is to delete everything after the question mark, and there are two ways we can do this. Now we can use D dollar sign that deletes the motion to the end of the line, or we can use the shortcut which is shift D or capital D. And that's what I'm gonna use here. I'll do shift D and the cats word has been deleted. Now let's position the cursor at line 43, I'll use 43GG to go to line 43. Next we want to delete the first word of the line with the D operator. And we can use this with D lowercase W or D capital W. I want us to use D lower case W since that's fairly simple and quick, and it does what we want. So now the first word is gone. Now let's delete the second word also, we can use DW again or you could have actually used the dot command. Now we want to delete the text away, including the comma and space that come after it. And we wanna do this with two keystrokes. Now, if you remember the lowercase W motion stops at punctuation, and the uppercase W motion ignores punctuation, and stops the cursor after white space. It uses white spaces word boundaries and that's what we're after here. So to delete away and the following comma and space with just two keystrokes, we would use D Shift W or D capital W. Let's practice deleting words in like a reverse motion, and to do that let's just position our cursor at the beginning of the next word the, with a W motion, and now we're there. And let's say, we want to delete the word behind it. Well there's a couple of ways we can do that. We can use D lowercase B or D uppercase B. I'm just going to use D lowercase B and it deletes back one word. Now let's delete the words, the wild. So we want to delete two words and there's a couple of ways to do that. One is 2 D W, which repeats the delete word command twice and it gets us the result we want. Now let's delete the next two words, mountains and far. And the motion that moves you pass mountains far is two capital W, so let's use D 2 W, to delete that text. Remember the uppercase W, uses space as word boundaries and doesn't stop at punctuation. So two DW was a fairly efficient way to get that deletion completed. Now let's practice deleting lines by using DD. So let's delete the rest of this line and use DD and the line is gone. Now remember you can delete multiple lines by proceeding the DD command with account, and let's say we want to delete the next three lines. Now that's two lines that have text on them, and one line that doesn't, but it doesn't matter because those are three lines and so to delete three lines we can use three DD, the two lines with text and the blank line are all gone. They're all deleted with a 3DD command. Let's say you want to delete the next three lines with one keystroke, well how can you do that? Well, you can just simply type period which is the dot command. And it repeats the previous command, which was three DD. If you want to save your changes and keep running them you can use a colon W and Enter to save your changes and immediately exit type WQ and hit Enter. Or if you want to abandon your changes then type Q! and hit Enter. And that way you can come back and perform the exercise again later if you want. And remember, you can always hit the Escape key to abandon the command there, and go back to normal mode. I wanna encourage you to stay a few more minutes in this file and just practice deleting some more texts come up with your own exercises and own thoughts. And ask yourself questions like, what would happen if I did this, or I wanna to do this, how can I do that in the fewest amount of keystrokes? Given what I've learned so far in this course. Okay, have fun and I'll see you in the next lesson.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
3315
Courses
60
Learning Paths
7

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.