
Vim, also known as Vi IMproved, is an open-source, cross-platform text editor that extends the original Vi editor with advanced features. It supports modal editing, enabling efficient text navigation and modification using commands. Vim is commonly used to edit configuration files, write code, and manage scripts in Linux while offering multi-level undo, syntax highlighting, command-line editing, and visual selection features.
This article explains how to use the Vim text editor in Linux to create, manage, and modify files on your Linux workstation.
Before you begin, you need to:
Explore the available Vim modes and specific functions to efficiently use the text editor. Each mode enables the execution of specific tasks, such as navigation, text editing, selection, and command execution. The available modes in Vim include the following.
Normal Mode:
Vim starts in Normal mode, where you navigate, delete, copy, and issue commands. Press Esc to return to the normal mode from any other mode.
The following commands in the normal mode:
h, j, k, l: Move the cursor left, down, up, and right.dd: Delete the current line.yy: Copy the current line.p: Paste a copied line.u: Undo the last change.Ctrl + r: Redo the last undone change.gg: Jump to the beginning of the file.G: Jump to the end of the file.Insert Mode:
Use Insert mode to enter and edit files in Vim. You can switch to insert mode from normal mode using any one of the following commands.
i: Insert text before the cursor.a: Append text after the cursor.o: Insert a new line below the cursor.O: Insert a new line above the cursor.Visual Mode:
Use Visual mode to select and manipulate text in Vim. Activate this mode from the normal mode using any of the following commands.
v: Start character-wise selection.V: Start line-wise selection.Ctrl + v: Start block-wise selection.Below are the available commands you can execute in visual mode:
h, j, k, l or the Arrow keys (Left, Right, Up, Down): Allow you to move the cursor and adjust the selection.d: Delete the selected text.y: Copy the selected text.x: Cut the selected text.p: Paste text over the selected region.>: Indent the selection.<: Unindent the selection.Command-line Mode:
Use command-line mode for administrative tasks such as saving, quitting, and configuring Vim. Enter : to enter command-line mode from normal mode.
You can execute the following commands in command-line mode:
w: Save the file.q: Quit Vim.wq: Save and quit.q!: Quit without saving.set number: Display line numbers.set nonumber: Hide line numbers./: Search for keywords in a file. For example, /word searches for word in the file.You can create files using Vim by specifying the filename while opening the text editor. Follow the step below to create files using the Vim text editor.
Create files in Vim using the following command syntax.
For example, run the following command to create a new vimExample file.
The above command creates an empty text editor you can use to add your content and save the file to create it.
Pressing Esc to quit insert mode, then enter :wq to save and close the Vim text editor.
You can use Vim to efficiently open and manage one or more files. Follow the steps below to open files using Vim and navigate between multiple open files.
Open a file using Vim by specifying the filename after the vim command. For example, run the following command to open existingfile.
The above command opens existingFile, allowing you to edit and modify the file.
If the file does not exist, Vim opens an empty buffer, allowing you to create the file.
Open multiple files using Vim by specifying the filenames. For example, run the following command to open file1 and file2 using Vim.
The above command opens file1 and file2 in separate buffers.
Switch between the open files in Vim using the following commands:
:n or :next: Moves to the next file.:prev or :previous: Returns to the previous file.Vim provides multiple ways to edit text, undo changes, and save files. You can add or modify text using Insert mode, revert edits with undo and redo commands, and save files. This section explains how to edit a file using Vim.
To add text to your file, you need to enter in Insert mode, where you can begin typing. This section walks you through the process.
Open the file for editing.
Press i to enter Insert mode and start typing new content.
Press Esc to return to Normal mode.
This section shows you how to quickly revert recent changes in your file using Vim. You can undo or redo modifications to easily correct mistakes and ensure the desired outcome.
Press u in Normal mode to undo the last change.
Press ctrl + r to redo the last undone change.
This section explains how to save your changes, including how to handle files that require elevated permissions.
Enter :w in command-line mode to save a file.
Run the following command to save the file with root privileges.
This command saves the file. To exit, press :q in normal mode followed by Enter.
Vim offers efficient ways to search and replace text within a file. This section explains how to quickly find specific text patterns and replace them when needed.
In this section, you will learn how to search for specific text patterns in a file. This allows you to quickly locate the information you need.
To search for a specific text pattern, enter the below command in Command-line mode, replacing pattern with the text you want to find.
To highlight all occurrences of a pattern, enter the below command in Command-line mode.
To remove highlighting, use the below command in Command-line mode.
In this section, you will learn how to replace specific text within a file. This feature helps you to efficiently make necessary modifications.
To replace specific text with another, type the below command in Command-line mode.
In the above command, replace old with the text you want to find and new with the replacement, and use the g flag to replace all occurrences in the file.
To replace text only in a specific range of lines, specify the line numbers before the s command.
This above command will replace old with new from lines 6 to 8.
To display line numbers, use the command.
To confirm before replacing each occurrence, use the c option.
This will prompt you to confirm each replacement (y to replace each, a to replace all, n to skip).
In this section, you will learn how to efficiently cut, copy, and paste text within Vim. These actions allow you to manipulate your text quickly and precisely.
To cut text in Vim, follow these steps:
v.←, →, ↑, ↓) or h, j, k, l. Press d to cut the selected text.dd in Normal mode.d followed by a movement command nj (where n is the number of lines). For example, 3j will cut the cursor's line and the next two lines.Follow the steps below to copy text in Vim.
v.h, j, k, l. Press y to copy the selected text.yy in Normal mode.y followed by a movement command nj. For example, 3j copies the cursor's line and the next two lines.To paste text:
p to paste the copied or cut text after the cursor, or P to paste it before the cursor.N ( where N is the number of times you want to paste ) followed by p in Normal mode.Vim provides several ways to delete lines efficiently. By default, deleted lines are stored in a register, allowing you to paste them later. However, to discard lines permanently, use the black hole register ("_), which prevents deleted content from being saved.
dd to delete it."_dd.ndd (replace n with the number of lines) to delete multiple lines from the cursor’s position. For example, 3dd deletes the current line and the next two lines."_ndd. For example, "_3dd.dG to delete all lines from the cursor's position to the end of the file."_dG.dgg to delete all lines from the cursor's position to the beginning of the file."_dgg.Vim allows customization through configuration settings, enabling you to modify its behavior and appearance. Use the .vimrc file to define settings, key mappings, and plugins.
.vimrc FileThe .vimrc file stores user-defined settings for Vim.
Open the .virmc file.
Add your desired settings to customize Vim. For example:
Enable line numbers.
This ensures line numbers are displayed for all files when you open them using Vim.
Enable syntax highlighting.
This highlights code elements in various colors based on their structure, improving readability.
Show matching brackets.
This highlights matching parentheses, brackets, or braces to aid in code navigation and debugging.
Highlight cursor line horizontally.
The cursor's line is highlighted, which helps to identify the active line.
Vim supports color schemes to change its appearance, providing a more personalized and visually appealing editing environment. Follow the steps below to customize colors using profiles in Vim.
Check the active color scheme in Command-line mode.
This command displays the current color scheme.
To see a list of available color schemes available, enter the :colorscheme command, followed by a space, then press Ctrl+D to display a list of available themes similar to the one below.
Change the color scheme to a profile such as habamax.
Vim resets color schemes when restarted. To keep changes, add the configuration to your .vimrc file.
Follow the steps below to define custom key mappings using the .vimrc file.
Open the .virmc file.
Map qq to exit Vim in Insert mode.
Now pressing qq in Insert mode will exit to normal mode.
Map Ctrl + S to save a file in Vim.
Press the Ctrl + S key combination in mormal mode or Insert mode to save a file.
Map q to close a file.
Pressing q in normal mode will close the active file.
Map Ctrl + V to toggle paste mode.
Pressing Ctrl + V in Normal mode or Insert mode will toggle paste mode, which is useful when pasting code from your system clipboard into Vim text editor.
Plugins modify the Vim text editor and extend its functionality. Follow the steps below to use the vim-plug plugin manager to install and manage plugins.
Install the latest vim-plug plugin manager.
Open the .virmc file.
Add the following contents to the file to define the plugins.
Save and close the file.
The above configuration installs the vim-commentary and vim-airline plugins in Vim.
Open Vim and run the following command in Command-line mode to install the specified plugins
vim-plugEnter normal mode in your Vim text editor.
Update all Installed Plugins.
Remove all unused plugins.
List all installed plugins.
Visit the Vim Awesome page to explore additional plugins you can install in your Vim text editor.
Exiting Vim can be done in several ways, depending on whether you want to save your changes or not. Follow the steps below to exit the Vim text editor.
Enter :wq in command-line mode to save a file and exit Vim.
Exit Vim and discard any changes to the file.
Exit Vim.
Exit all open files.
The above command exits Vim, closing all open files.
Exit Vim without saving changes across multiple open files.
You have used the Vim text editor in Linux to create, open, manage, and modify files. You explored the Vim syntax highlighting, configured color scheme features, and modified key mappings to enable efficient navigation within the text editor. For more configuration options, run the man vim command to view the Vim manual and usage page.
0 Comments
Be the first to comment and share your perspective with the community.