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


저자 소개

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.

UI_Icon-Red_Hat-Close-A-Black-RGB

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Virtualization icon

가상화

온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래