Skip to main content

VIM EDITOR: A Powerful and Versatile Text Editor for Linux

 

VIM is one of the most popular and widely used text editors among Linux users. It stands for Vi IMproved, as it is an enhanced version of the original Vi editor, which was created by Bill Joy in 1976. VIM is free, open-source, cross-platform, and highly customizable. It can be used for editing any kind of text, from code to configuration files, from plain text to markup languages.

In this blog post, we will cover the following topics:

  • How to install and launch VIM on Linux
  • How to use different modes and commands in VIM
  • How to edit, save, and quit files in VIM
  • How to search, replace, and navigate text in VIM


How to Install and Launch VIM on Linux

VIM comes pre-installed on most Linux distributions, but if you don’t have it on your system, you can install it using the package manager of your choice. For example, on Debian-based systems, you can use the following command:

sudo apt update
sudo apt install vim

To verify that VIM is installed correctly, you can run the following command:

vim --version
This will show you the version and features of VIM on your system.

To launch VIM, you can simply type vim in the terminal, followed by an optional file name. For example, to create and edit a new file called test.txt, you can use the following command:

vim test.txt

This will open VIM with a blank screen and a message at the bottom:

"test.txt" [New File]

How to Use Different Modes and Commands in VIM

One of the most distinctive and important features of VIM is its modal nature. This means that VIM has different modes of operation, each with its own purpose and set of commands. The main modes in VIM are:

  • Normal mode: This is the default mode when you launch VIM. In this mode, you can use various keyboard commands to manipulate and navigate the text, such as deleting, copying, pasting, moving, etc. You can also switch to other modes from this mode.
  • Insert mode: This mode is used to insert and edit text in the file. You can enter this mode by pressing i in normal mode. You can exit this mode by pressing Esc to return to normal mode.
  • Command-line mode: This mode is used to enter and execute commands that affect VIM or the file, such as saving, quitting, searching, replacing, etc. You can enter this mode by pressing : in normal mode. You can exit this mode by pressing Enter to execute the command or Esc to cancel it.
  • Visual mode: This mode is used to select and highlight text in the file, which can then be operated on by other commands. You can enter this mode by pressing v in normal mode. You can exit this mode by pressing Esc to return to normal mode.


There are also other modes in VIM, such as replace mode, select mode, and operator-pending mode, but they are less frequently used and beyond the scope of this post.

To use VIM effectively, you need to master the different modes and commands in each mode. The following sections will introduce some of the most common and useful commands in VIM.


How to Edit, Save, and Quit Files in VIM

To edit text in VIM, you need to switch to insert mode by pressing i in normal mode. This will change the message at the bottom to:

-- INSERT --

Now you can type and edit text as you would in any other text editor. You can use the arrow keys, the Home and End keys, and the Backspace and Delete keys to move and modify the text.

To exit insert mode and return to normal mode, you need to press Esc. This will change the message at the bottom to:

"test.txt" 4L, 27C

This shows you the file name, the number of lines, and the number of characters in the file.

To save the file, you need to switch to command-line mode by pressing : in normal mode. This will show a colon at the bottom left corner, where you can type the command. To save the file, you can use the w command, which stands for write. For example:

:w

This will save the file and show a message like:

"test.txt" 4L, 27C written

You can also save the file with a different name by specifying the name after the w command. For example:

:w new.txt

This will save the file as new.txt and show a message like:


"new.txt" 4L, 27C written

To quit VIM, you can use the q command, which stands for quit. For example:

:q

This will quit VIM and return to the terminal. However, if you have unsaved changes in the file, VIM will not let you quit and show a message like:

E37: No write since last change (add ! to override)

This means that you need to either save the file before quitting or force quit without saving. To save and quit in one command, you can use the wq command, which stands for write and quit. For example:


:wq

This will save the file and quit VIM. To force quit without saving, you can use the q! command, which stands for quit with a bang. For example:

:q!

This will quit VIM without saving the file and discard any changes.

How to Search, Replace, and Navigate Text in VIM

To search for a text pattern in VIM, you can use the / command in normal mode. This will show a slash at the bottom left corner, where you can type the pattern. For example, to search for the word “text”, you can use the following command:

/text

This will highlight the first occurrence of the word “text” in the file and show a message like:

/text
text.txt" 4L, 27C

To find the next occurrence of the word, you can press n in normal mode. To find the previous occurrence, you can press N in normal mode. To clear the highlight, you can press :noh in command-line mode.

To replace a text pattern with another text in VIM, you can use the :%s command in command-line mode. This command stands for substitute and takes the following syntax:

:%s/old/new/g

This will replace all occurrences of the old text with the new text in the file. For example, to replace the word “text” with the word “file”, you can use the following command:

:%s/text/file/g

This will show a message like:

4 substitutions on 4 lines

You can also use various options and modifiers with the :%s command, such as case sensitivity, confirmation, and regular expressions. For more information, you can use the :help :s command in command-line mode.

To navigate text in VIM, you can use various keyboard commands in normal mode. Some of the most common and useful commands are:


  • h: move the cursor left
  • j: move the cursor down
  • k: move the cursor up
  • l: move the cursor right
  • w: move the cursor to the next word
  • b: move the cursor to the previous word
  • e: move the cursor to the end of the word
  • 0: move the cursor to the beginning of the line
  • $: move the cursor to the end of the line
  • G: move the cursor to the end of the file
  • gg: move the cursor to the beginning of the file
  • Ctrl-f: move the cursor one page forward
  • Ctrl-b: move the cursor one page backward
  • Ctrl-d: move the cursor half a page down
  • Ctrl-u: move the cursor half a page up
  • :n: move the cursor to the line number n

You can also use a number before any command to repeat it that many times. For example, to move the cursor 10 words forward, you can use the following command:


10w

VIM Cheatsheet :

https://devhints.io/vim





















Comments

Popular posts from this blog

Cockpit vs. Webmin: A Detailed Comparison for Linux Administration

  Introduction In the realm of Linux system administration, having efficient tools for managing and monitoring servers is crucial. Two popular tools that system administrators often use are  Cockpit  and  Webmin . Both of these tools provide a graphical interface accessible via a web browser, simplifying the management of Linux systems. This blog post will explore what Cockpit and Webmin are, their purposes, a comparison table, and additional information to help you choose the right tool for your needs. What is Cockpit? Overview Cockpit is a web-based graphical interface for managing Linux systems. It is designed to be easy to use, enabling both experienced and novice administrators to manage their systems effectively. Cockpit integrates seamlessly with the system’s existing infrastructure, providing real-time monitoring and management capabilities. Purpose Cockpit is primarily used for: Monitoring system performance and resource usage Managing system services Handli...

How to Set Up Custom Screen Resolution on Fedora 38 Permanently

  If you are using Fedora 38 as your operating system, you may have encountered some issues with the screen resolution. The default resolution may not be suitable for your monitor or your preferences, and you may want to change it to a higher or lower value. However, changing the resolution from the Settings menu may not work properly, or it may not persist after a reboot. In this blog post, I will show you how to set up a custom screen resolution on Fedora 38 permanently using some simple commands and configuration files. The first step is to disable the Wayland display server, which is the default display server for Fedora 38. Wayland is a modern and secure display server, but it may not support some custom resolutions or drivers. To disable Wayland, you need to edit the /etc/gdm/custom.conf file as root. You can use any text editor of your choice, such as nano, vim, or gedit. To open the file with nano, for example, you can type the following command in the terminal: sudo nano ...

Key Concepts and Tools for a Linux System Administrator

  A Linux System Administrator needs to have a comprehensive understanding of various concepts and tools to manage, configure, and maintain Linux systems effectively. Below is a categorized list of essential skills and tools with brief descriptions. Category Key Concepts & Tools Description Operating System Linux Distributions (e.g., Fedora, Ubuntu, CentOS) Knowledge of different Linux distributions, their package management systems, and unique features. Kernel Configuration and Management Understanding how to configure and optimize the Linux kernel for different workloads. System Boot Process (GRUB, systemd) Familiarity with the boot process, bootloaders, and system initialization processes. Command Line Skills Bash Shell Scripting Ability to write and debug shell scripts for automation of tasks. Core Commands (ls, cp, mv, rm, find, grep, awk, sed) Proficiency in using basic and advanced command-line utilities for system management. File System File System Hierarchy Standard (...