Getty Images/iStockphoto

How to use Vim in Linux

This tutorial explains basic functions and commands used in Vim to complete simple tasks. Follow along to learn some beginner tips on how to install and use Vim in Linux.

Vim is a powerful text editor that's prevalent among developers and system administrators, especially in Linux environments. Known for its efficiency and extensive customization options, Vim can initially seem daunting for beginners. However, with practice and understanding of its unique functionalities, users can create, edit and navigate files with ease.

This tutorial will guide you through the basics of using Vim on Linux. It covers essential functions and commands, taking you from installation to key features. By the end of this article, you will have the foundational knowledge needed to edit files effectively with Vim, along with tips to enhance your overall experience.

How to install Vim

If you're using Linux and need to install Vim, use the system's package manager to add it. On a Red Hat-based system, type the following:

$ sudo dnf install vim

If you use a Debian-based system, enter the following:

$ sudo apt-get install vim

To add Vim using Brew, type the following:

$ brew install vim

You can download the Vim installer for Windows from the official site. Double-click the installer to launch the installation process.

Vim modes

Many Linux servers do not use a GUI. No drop-down menus exist to save the file or initiate a spell-check process. Instead, users rely on the keyboard to manipulate the file.

Vim accomplishes file manipulation by using modes. Think of modes as keyboard mappings. Depending on the Vim mode, the keyboard will react differently to keystrokes. For example, in Vim's Command mode, the X key deletes a character, but in Vim's Insert mode, the X key places the X character in the file's text.

Vim has four main modes:

  • Normal. This is the default mode used for navigation, text manipulation and entering other modes.
  • Command-line. This mode issues commands to Vim. Type : and enter commands at a prompt. For example,:wq saves the file and quits Vim.
  • Insert. This mode inserts text into a file. Press the i key to enter Insert mode. Several keystrokes initiate this mode, providing different cursor placements or line creation.
  • Visual. This mode enables the use of keyboard commands to edit selected text. Type d to delete, y to yank or copy, c to change, < and > to indent and dedent, and :sort to sort. Press the V key to enter character-wise visual mode, Shift+V for line-wise visual mode and Ctrl+V for block-wise visual mode.

Additional modes, known as sub-modes, include the following:

  • Replace. This mode replaces existing text with each new typed character. Press Shift+R to enter Replace mode.
  • Select. This mode deletes selected text and then switches to Insert mode to enable new text to fill the area. Press Ctrl+H to enter Select mode.

How to start using Vim

The following is a tutorial on how to use Vim by creating a simple script. Install Vim on your system and follow the examples to begin using this editor.

1. Open a file for editing

Begin by creating a file. Type the vim command using the filename as the argument. If the file exists in the current directory, Vim opens it. If the file does not exist in this directory, Vim creates it.

$ vim simplescript.sh

Vim opens in Command mode, so pressing a key will issue a command to Vim. Press the i key to switch to Insert mode. Notice the INSERT marker in the lower left corner. This enables you to enter text into the file.

Screenshot of Vim in Insert mode with INSERT banner in the lower left corner.

2. Edit a Vim file

You're now ready to write a script using Vim. Begin by placing a sh-bang on the first line, press Enter and write a comment introducing your script. Here's an example:

#!/bin/bash

#This is a sample script

Here is the body of the script, including a comment:

"Greet the user and provide today's date

echo "Hello $USER"

echo "Today's date is $(date)"

Code screenshot of writing the file.

3. Save and close a file

Now that the script has content, it's time to save changes and exit Vim. The first step is to press the Esc key to exit Insert mode and enter Command mode.

Next, insert a command line inside Vim to issue the save command. Focus on the lower left corner of Vim and press the : key. A command prompt will appear. Press W, which means write, to save your changes. Use this command frequently to save changes to your file.

Use the command :q to exit Vim and return to the shell environment.

You can also combine the two commands to make :wq to write and then quit. Type :q! to quit without saving changes.

In Linux or macOS, type the ls command to list directory contents. In Windows, type dir to see the same information. Your script should be visible.

This example covered basic Vim functionalities. Now let's look at some additional features.

4. Finish the script

You can test a script by setting the executable permission and running it from the local directory. In Linux or macOS, type the following two commands:

$ chmod 755 myscript.sh

$ ./myscript.sh

The chmod command sets permissions. The ./ before the script name tells Linux to execute the file from the current directory rather than using the path.

Advanced uses of Vim

Vim power users have extensive .vimrc customizations and many plugins to turn Vim into the tool they need for editing and programming. Let's evaluate two settings to get you started.

You can customize Vim's configuration by using the ~.vimrc file. Filenames beginning with a dot are hidden. Use the command ls -a in Linux or macOS to display them.

To display line numbers for all files, create or open the .vimrc file and enter the following script on the first two lines:

"The following line displays line numbers in all Vim documents

set number

