Subscribe to the feed

In the Linux world, most work is accomplished from a CLI. One of our main tasks is to manage file content. To accomplish this, we need a tool to edit files. One of the most common editors in the Linux world is the Vi editor, also known as a visual editor. Let's learn about some of the basic Vi operations before looking at my favorite commands.

What is the Vi editor?

A visual editor allows users to write and manipulate text in a file in a Unix-based operating system. An improved version of Vi is called the Vim editor. We can download Vim in Red Hat Enterprise Linux 8 (and similar distributions) by using dnf install -y vim.

How do I use this editor?

Using this editor is quite simple. Type vi file-name, and the editor opens. One advantage of this editor is that we can manipulate text without using a mouse. We only need the keyboard. Let's start this great tool right away.

# vi filename

This command opens up a brand new file named filename. The file looks like this:

Empty file in Vim

Here you see a ~, which means those lines are unused. At the bottom of the page, we see the filename and [New File]. To start writing content into the file, simply press i (insert). Now the file goes to edit mode.

Basic commands

I have written five basic commands that I use with vim:

To copy text, use yy and yw:

  • yy - Copies the current line.
  • yw - Copies the current word from the character the lowercase w cursor is on, until the end of the word.
  • p - Pastes the copied text.

To move to the start or end of the file:

  • :1 - Moves the cursor to the start of the page.
  • :$ - Moves the cursor to the end of the page.

To search for a particular string in the file:

  • / - For example, if you want to search for the string redhat in a given file, you type: /redhat or red* to take you to the matching string.

To set the line numbers and paste complex texts inside the file:

  • set num - Sets the line numbers.
  • set paste - Pastes a bulk of text from some other location.

To move forward and backward for one fullscreen:

  • Ctrl+f - moves forward by fullscreen.
  • Ctrl+b - moves backward by fullscreen.

Intermediate commands

I have written three advanced commands that I use with the Vim editor:

To undo and delete a particular line:

u - Undo the previously executed command.

dd - Deletes the current line of text.

To rename the file:

:f filename - Helps you to rename the file.

To toggle between two open files:

:e # - Helps you to navigate/toggle between two open files.

Conclusion

In this article, I covered the most common commands that I use in my day-to-day work with the Vim editor. By no means does this include all the available commands. Feel free to check the man page for other helpful commands as per your scenario.

[ Free online course: Red Hat Enterprise Linux technical overview. ]


About the author

I work as a Solutions Engineer at Red Hat and my day-to-day work involves OpenShift and Ansible. I'm highly passionate about open source software, cloud, security, and networking technologies.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech