Technical skill

Linux command: vim

Edit a text file in the terminal

Vim is a text editor that runs in a terminal. It has modes because the keyboard does two jobs: in Insert mode, letters become text; in Normal mode, keys move around the file or issue editing commands. The open text sits in a buffer, Vim's working copy in memory. You must write the buffer to save changes to the file. A common first edit is to open a practice file, press i, type a line, then press Escape to return to Normal mode. From Normal mode, i starts typing before the cursor, a starts after it, and o opens a new line below.

When you have finished typing, the Escape key leaves Insert mode. If a key moves the cursor or triggers an edit command when you meant to type, check which mode is active. vim path/to/file opens an existing file or starts a new one at that path.

Save or leave the editor

From Normal mode, type : to open Vim's command line. These commands handle common choices:

:w      write the current buffer to its file
:q      quit when there are no unsaved changes
:wq     write the file and quit
:q!     quit without saving the current buffer

:w writes the buffer; :q refuses to discard unsaved edits. :wq does both operations in order. The ! in :q! overrides Vim's safeguard and discards edits that have not been written. It does not reverse earlier saves. If :w fails, check the filename, directory permissions, and whether the file is marked read-only before considering an ownership or privilege change.

A disposable file is a good place to practice:

vim /tmp/vim-practice.txt

Press i, type a couple of lines, press Escape, then enter :wq and press Enter. Inspect the result with cat /tmp/vim-practice.txt. Repeat the edit and use :q! to see that Vim discards only the unsaved buffer changes.

Find help and text

Vim's built-in help works from Normal mode. Enter :help or a topic such as :help :write or :help usr_02.txt, then press Enter. To search within a file, type /, enter a term, and press Enter; n moves to the next match. Read nearby lines before changing a setting. Many installations include vimtutor, an interactive introduction, but some systems ship a minimal vi-compatible build or disable features. If a command behaves differently, use the local help for the installed version.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful