How to Select All in Vim / Vi

vi and vim logos on gray background gradient

Knowing how to select all content in Vim or the Vi editor enables you to complete routines like copying and pasting in Linux quickly. The process can be tricky if you don’t understand how to use the editors properly or bind keys.

For instance, you can select all in Vim/Vi by combining the gg, V, and G keys.

ggVG

Before that, you must be in the normal mode and know what the groups of keys mean or do.

This article takes you through Vim/Vi modes, commands, and key bindings. You will find it simpler to select and use file contents with this knowledge.

Switch Modes

Vi is the most familiar editor for all Linux distributions. On the other hand, Vim is an improvement of the Vim editor. That implies you don’t need to install the Vi editor, and Vi traits exist in Vim with little modifications. But you can check if Vim exists on your computer and install it if it does not exist.

vim --version

Notes/Articles/How to Select All in Vim Vi/install vim.png

Vim/Vi exist in three modes: normal, insert, and visual. The normal (also called the command) mode enables you to run commands. The editor enters the normal mode whenever you open a file with it.

vim file.txt

Vim checks the file’s existence or creates the file before opening it.

While inside the file, you can write content by entering the insert mode by pressing the i, a or o keys.

  • i: write before the current cursor position,
  • a: write at the end of the cursor position, or
  • o: write on a new line after the current line.

After you are done writing, you should return to the normal mode before saving and quitting wq or quitting without saving q! the file. Alternatively, you can run more commands on the file or enter the visual mode by pressing the v key.

The visual mode enables you to select multiple lines without using the mouse. It would be best to understand the concept of key bindings before diving into selecting all lines in Vim/Vi editors.

Bind Keys

Key bindings speed up using the Vim/Vi editors. For example, one of the longest routes you can take to delete three words is entering the insert mode, moving to the end of each of the three words then pressing the backspace key for every word and the space separating it with other words.

Alternatively, you can position the cursor before the target word(s) then bind the d, 3 and w keys to delete the three words without switching modes.

d3w

Also, numbers can be used for key bindings. For example, 5 followed by the down arrow keys moves the cursor 5 lines down the page from the current line.

Lab Setup to Practice Selecting All in Vim/Vi

This section will mimic an environment to select all or some lines in Vim and manipulate the selected lines.

Open your terminal by searching the startup menu or typing the ctrl+alt+t keys. Next, create a file called auth.js and open it with Vim.

touch auth.js
vim auth.js

Enter the insert mode and write the following content in the file.

export const auth = (req, res, next) => {
    if(req.isAuthenticated()){
        next()
    }else {
        res.redirect('/users/login')
    }
}

Notes/Articles/How to Select All in Vim Vi/insert content.png

Now that we have a file with some lines, let’s practice selecting lines.

Select and Manipulate files in Vim/Vi

Example~1: Select all contents of a file, then copy-paste the selected content.

Assume you want to select all the file lines and copy and paste them below the current last line. You can do that using these steps.

Select all

Move to the beginning of the file content.

gg

Enter the Visual Line mode.

V

And select (all) lines to the end of the content.

G

Notes/Articles/How to Select All in Vim Vi/use ggVG key bindings.png

Copy

Copy the selected content using the yank command.

y

The bottommost left part of the page tells you the number of copied lines. For example, since the above lines are seven, you see 7 lines yanked at the bottom of the interface.

Paste

Switch to the insert mode.

i

And create a space after the current content by pressing the enter key.

enter

Then, return to the normal mode by pressing the escape key esc.

esc

Finally, paste the content using the paste command.

p

Notes/Articles/How to Select All in Vim Vi/7 more lines.png

How to select all in Vim/Vi without entering the visual mode

Alternatively, you could select all lines without highlighting the selected content and then pasting it using gg99999yyp key bindings, which can further be broken down into portions as follows:

gg: Move to the beginning of the file.

99999: Move down the page by 99999 lines. That implies select everything since the above file’s lines are less than 99999.

yy: the selected content.

p: Paste the selected content after the current cursor position.

The shorter alternative to copying everything is to use the % sign followed by the yank command.

:%y

Then paste the content.

p

Example~2: Select all lines up to a particular line

You may want to select all lines to a specific line. You can start by numbering the lines. Enter the normal mode, then run the set number command.

:set number

Assume you want to select all lines up to the fifth line. You can do that by binding the 5 and V keys.

5V

Notes/Articles/How to Select All in Vim Vi/select all till a particular line.png

Example~3: Select all contents of a line and delete it.

Write the following words in a new line.

I am about to select and delete this line.

Return to the normal mode esc before entering the Visual mode v. Using the left or right arrow keys, select the newly created line.

Notes/Articles/How to Select All in Vim Vi/select line.png

Then delete it by pressing the d key.

Alternatively, you could select and delete all content by binding the % command to the d key.

:%d

Conclusion

Selecting all in Vim/Vi is easy after knowing how to switch modes and bind keys. Then, there are multiple ways to select, copy-paste, or delete the target content.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Source Command
Read More

Bash Source Command

The Bash source command reads and executes commands from a specific file as an argument within the current…