Linux vim Command


The vim command in Linux opens the Vim text editor, a highly configurable, powerful text editor widely used by developers. Unlike nano, Vim has multiple modes (such as Normal, Insert, and Command modes) that provide specialized controls for navigation, editing, and executing commands.

Vim Modes

  • Normal Mode: The default mode when you open Vim, used for navigating and executing commands.
  • Insert Mode: Used for typing text, like a regular editor.
  • Command Mode: Used for saving, quitting, and other command executions.

Basic Syntax

vim filename

Example Workflow with Output

1. Opening a File

Opening a file in Vim starts in Normal Mode.

Command
vim file.txt
Output (On-Screen in Vim)
~ ~ ~ "file.txt" [New File]

Explanation:

  • vim file.txt opens file.txt. If the file doesn’t exist, it opens a new file.
  • The ~ symbols indicate empty lines, and [New File] confirms it’s a new file.

2. Switching to Insert Mode and Adding Text

To enter Insert Mode for typing, press i.

Steps
  1. Press i to enter Insert Mode.
  2. Start typing: “This is a new file.”
Output
This is a new file.

Explanation:

  • After pressing i, you enter Insert Mode, where you can add text.
  • When done typing, press Esc to return to Normal Mode.

3. Saving the File and Exiting Vim

In Normal Mode, use :w to save and :q to quit.

Command
:wq
Output
"file.txt" [New File] 1L, 18C written

Explanation:

  • :wq (write and quit) saves changes and exits Vim.
  • The message 1L, 18C written indicates 1 line and 18 characters saved to file.txt.

4. Exiting Without Saving

To quit without saving changes, use :q!.

Command
:q!

Explanation:

  • :q! discards any changes made and closes the file.
  • This is useful if you’ve made changes by mistake.

5. Navigating Within a File in Normal Mode

In Normal Mode, you can move the cursor using Vim-specific keys.

KeyDescription
hMove left
jMove down
kMove up
lMove right
GMove to the last line of the file
ggMove to the first line of the file
Example

Pressing j moves the cursor down, while k moves it up. This can be done repeatedly to navigate through a large file.

6. Searching for Text

To search for text, use / followed by the search term in Normal Mode.

Command
/searchterm
Output (highlighted text in Vim)
This is a new file. Here is the searchedterm.

Explanation:

  • /searchterm highlights the first instance of “searchterm” in the file.
  • Press n to go to the next occurrence, or N to go to the previous one.

Summary of Common Vim Commands

CommandDescription
iEnter Insert Mode to type text.
EscReturn to Normal Mode.
:wSave (write) the current file.
:qQuit Vim if no unsaved changes.
:wqSave and quit.
:q!Quit without saving changes.
/textSearch for "text" in the file.
nFind the next instance of the search term.
GMove to the last line of the file.
ggMove to the first line of the file.

Summary

The vim editor provides powerful editing and navigation capabilities in Linux through modes and commands. It may have a learning curve, but mastering its commands and shortcuts makes Vim an efficient tool for managing text files.