Text Objects & Macros
The course is part of this learning path
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.
This course is ideal for anyone who needs to edit text files in a command-line environment.
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
When you're editing texts, you'll often find yourself working with text constructs such as words, sentences and paragraphs. Sometimes when you're editing a file, you'll want to change an entire paragraph, or delete a sentence, or save a word into a register. When you're editing shell scripts, source code or HTML, you'll find yourself working with text that is delimited by various bits of punctuation, such as quotation marks, back ticks, brackets, curly braces and tags. For example, it's nice to have a quick way to change the value of a variable, or even replace an entire function with a bit of text. All of these objects be they sentences, paragraphs, or text enclosed within quotes are VIM text objects. These text objects or logical regions if you will, are fairly intuitive for us humans and are often very efficient blocks for us to edit. This lesson might be a little tedious. So prepare yourself. I just wanna make sure I demonstrate all the text objects so you can get the full benefit of working with these powerful text objects. Again, these text objects are fairly intuitive and easy to spot. So let's start looking at some examples so you can see for yourself. I'm going to open the text objects.txtfile and run through a few demonstrations. First, I'm going to open up a terminal here on my system and navigate to where I extracted the contents of the course download archive. For me, that's the downloads folder, and then I'll change it to the vimclass directory. And now I'll open the file with vim textobjects.txt, and press enter. As always you can follow along with me now or wait for the practice exercise that will follow. Text objects are used after an operator. It's very similar to using motions. For example, you previously learned how to delete a word by using DW, which is an operator D and a motion W. Let's place our cursor in the middle of the word compromised. And I'll do that by typing fr to put us under the letter R in compromise. Now, when you type DW, I'll do that now DW, the text that's under your cursor all the way through the motion is deleted. So, how do you delete a word if your cursor is not at the beginning of a word? Well, you use a text object. When you combine a command with a text object, the command operates on the entire object, regardless of the cursor position. Now I'm going to undo this deletion with U. Now, I'm going to type DAW. You can think of this as delete a word. You'll notice that the white space after the word was deleted, too. Let's undo this with U. If you don't want to delete the white space, then use DIW which I'll type now DIW, which stands for or delete inner word. Now, the space remains. The pattern you want to remember is this, operator followed by either an A or an I followed by an object. The A is just an A as in a word or a word. Some people like to think of A as around because it includes the delimiter around the object. Other people like to think of the A as all. The I stands for inner, and doesn't include the object delimiter. Let's undo that last delete with U. Let's say you want to change the word "compromise" to "made." The easiest way to do that would be to use CIW type the word made and press escape. Just like the AW and IW text objects, There are A capital W and I capital W text objects. Remember that the lowercase W uses spaces and punctuation as word boundaries. Whereas the capital W only uses spaces. The text object for an entire sentence is AS and for an inner sentence, it's IS. So to delete a sentence you could use DAS, which I'll type now, D A S and now that entire sentence, including the white space after it has been deleted. Let's undo that with U. Now let's use DIS I'll type that here, D I S and you can see that the sentence was deleted but the space still remains. Remember A includes the delimiter or boundaries. And I does not. Let's use DAS again, D A S. Remember to repeat a command you can use the dot command. So if we type period another sentence is deleted. I'll do that now, I'll type period. You'll notice that VIM was smart enough to include the line break as part of the sentence even though it continued on another line. Going to move down to the next group of sentences here with 2J. So a sentence boundary is a period. A paragraph boundary is a blank line. You can probably guess that a paragraph is represented by AP and an inner paragraph is represented by IP. So to delete a paragraph, you can use DAP and I'll type that now, D A P and sure enough the entire paragraph disappears. Now let's do that with U. Here's what it looks like when you use DIP. D I P using enter does not include the object boundary. So the blank line remains. You can use word, sentence and paragraph text objects to quickly edit text documents configuration files, or scripts. However, there are several text objects that are more specifically aimed at programming languages and configuration files. Let's move down to the line that begins with color and position our cursor somewhere in between the brackets. So I'll just move down with J and go to green. For example, FG. Let's say you want to change everything within the brackets. To do that, you would use CI closed bracket. It doesn't matter if you use the opening bracket or the closed bracket. I like to use the closing bracket because it's easier for me to type. In any case, it does the same thing. Now I can replace the text within the brackets. Now I'll just type new stuff here. And when I press escape, we're returned to normal mode. For example, if you want to delete everything within the brackets, including the brackets themselves you can use DA opening bracket or DA closed bracket. Moving on to the next line, we have some text within a set of parentheses. My cursor is positioned somewhere within the parentheses. Now it could even be on the opening or closing parentheses or like it is here right in the middle. Now, since this is fairly intuitive you can probably guess the text objects for parentheses are A open parentheses or A closed parentheses. And I open parentheses or I closed parentheses. VIM also classifies this text object as a block. So you can also use AB or IB. If you want to change what's in the parentheses then run CI parentheses, type some text and hit escape. So I'll do that now. I'll do CI parentheses that's inside the parentheses, hit escape to return to normal mode. And let's say we want to change everything, including the parentheses. Then I could do CA either open or closed parentheses. It doesn't matter. And now that is gone and then I can just type some other text in here. On the next line, we have some text between a less than sign and a greater than sign. Some people call these angle brackets. To access the text within angle brackets, you can use a greater than or a less than or I greater than, or I less than. So let's just yank the text from inside the brackets with YI greater than, for example. If you look at the unnamed register with :REG double quote you'll see that the characters HTML were yanked from within the brackets. If you wanted to include the brackets themselves then you would of course, need to use the A version of the text objects. So let me do that here. I'll do YA, and this time let's just use the less than symbol, for example. We'll look at the register and now you can see that the brackets were yanked as well as the text within the brackets. The next text object we're going to cover is the tag object which is represented by AT4, a tag, or IT for enter tag. If you ever find yourself editing HTML or XML you'll find this text object useful. If you were to place your cursor at the beginning of the line that starts with a P, like I have here, you could change its entire contents with CIT. I'll do that now With CIT the cursor positions itself inside the tag and you're ready to change the text. So I can do TEST for example, and press escape. Let me undo that change with U. You'll notice that there are actually nested tags on this particular line. So we have text that falls within the P tag as well as text that falls within the strong tag. Let's move the cursor over to the strong tag. So I can do F angle bracket. If you want to change everything within the tag including the tag itself, you could use CAT. Now we can use a different tag, for example, like bold or B. Press escape, and we're returned to normal mode. Now let's look at the next line. VIM understands tags as objects. So it works on custom tags like on this line. So for example, you can delete the text within the I made this up tag with DIT. Moving onto the next bit of text, We see what looks like a variable assignment. You'll see this type of thing in many programming languages. Regardless of the language, you can manipulate the data between the curly braces, with the A opening curly brace A closing curly brace, or the I opening curly brace or the I closing curly brace. VIM also considers this a block. So you can use a capital B or I capital B as well. Let's say you want to change the type of variable this is, so you want to change the braces as well as the content inside those braces. To do that, use CA and either the open or closing curly brace. Let's change this to 1. Now I press escape and I'm back to normal mode. If you wanted to change the contents inside the block or inside the curly braces, you can use CI opening curly brace, or CI closing curly brace. So let's undo this with U. And now I'll do CI opening curly brace, and that lets us change the contents inside. And for example, I could just type cats and their score is seven. Let's move on to quoted strings. The text object for a double quoted string is a double quote or I double quote. So say you want to change the name of the backup server from deepfreeze01 to backup1. Well, to do that you would just position your cursor somewhere within that string and type CI double quote, followed by the content. So I'll just do four to the double quote and then I can do CI double quotes. And now I just type in my replacement text and hit escape when I'm done. Let's move down a line and change mail13 to newmail1. Now the single quoted string text object is either A single quote or I single quote. Again, this is pretty intuitive stuff. So let's change it with CI single quote. And now we'll just type newmail1 and press escape when we're done. Okay, we've made it to the end. This is the last quick example and I can pretty much bet you know how to access the data within the back ticks. Yep. Use A back tick or I back tick. Let's say you wanted to change uname -n to hostname. You could type CI back tick, followed by host name and then hit escape when you're done. Okay, that wraps it up. Those are all the built-in VIM text objects. To quickly review, a text object is used in two forms, an A form and an I form. The A is just an aid as in a word. Some people like to think of A as around because it includes the delimiter around the object. Other people like to think of the A as all. The I stands for inner, and doesn't include the object delimiter.
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.