Maybe one of the first things we learn in VIM is to be able to copy a line using the command yy, but sometimes we do not want the complete line. Using the VIM Visual Editor we are able to select the text that we require and copy and paste it wherever needed. In this lesson, we are going to look at using the VIM Visual Editor to select the text that we want to be able to copy.
The example file that we use is a puppet manifest. It is not important the file that we use but we want to use practical examples rather than just random words that often accompany these tutorials. The file is short and demonstrates a conditional statement. As we want to use the or keyword to join two similar conditions together we want to select the condition and copy and paste the text. This is where the VIM visual editor will help.
Move to the Start of the Line
Although we were already at the start of the line you can use ^ to move to the start and $ to move to the end of the line. The starting point may help with navigating to the text that we want to copy. For me the starting point in near the start of the line so I would use ^ to move to the line start.
Move One Word
If we need to move just one word we can use w to move forward and b to move back a word. We can also use uppercase versions of these commands where we want to move to spaces defining the word end. This differentiates from quotes and colons that can also delimit a word.
Make the Selection Using the VIM Visual Editor
When the cursor is located on the first character of the text we want to copy we can use the command v. We then move to the right using the right arrow key or l. Of course, we can select text in other directions too. With the text selected we can now use y to copy and then paste where we need. Use p to paste after the cursor position and P to paste before. If we make a mistake then u will undo the last change.