Type this into .vimrc to disable error bells:

"Remove the error chime

set noerrorbells

Vim's .vimrc file uses " for comment fields, but many other files rely on # for comments.

Split command

Vim enables files to be opened simultaneously by splitting the screen into multiple windows. This feature is useful for comparing files and referencing documentation while coding without leaving your editor.

Press Ctrl+W+S to split the window horizontally.

:split or :sp

Split the screen horizontally and open a specified file -- replace filename with the name of the desired file -- in a new window by typing the command:

:split filename or :sp filename

Press Ctrl+W+V to split the window vertically.

Split the screen vertically and open a specified file in a new window by typing the command:

:vsplit filename or :vs filename

Spelling counts with Vim

Vim has a spell-checker. Once you've created some content, enter Command mode and type the following:

:set spell

Vim displays misspelled words in red. Turn off the spell-checker by typing the following:

:set nospell

Enable spell-checker in the .vimrc file so Vim continuously checks spelling.

Syntax check with Vim

Use the .vimrc file to cause Vim to highlight words based on language syntax. For example, suppose you're writing a Bash script or a Python program. Syntax checking highlights words, such as keywords or variables, to help you differentiate between them. It also identifies potential typos or syntax errors, which makes debugging easier.

If your Vim implementation does not already include syntax checking, add the following to your .vimrc file:

"Enable syntax checking for common languages

syntax on

To disable syntax checking, type :syntax off.

Sample .vimrc file with suggested modifications.

Vimtutor

Vimtutor is a hands-on tutorial of Vim that shows off its features and capabilities. Vimtutor is best for beginners or those looking for a refresher of basic commands.

Type vimtutor to begin the tutorial. It takes about 30 minutes to complete.

Additional Vim tutorials are available online in multiple formats, including interactive tutorials, books, videos and community support forums.

Why use Vim?

Text editors are critical to Linux system administration. System administrators modify configuration files, such as the SSH server file sshd_config, using a text editor. In addition, administrators frequently create scripts to maintain or back up the system or to manage automation and orchestration files, such as Ansible YAML files.

While it's less common to configure macOS or Windows using files, users of these platforms still work with text files regularly.

Vim is not the tool to use when creating large documentation files. However, Vim is helpful for quick editing, initial ideation and constructing scripts or automation files. Be sure to investigate the plugins available to help you customize your Vim.

Additional editing commands in Vim

Below are examples of additional editing commands in Vim. Most functions have multiple keyboard shortcuts. This list explains basic commands.

Insert

  • i: Insert text before the cursor.
  • I: Insert text at the beginning of the current line.
  • a: Append text after the cursor.
  • A: Append text at the end of the current line.
  • o: Open a new line below the current line.
  • O: Open a new line above the current line.

Delete

  • x: Delete the character under the cursor.
  • dd: Delete the current line.
  • ndd: Delete multiple lines, including the current line. Replace n with the number of lines you want to delete.

Copy (yank)

  • yy: Copy the selected line.
  • yw: Copy text from the cursor to the end of the current word.
  • y$: Copy text from the cursor to the end of the current line.
  • nyy: Copy multiple lines, including the current line. Replace n with the number of lines you want to copy.
  • "aY: Copy the current line into register a. Any lowercase letter from a to z can be placed before Y.

Paste

  • p: Paste the contents of the unnamed register after the cursor.
  • P: Paste the contents of the unnamed register before the cursor.
  • "ap: Paste the contents of register a after the cursor. Any lowercase letter from a to z can be placed before p.
  • "aP: Paste the contents of register a before the cursor. Any lowercase letter from a to z can be placed before P.

The following is a concise list of the daily benefits administrators reap from using Vim effectively:

  • Uncluttered. Vim contains little automation.
  • Speed. Vim functions using a keyboard. Menu scrolling is not an option.
  • Easy. Necessary functions are open, edit, save and close. Anything else you learn will make you more efficient.
  • Efficiency. Vim consumes few system resources, making it ideal for IoT devices, network appliances and other small-scale systems.
  • No background formatting. Copying text from one file to another will not have hidden formatting, unlike Word processors.
  • Extensibility. Vim is highly customizable with configuration files and plugins.
  • Language support. Vim recognizes hundreds of programming and markup languages, including Python, Markdown, YAML and HTML.

As of September 2025, Vim is on version 9.1.1752. Check the official site for the most current version of Vim and documentation.

Editor's note: The site editor updated this article in September 2025 to reflect changes in Vim version history and to update Vim commands.

Damon Garn owns Cogspinner Coaction and provides freelance IT writing and editing services. He has written multiple CompTIA study guides, including the Linux+, Cloud Essentials+ and Server+ guides, and contributes extensively to TechTarget Editorial, The New Stack and CompTIA Blogs.Kelly Richardson is the site editor for TechTarget's data center site.

Dig Deeper on Data center ops, monitoring and management