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 was once the default editor with Linux. Now, with other text editors, like Nano, available to install, it may be difficult to decide what editor is best for you.
The following tutorial and tips to using Vim with Linux will provide a better understanding on how to efficiently use the tool.
Why use Vim?
Text editors are a critical part of 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.
There is a difference between a text editor and a word processor. Text editors just enter text into files and are best for straightforward tasks and creativity. Word processors are feature-rich with dynamic menus and autocorrect.
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 code:
$ sudo apt-get install vim
To add Vim using Brew, type the following:
$ sudo 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. There are no drop-down menus 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.
There are four Vim modes:
- Command. The keyboard issues commands to Vim. This is sometimes referred to as Normal mode.
- Insert. The keyboard enters text into the file.
- Execute. Type the colon (:) and then enter commands at a prompt. For example, :wq saves the file and quits Vim.
- Visual. A mode that provides additional options for selecting text.
Tutorial on how to start using Vim
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.
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 allows you to enter text into the file.
Edit a 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)"
Save and close a file
Now that there is content on the script, 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! if you want 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.
Finish the script
You can test a script if you set the executable permission and run 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.
More advanced use
Vim power users have extensive .vimrc customizations and many plugins to turn Vim into the tool they need for editing and programming. Here are 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 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.
Spelling counts
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 line:
:set nospell
Enable spell-checker in the .vimrc file so Vim continuously checks spelling.
Syntax check
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.
Vim tutor
Consider using the built-in Vim tutorial to make the most of this tool. The hands-on explanation of Vim shows off its features and capabilities. Type vimtutor to begin the tutorial. It takes about 30 minutes to complete.
Daily benefit
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.
The following is a concise list of the daily benefits administrators reap from using Vim effectively:
- Uncluttered. Vim contains little automation.
- Speed. Vim functions by the use of a keyboard. Menu scrolling is not an option.
- Easy. Necessary functions are open, edit, save and close. Anything else you learn will simply 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 languages and markup languages, including Python, Markdown, YAML and HTML.
As of 2022, Vim is on version 9.0. Check the official site for the most current version of Vim and documentation.