Home Learn Linux Top 25 VI Commands: Enhance Your Text Editing Skills

Top 25 VI Commands: Enhance Your Text Editing Skills

Dive into the world of VI, the powerful text editor, with our comprehensive guide to over 25 essential commands. Whether you're a beginner or looking to refine your skills, these commands will enhance your editing efficiency, streamline your workflow, and help you navigate through files with ease.

by Arun Kumar
Published: Updated:
vi linux command

Vim, also known as VI, is a highly customizable text editor built to enable efficient text editing. It is a popular tool among programmers and developers, and is often cited as a must-know tool for anyone working with code. In this article, we will explore 25 essential VI commands that you need to know to improve your productivity and efficiency when working with text files.

These commands will help you navigate and edit text files with ease. From basic navigation to advanced editing techniques, these VI commands will take your text editing skills to the next level. So, let’s dive in and explore the essential VI commands you need to know.

What is VI?

Before we jump into the commands, let’s take a moment to appreciate what VI is. VI, or Visual Interface, is a text editor that comes pre-installed on Unix and Linux systems. It’s known for its efficiency and the ability to perform quick edits on any text file—be it a configuration file, script, or code. Although it has a steep learning curve, mastering VI can significantly improve your productivity.

editing a text file using vim editor

Editing a text file using vim editor

Essential VI Commands You Need to Know

Opening and exiting VI

1. Opening VI:

To start, open your terminal in Ubuntu and type vi filename, replacing “filename” with the name of the file you wish to edit or create.

vi myfile.txt

Sample output: This command opens myfile.txt in VI. If the file doesn’t exist, VI creates it.

2. Exiting VI:

To exit VI, you first need to press ESC to ensure you’re in command mode, then type :q! to quit without saving, or :wq to save your changes and quit.

Sample input to exit without saving:

:q!

Sample input to save and exit:

:wq

Basic navigation

3. Moving around:

Use the h, j, k, l keys for left, down, up, and right navigation, respectively. It’s like using the arrow keys but faster once you get used to it.

Personal note: I found this odd at first, but now I can’t go back to arrow keys without feeling slow!

Editing text

4. Insert mode:

Press i to enter insert mode, where you can start typing text normally.

Sample action: Press i, then type “Hello, VI world!”

5. Appending text:

Press a to append text after the cursor position.

Sample action: Move the cursor to the end of a line and press a to add more text.

Cutting, copying, and pasting

6. Cutting (yanking) and pasting:

  • To cut (or “yank” in VI terms) a line, press yy.
  • To paste the yanked line below the current line, press p.

Sample action: Yank a line with yy and paste it with p.

Deleting text

7. Deleting a character:

Press x to delete the character under the cursor.

8. Deleting a line:

Press dd to delete the entire line the cursor is on.

Sample action: Move to a line you wish to delete and press dd.

Undoing and redoing actions

9. Undoing:

Press u to undo the last action.

10. Redoing:

To redo an undone action, press CTRL + r.

Searching text

11. Searching for text:

To search for text, press /, type your search query, and press Enter.

Sample input to search for “error”:

/error

Sample output: VI highlights the first instance of “error” in the document.

Replacing text

12. Replacing a character:

Press r followed by the character you want to replace the current one with.

13. Find and replace:

To find and replace text throughout the file, use :%s/old/new/g.

Sample input to replace “hello” with “hi”:

:%s/hello/hi/g

Saving files

14. Saving changes:

To save changes without exiting, press ESC to go to command mode, then type :w.

Sample input to save:

:w

Getting help

15. Accessing VI help:

Type :help to access VI’s built-in help system.

Sample input to access help:

:help

Advanced navigation

16. Jumping to the start of the file:

Press gg to move the cursor to the first line of the file.

Sample action: Simply press gg and you’re back at the beginning.

17. Going to the end of the file:

Press G to move to the last line of the file.

Sample action: G will take you straight to the bottom, no matter where you are.

Editing at scale

18. Inserting text at the beginning of each line:

To insert text at the beginning of each line, use the :normal command. For example, to add a # at the start of every line:

:%normal I#

This command is a bit more advanced but incredibly powerful for batch editing.

Working with multiple files

19. Opening multiple files:

You can open multiple files by listing them when you open VI: vi file1.txt file2.txt.

20. Navigating between files:

  • Use :n to move to the next file.
  • Use :prev to go back to the previous file.

Visual mode

21. Entering visual mode:

Press v to enter visual mode, which allows you to select text using the navigation keys (h, j, k, l).

22. Visual line and block modes:

  • Press V to enter visual line mode, selecting entire lines.
  • Press CTRL + v to enter visual block mode, allowing column selection.

Advanced cutting and pasting

23. Cutting and pasting selected text:

  • In visual mode, select text and press d to cut.
  • Move to the desired location and press p to paste.

Incremental search

24. Incremental search:

As you type your search query after pressing /, VI will incrementally search for the text, highlighting matches as you type.

Set commands for customization

25. Customizing VI settings:

Use :set commands for various customizations, like :set number to display line numbers or :set nocursorline to disable the cursor line highlighting.

:set number

Split screen editing

26. Splitting the screen:

  • Use :split to split the screen horizontally and edit multiple files or the same file in different locations.
  • Use :vsplit to split the screen vertically.

Macros

27. Recording and using macros:

  • Press q followed by a letter (e.g., qa) to start recording a macro to that letter.
  • Perform the desired actions.
  • Press q again to stop recording.
  • Press @a to replay the actions recorded in macro a.

Sample action: If you record a macro to format a line, you can replay it with @a on any line.

Conclusion

Expanding your VI command knowledge opens up a vast landscape of editing possibilities. While the initial learning curve is steep, the payoff in terms of speed and efficiency is well worth the effort. Commands like macros and split screen editing have transformed the way I work with text files, allowing me to perform complex edits with a few keystrokes.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.