Vim in Linux

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Vim is a text editor for the Unix-like operating system (Linux, BSD, macOS) with both the command line and graphical interface. It provides the most flexibility for text editing in Linux environments. Vim is designed to be fast, efficient, and extensible, making it a preferred choice for all developers and system administrators. In this article, I will provide a complete Vim guide so that new users can use Vim with ease.

What is Vim in Linux?

Vim is a popular text editor used in Unix-like operating systems (Linux, BSD), windows, and macOS for programming, writing, editing configuration files, and other tasks requiring text manipulation. Vim refers to vi improved, written by Bram Moolenaar, a Dutch computer programmer in the late 1980s. It is an enhanced version of vi (visual editor) which was written by Bill Joy back in 1976, providing additional features and supporting both text and graphical interfaces.

Vim is well-known for its efficiency and its wide range of customization options. It is a free and open-source text editor that is completely maintainable by the keyboard. Without using any menus or mouse, users can operate it seamlessly. Like, to insert text into a file, users only need to press i and type. Vim adds a new dimension for editing complex text and files.

Why Do We Use Vim in Linux?

In Linux, vim is a powerful text editor with numerous advantages, making it a valuable tool for developers. Here are a few reasons to learn Vim:

  • Installable Everywhere: Most Linux distros install Vim by default. However, it can also be installed virtually on all Unix-like systems. Regardless of which Linux distribution you are using, Vim is accessible everywhere.
  • Fast and Efficient: Vim’s keyboard-based interface allows you to edit text quickly and efficiently. Once you get the hang of Vim’s commands and keyboard shortcuts, you’ll be able to complete tasks in half the time it takes by using a mouse.
  • Flexible: You can customize Vim’s plugins, mapping, and other settings to create a custom editing environment, making it more flexible for users.
  • Scalable: Whether you are working on a small or large text, Vim will always be responsive and scalable, ensuring smooth performance.
  • Supports TUI and GUI: Vim supports both text-driven Terminal User Interface (TUI) and Graphical User Interface (GUI) that lets users work with their preferred interface.
  • Simple Commands: The commands and shortcuts used in Vim are easier to remember. For instance, i for inserting, d for deleting, q for quitting, etc.

Vim Modes

Vim has multiple modes of operation, each of which has a distinct function, such as editing, copying, inserting, and so forth. Here are 5 Vim operating modes:

  1. Normal Mode: This is the default mode where you can navigate through documents and perform various operations such as copying, pasting, deleting, and more using single key commands.
  2. Insert Mode: It allows users to modify and insert text by typing and removing characters, just like modern text editors do. There are 3 ways to enter the insert mode from the default mode:
    • Press i key to enter insert mode at the current cursor position.
    • Press I key to enter insert mode at the start of the current line.
    • Press a key to enter insert mode after the current cursor position.
    • Press A key to enter insert mode at the end of the current line.
    • Press o key to enter insert mode, inserting a new line below the current character position.
  1. Visual Mode: This mode lets you visually edit the text block. To switch to visual mode, hit the v key to indicate the beginning of the selection. Move your arrow keys to the end of the selection. Then, run the available normal mode commands to edit the text.
  2. Command-line Mode: It provides a more sophisticated way to run commands. To switch from the normal mode to the command line mode, press : which moves the cursor to the command box. After that, type any command and hit ENTER to execute it.
  3. Replace Mode: In replace mode, you can simply type over the existing text. To switch to replace mode from default mode, press R and continue typing.

How to Use Vim in Linux?

To learn to use Vim in Linux, follow the simple steps below:

Step 1: Open a Terminal Window

Open a terminal from your Linux operating system. Here, I am going to open my Ubuntu terminal by Pressing CTRL+ALT+T. Mac users can also follow these instructions as Vim is available in macOS as well.

Step 2: Create a File

After opening the terminal, write Vim followed by the filename to create a file “file1.txt”:

vim file1.txt

You can see that the “file1.txt” file is created in the Vim text editor:

creating a file in vim.

Note: If Vim is not installed on your system, install it using the following command:

For Deian-based distro users:

sudo apt install vim

For Red-hat-based distro users:

sudo yum install vim

For Mac users:

brew install vim

Step 3: Insert Text and Save File Modifications

To insert text into the file, hit i button. It will take you to the insert mode from the normal default mode. Now, type texts as per your need inside the file. Here, I am typing the following text in the “file1.txt” file:

Hello user, I am using Ubuntu.
Welcome to Linuxsimply.com

inserting text to a file in vim

Once you have done inserting text, press the Esc key to go back to the normal mode. After that, type : to enter command-line mode.

To save the “file1.txt”, write w after the colon :w and press ENTER:

saving file text in vim

As you can see, the contents of the “file1.txt” have been written (saved) on the disk.

You can also change the filename by pressing ENTER after writing :w newfile.txt. It will change the filename from “file1.txt” to “newfile.txt”.

Step 4: Navigate through File

Vim offers many amazing navigation features. If you want to go straight to the 5th line of the file content, you can achieve that using Vim’s commands without clicking traditional arrow keys several times. It is beneficial when the file size is huge. Let’s see practically how to navigate through a file in Vim:

To set the number before each line of the file contents, type :set number and press ENTER:

navigating through file in vim

Now to jump to line number 2, type :2 and press ENTER:

navigating through file in vim2

You can go to any line as per your requirements using :Line_number command.

If you want to go to the end of the line, write :$ and press ENTER:

going at the end of the file contents

Note: Before executing any command, hit Esc to return to normal mode.

Step 5: Editing Text in a File

To edit the text in a file, go to the line using navigation keys or shortcuts to perform the editing operations like delete, undo, highlight, copy, paste, and so on.

A. Delete File Line

Now, to delete line number 2 from the file, type :d and press ENTER:

deleting file contents in vimLine 2 “Welcome to Linuxsimply.com” has been deleted successfully.

B. Undo Operation

To undo the previous operation, just press the u button. Multiple operations can also be undone by pressing the u button multiple times. For example, to get back the deleted line, I will perform the following undo operation:

undo operation in vim

C. Highlight Text

Highlighting text allows you to focus on a specific text or character. It improves readability by highlighting different parts of a complex code. To highlight a line in Vim, hit v button to enter a visual mood from the normal mode. After that, select the text to highlight using the left and right arrow keys:

selecting text to highlight

Once the text has been chosen, you can run various commands to change the background color as well as the text color.

D. Copy and Paste Text

To copy a text in Vim, first select the text you want to copy. Then, press Y to copy the text to the clipboard. Afterward, move the cursor to specify the location for pasting. Now, hit the p button to paste the copied text:

copying and pasting text in vim

You can see that, line 1 “Hello user, I am using Ubuntu.” has been pasted after line 2.

Step 6: Searching Text in a File

To search for a specific pattern in a file using Vim, run /pattern and press ENTER. Here, I am going to search for the word “user” in “file1.txt” by typing /user. To move to the next match (in case there are multiple matches), press n, and hit shift+n to move to the previous match.

searching text in vim

After running the /user command, the “user” word has been highlighted.

Note: To search backward (from the current cursor position) for a pattern, you can type ?pattern instead of /pattern.

Step 7: Exit File

To exit the opened file after saving the changes in the file in Vim, enter :x or :wq. To quit without saving the file changes, enter :q!:

exiting file

Vim Commands

There are some useful Vim commands that every Linux user should know to experience smooth performance with Vim. These are:

