Skip to main content

Vim: Basic and intermediate commands

Here are some of my most frequently used commands in the Vim text editor.
Image
vim 2.0
Image by ROBERT SŁOMA from Pixabay

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:

Image
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. ]

What to read next

Topics:   Text editors  
Author’s photo

Shashank Nandishwar Hegde

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. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX