Knowing Vi Editor commands allows Linux/Unix engineers to efficiently edit configuration files, scripts, text-based files in a terminal environment
Mastering Vi Editor Commands for Linux

Knowing Vi Editor commands allows Linux/Unix engineers to efficiently edit configuration files, scripts, text-based files in a terminal environment

Vi is a standard command-line text editor for Linux and Unix that allows users to create, edit, and manage text files. The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words, it is available in all Linux distros. It is user-friendly and works same on different distros and platforms. It is a very powerful application. An improved version of vi editor is vim.

The Vi editor has two modes:

  • Command Mode: In Command mode, when you first open a file with Vi, you are in Command mode. Command mode means you can use keyboard keys to navigate, delete, copy, paste, and do a number of other tasks—except entering text.
  • Insert Mode: in Insert mode, press i key to Insert mode, you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate text. To return to Command mode, press the Esc key once.

#Note: If you are not sure which mode you are in, press Esc key twice and you'll be in command mode.

vi syntax:

$ vi filename.txt


Mode change:

  • i — Switch to Insert mode.
  • Esc — Switch to Command mode.

To save and quit:

  • :w — Save and continue editing.
  • :wq or ZZ — Save and quit/exit vi.
  • :q! — Quit vi and do not save changes.

Navigation:

  • h — Move left one character.
  • j — Move down one line.
  • k — Move up one line.
  • l — Move right one character.

Advanced commands enhance editing efficiency:

Copying and Pasting:

  • yy — Yank (copy) a line of text.
  • p — Paste a line of yanked text below the current line.
  • P — Paste before the current line
  • Xyy — Yank X number of lines.

Deleting Text:

  • x — Delete a single character.
  • dd — Delete an entire line.
  • Xdd — Delete X number of lines.

Undo and Redo:

u — Undo.

  • Ctrl + r — Redo.
  • (dot). — Repeat the last command

Search and Replace:

  • - /search_term — Search forward.
  • - ?search_term — Search backward.
  • - :s/old/new/g — Replace all occurrences of 'old' with 'new' in the current line.

Other Vi shortcuts:

  • o — Open a new line under the current line.
  • O — Open a new line above the current line.
  • A — Append to the end of the line.
  • a — Append after the cursor’s current position.
  • I — Insert text at the beginning of the current line.
  • b — Go to the beginning of the word.
  • e — Go to the end of the word.G — Go to the last line in a file.
  • XG — Go to line X in a file.
  • gg — Go to the first line in a file.
  • :num — Display the current line’s line number.


Knowing these vi commands allows Linux/Unix and DevOps engineers to efficiently edit configuration files, scripts, and other text-based files in a terminal environment.

If you like this article, please hit like and comment's.



要查看或添加评论,请登录

Shahjahan Ali的更多文章

社区洞察

其他会员也浏览了