image
Exercise: Text Objects
Exercise: Text Objects
Difficulty
Intermediate
Duration
50m
Students
133
Ratings
3.6/5
Description

This course looks at text objects and macros in Vim, both of which are tools intended to help you edit text more quickly and efficiently. Text objects allow you to make changes to words, phrases, and sections of text based on various parameters. We then look at macros, which allow you to repeat preset series of commands in order to save time.

For both text objects and macros, we will walk you through some examples of how they are used in order to give you a practical understanding of them.

Intended Audience

This course is ideal for anyone who needs to edit text 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 give you some practice with Vim's text objects. First let's start a command line session on your local machine and then use Vim, to open the textobjectspractice.txt file, that came in the course downloads. And to do that, navigate to the location of the file and remember that this could be different for you depending on where you extracted the contents of the file on your system. But for me, it's in my downloads directory so I'll change into there first, then into the vimclass. And now, I'm going to open the file. First we're going to start out by positioning our cursor somewhere in the word time on the first line. For example, we could use FI to move to the first occurrence of I, which happens to be in the word time. Now let's delete the word with D A W. Notice how the entire word is deleted. This is different than deleting a motion, with D W for example. Next let's change the word traveler to tourist. And to do that, we can use C I W for change inner word, and then type tourist, and finally hit Escape to return to normal mode. Next position your cursor on the opening parenthesis, by typing W. Now, change the text within the parenthesis to read as we will call him. And to do that, we can use C I, open parentheses, or C I closed parentheses. And this allows you to change the block of texts within the parentheses. Notice how your cursor is placed inside the parentheses to make it easy for you to make this change. Now as we have to do is type, as we will call him and press Escape to return to normal mode. Now let's move to the next line in the file that has some text in it, and let's place our cursor anywhere on or within the parentheses. And one way to do that is with a forward search. So I can do forward search, for the opening parentheses and then press Enter. Now let's delete the opening parentheses the closing parentheses and everything in between. And to do that we can use DA open parentheses or DA close parentheses. Now let's move to the next line and place our cursor anywhere, either on the double quotes or inside the double quotes. One quick way to do that is a forward search for the double quote and I'll hit Enter. And now what we wanna do is change the text within the double quotes so we can use C I double quote. Again, our cursor is positioned right where we need to insert our text, and then we can type our replacement texts which is, it was cold. And hit Escape to return to normal mode. Now let's move to the next line, but this time let's only change what appears within the single quotes. Again, I'm just going to use a forward search to get there quickly, forward search for a single quote, and press Enter to move my cursor there. Now I'm gonna change everything inside those single quotes with C I single quote, and then I will type, it is hot outside and press Escape to return back to normal mode. Next let's place our cursor somewhere within the brackets here and I'll just do a forward search for bin because that appears multiple places within the brackets. And now what we wanna do is delete all the contents within the brackets. And so to do that, you can use D I, opening bracket or D I close bracket whichever one is easiest for you. Now let's move down to the line that says yank me and now what we're going to do is place the text within that block in the I register. So to do that, we can use double quote I for the I register, Y for yank, and now either the opening angle bracket or the closing angle bracket. I'll just use the closing one here. Now it's confirmed that the text yank me is in the I register with colon reg, Space I and hitting Enter. Yep, sure enough. Yank underscore me is in the I register. Now let's place that same text, including the angle brackets into the A register. And to do that we can use double quote A, Y A and either the less than or greater than sign. Now we can check the register with colon R E G A and sure enough, we included the text as well as the delimiters, with our command. Now let's change linuxtrainingacademy, to L T A on the next line, and you'll notice that, that is the content of the, A tag. So what we can do is move anywhere into the A tag. And one way to do that is just do a forward search and I can do forward search H, for example and that will put us right in the A tag somewhere. And now what we can do to change what's inside that tag, is use C I T. And our cursor is positioned right where we need to type so we'll just type L T A, and press Escape to get back to normal mode. I'm going to move to the next block of text here. I'm just going to move it up our screen with Z and Enter. Our goal here is to delete the entire entry for the first CD, titled Jazz At Massey Hall. To do that we need to position our cursor under the opening CD tag or the closing CD tag. And I'll just do that with forward search CD and press Enter. Now what we can do is simply type, D A T, which stands for delete attack. I'd like to point out if you happen to position your cursor elsewhere, you would delete one of the nest attacks, for example if you were inside the title here, add a D A T, it would just delete that tag and not the entire CD tag. So I'm gonna hit U to undo that change there. Now I'm gonna go down to the next block of text that begins with musicians. I'll just forward search there and hit Enter. Now what I would like to do is just delete the entire contents of what is included, inside those braces. And to do that, we need to place our cursor anywhere inside those braces and so, for example we could do a forward search for alto and that would place our cursor there. Now we can delete that block with D I opening braces or D I closed curly brace. Now we're left with the braces on different lines and if we wanna collapse those, we can just hit K to move up to the next line, and capital J to join those two lines together. Our next goal is to yank the next sentence of text into the S register. And to do that we just need to position our cursor somewhere within the sentence. And so I'll just hit J a couple of times in order to position my cursor in that sentence. You'll notice that this sentence isn't really an English or any other language for that matter. And just remember that Vim is looking for text object boundaries, not what is in those boundaries. A sentence in Vim is defined as ending at a period, an exclamation mark or a question mark, followed by either the end of a line or by a Space or Tab. So to yank this sentence into the S register, we can use double quote S Y A S which stands for yank a sentence. And we can confirm that that worked with colon reg, S and pressing Enter. Now let's delete that entire paragraph with three simple characters, D A P which stands for delete a paragraph. At this point if you wanna abandon your changes so you can go back and try this practice again some other time, hit colon Q exclamation mark and Enter.

About the Author
Students
21097
Courses
61
Learning Paths
18

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.