Day 16 || vi Text Editor in Linux:  And the mode of vi editors essential shortcuts keys

Day 16 || vi Text Editor in Linux: And the mode of vi editors essential shortcuts keys

what are vi editors?

VI is a text editor commonly found on Unix-like operating systems, including Linux. It has two primary modes: command mode and insert mode. Additionally, it has a third mode called "ex mode," which is invoked by typing : in command mode.

Command Mode

  1. This is the default mode when you start VI. In command mode, you can navigate through the text and issue various commands to manipulate it. Here are some common command mode shortcuts:

    • i: Enter insert mode before the current cursor position.

    • I: Enter insert mode at the beginning of the current line.

    • a: Enter insert mode after the current cursor position.

    • A: Enter insert mode at the end of the current line.

    • o: Open a new line below the current line and enter insert mode.

    • O: Open a new line above the current line and enter insert mode.

    • dd: Delete the current line.

    • yy (or Y): Yank (copy) the current line.

    • p: Paste the yanked or deleted text after the current line.

    • /: Start a forward search for a specific term.

    • n: Move to the next occurrence of the search term.

    • N: Move to the previous occurrence of the search term.

    • :w: Save changes to the file.

    • :q: Quit VI.

    • :q!: Forcefully quit VI without saving changes.

    • :wq (or :x): Save changes and quit VI.

    • ZZ: Save changes and quit VI (shortcut for :wq)

    • Insert Mode

  2. In insert mode, you can directly type and edit the text. To enter insert mode from command mode, press i, I, a, or A, depending on where you want to start typing.

    • i: Enter insert mode before the current cursor position.

    • I: Enter insert mode at the beginning of the current line.

    • a: Enter insert mode after the current cursor position.

    • A: Enter insert mode at the end of the current line.

Ex Mode

Ex mode is accessed by typing : in command mode. It's used for more advanced operations, such as saving, quitting, and searching.

  • :w: Save changes to the file.

  • :q: Quit VI.

  • :q!: Forcefully quit VI without saving changes.

  • :wq (or :x): Save changes and quit VI.

  • :e filename: Open another file for editing.

  • :r filename: Read the contents of another file into the current document.

These are some of the most commonly used VI commands and shortcuts. VI has a rich set of features and commands, which makes it a powerful and flexible text editor. Learning and mastering these commands can significantly improve your text editing efficiency, especially in a Unix-like environment.

Did you find this article valuable?

Support Aqib Hafeez(DevOps enthusiast) by becoming a sponsor. Any amount is appreciated!