While editing text files, we might want to do several actions like deleting lines, finding and replacing text, selecting text, etc. And depending upon the editor, there could be several shortcuts available for these common tasks.
In this tutorial, we’ll check how we can select all text in a file using the vim editor.
2. Speaking Vim
While working with vim, we speak a language. For this, we have verbs, modifiers, and objects.
As a result, if we wanted to do anything in vim, we first put that in words. Then we use the related verbs and modifiers to accomplish that.
Let’s say we need to delete a word. For this, we’ve got the verb d to delete and w for the object word. Hence, by executing the dw command, we get the result we wanted.
In the above example, we saw a very simple action. Similar to this, vim supports a wide range of verbs, objects, and motions. Using these in combinations, we can achieve a complex task.
3. Selecting Using Visual Mode
Firstly, let’s check how we can select lines using the visual mode.
The vim editor supports different modes. By tapping different keys, we can access these modes.
In vim, visual mode helps us select a set of lines or characters and execute different operations on it. Therefore, it’ll be quite convenient to visualize the area of text upon which our commands will take effect.
We can instruct vim to select lines while going to visual mode: V
- V: visual mode line-wise
We can set a count to select that many lines before going to visual mode: 10V
This will select 10 lines from the current cursor position.
4. Selecting a Line
To select a line, we can use this command combination: 0vj
- 0: (zero) to move to the beginning of the line
- v: to switch to visual mode character-wise
- j: to move the cursor down
As the cursor moves down, it selects the whole line. We can do this for all the lines in the file to select the whole content.
We can also bring in a count before the ‘j’ verb to select an n number of lines: v10j
This will move the cursor 10 lines down, selecting all those lines. We can also give a bigger count and select more lines.
Better yet, we can press ctrl+g, which shows the current cursor position. It also shows the total number of lines in the file. We can give the total number of lines as the count to select the entire contents of the file.
5. Selecting a Sentence
Selecting the full content line by line is tedious when you have a big file with many lines.
In this section, let’s check how we can select a sentence: vas
- as: around the sentence
This will select a sentence. The beauty of this command is that it selects the whole sentence even if the cursor is in the middle.
Repeating ‘as‘ many times, we can select all the contents of the file.
Here also, we can set a count before the ‘as’ command to select any number of sentences: v10as
With this, it selects 10 sentences. We can give a bigger count to select more sentences and eventually the entire file.
6. Selecting a Paragraph
The is exactly the same as the above. Instead of giving a sentence as the object, we give a paragraph.
For this, the key combination is: vap
- ap: around the paragraph
This will select a paragraph.
Here also, we can set a count before the ‘ap’ command to select any number of paragraphs: v10ap
With this, it selects 10 paragraphs.
7. Selecting All Lines
Selecting the contents by sentence or by paragraph is cumbersome, especially when the file spans multiple pages.
We’ve seen selecting lines using visual mode in section 3. If we extend this a little, we can select the entire file content.
Let’s look at that: ggVG
Here we’ve grouped a few commands. This selects all the text in the file. Let’s dissect this and check how it works:
- gg: go to the first line
- V: start visual mode
- G: go to the last line
Thus, by going to the last line, it selects all of the file contents.
In this article, we’ve seen different ways we can use vim to select contents in a file. As we’ve seen, we could select by lines, paragraphs, and also the entire file.