Commands Tasks
i Go to insert mode starting before the current cursor position.
a Go to insert mode starting after the current cursor position.
o Go to insert mode after adding a blank line starting after the current cursor position.
v Go to visual mode.
r Go to replace mode.
Esc key Go to command mode.
:w Write file.
:q Quit file.
:q! Quit without saving.
:wq Save and quit file.
:set number or :set nu Set line number.
:set number! or :set nu! Remove line number.
:u Undo last edit.
CTRL+r Redo last edit.
/ Search forward in file.
? Search backward in file.
dd Delete line. 2dd for deleting two lines.
x Delete letter.
dw Delete word.
dG Delete everything until the end of file.
yy or Y Copy line. 2yy or 2Y for copying two lines.
p Paste text after the current cursor position.
P Paste text before the current cursor position.
:%s/word1/word2/g Search all the word1 and replace it with word2.
gUh Converts to uppercase next word h.
guh Converts to lowercase next word h.

Navigation Shortcuts

In addition to the arrows, there are also a few shortcut commands that you can use to navigate through the file. Let’s take a look at some of the navigation commands in Vim:

Commands Tasks
h Move the cursor to the left.
l Move the cursor to the right.
k Move the cursor up one line.
j Move the cursor to the down of one line.
e Move the cursor to the end of a word.
H Move the cursor to the start of the screen.
M Move the cursor to the middle of the screen.
L Move the cursor to the bottom of the screen.
:2 Go to line2.
:+3 Go 3 lines forward.
:-3 Go 3 lines backward.
G Go to the last line of file.
gg Go to the beginning of file.
w Move ahead one word. 2w to move ahead two words.
W Move ahead one whitespace-delimited word.
b Move back one word. 2b to move back two words.
B Move back one whitespace-delimited word.
} Move ahead one paragraph. 3} to move ahead three paragraphs.
{ Move back one paragraph. 3{ to move back three paragraphs.
]s Go to the next misspelled word.
[s Go to the previous misspelled word.
Y} Copy the following paragraph.
y3w Copy the next three words.

Splitscreen Editing Commands

Vim split-screen editing commands let you edit multiple files at the same time. You can divide the Vim window vertically or horizontally to work on different sections of the same file or on different files. Here are some split-screen editing commands:

Commands Tasks
:split Split Vim window horizontally.
:vsplit Split Vim window vertically.
CTRL+wk Move to the split above the current one.
CTRL+wj Move to the split below the current one.
CTRL+wh Move to the split to the left of the current one.
CTRL+wl Move to the split to the right of the current one.
CTRL+w+ Increase the height of the current split.
CTRL+w- Decrease the height of the current split.
CTRL+w> Increase the width of the current split.
CTRL+w< Decrease the width of the current split.
:q Close split.

Conclusion

To sum up, in this article, I have explained the different modes of Vim, how to use it, and some helpful commands of Vim. Read through this article to learn Vim effectively. I hope that this will help you understand the Vim editor better.

People Also Ask

What is the role of Vim?

The main role of Vim is to provide an efficient environment for manipulating text. It is available on Linux, Mac OS, Windows, and all platforms and provides a wide range of keyboard shortcuts and macros, as well as text-processing commands. Vim can be used for coding, writing, and editing configuration files.

Is Vim only for Linux?

No. Vim is not only for Linux. It can be used on multiple platforms such as Linux, macOS, windows, and all Unix-like operating systems.

What is the difference between Vi and Vim?

The main difference between Vim and vi is that Vim (vi improved) is the updated version of vi. Vim supports syntax highlighting, split windows, undo/redo multiple times, and a lot of customization through the configuration file, which vi does not.

What is the Vim command?

Vim commands are used to carry out various text-processing operations such as editing, copying, pasting, replacing, navigating, etc. For instance, i command for inserting text, Y for copying text, and p for pasting text.

Why Vim is used in Linux?

Vim is used in Linux as it has many additional features and is readily available in all Linux systems. The versatility of Vim allows users to perform editing tasks more efficiently and quickly compared to other traditional text editors.

What are the advantages and disadvantages of Vim in Linux?

The primary advantages of Vim are its flexibility, speed, and editing capabilities, as well as being cross-platform. However, learning Vim commands takes some time and effort. Also, it’s a keyboard-based editing tool, so it’s not an ideal option for mouse users.

Related Articles


<< Go Back to Linux Text Editors | Learn Linux Basics

Rate this post
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

Leave a Comment