Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Vim Text Buffers in Linux
Next article icon

Vim Editor in Linux

Last Updated : 02 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

If you’re new to the world of Linux, one of the first things you’ll encounter is the command line interface. And if you’re going to be working in the command line, you’ll inevitably come across various text editors. Among them, Vim stands out as a powerful, yet notoriously complex, text editor. In this guide, we’ll take you through everything you need to know about Vim, from its basics to more advanced features, to help you get started on your journey with this essential tool.

What is Vim?

Vim, short for Vi IMproved, is a highly configurable text editor built to enable efficient text editing. It’s an enhanced version of the Vi editor, which was developed back in the 1970s. Vim is widely used by programmers, system administrators, and anyone who spends a significant amount of time working in the terminal

Vim Editor in Linux

  • Installation and Configure Vim in Our Linux System
  • Modes in Vim:
  • Basic Commands in Vim:
  • Navigating in Vim:
  • Deletion Commands in Vim:
  • Search in Vim Editor
  • Search and Replace in Vim Editor
  • Pratical Implementation of Command in Vim Editor

Installation and Configure Vim in Our Linux System

Most Linux distributions come with Vim pre-installed. However, if it’s not available on your system, you can install it using your package manager. For instance, on Ubuntu or Debian, you can use:

sudo apt-get install vim

To install vim on an arch-based distro run the following command:

sudo pacman -S vim

Now vim will be installed on your system. 

Modes in Vim:

One of the unique features of Vim is its modes. Vim has several modes, each serving a different purpose:

  1. Normal Mode: This is the default mode when you first open Vim. In this mode, you can navigate the file, delete text, copy text, and perform other commands.
  2. Insert Mode: This mode allows you to insert and edit text. To enter Insert Mode from Normal Mode, press `i`.
  3. Visual Mode: In this mode, you can visually select blocks of text. To enter Visual Mode from Normal Mode, press `v`.
  4. Command-Line Mode: This mode lets you enter Vim commands. To enter Command-Line Mode from Normal Mode, press `:`.

Basic Commands in Vim:

Here are some basic commands to get you started:

  • Saving a File: In Normal Mode, type `:w` and press Enter to save the file.
  • Exiting Vim: In Normal Mode, type `:q` and press Enter to quit Vim. If you have unsaved changes, you’ll need to use `:q!` to force quit.
  • Saving and Quitting: To save and quit in one command, type `:wq` and press Enter.

Navigating in Vim:

  • Moving the Cursor: Use the arrow keys or `h`, `j`, `k`, `l` keys to move left, down, up, and right respectively.
  • Jumping to the Beginning or End of a Line: Press `0` to jump to the beginning of a line and $ to jump to the end.
  • Jumping to a Specific Line: Type `:<line_number>` and press Enter to jump to a specific line.

Deletion Commands in Vim:

Always use the Esc key to go into normal mode and use the insertion, deletion keys, and other keys. 

To delete the word move the cursor to the beginning of the word and use dw command in normal mode. The word under the cursor will be deleted.

dw

To delete more than one word in a single line use the following command.

To delete 2 words use the command

d2w

To delete the line move cursor to the beginning of the line and use d$ command in normal mode. The line under the cursor will be deleted.

d$

Undo and Redo:

As we are programmers most time we are using undo and redo .vim to provide these to both features in it. To undo press u key in normal mode

u

To redo use the ctrl+r key in normal mode in vim

ctrl+r

Search in Vim Editor

To search the word After the cursor uses the backslash key and then write the word and press enter.

:/word

Use n to move on next matching word

:n

Use N to move on previous matching word

:N

Search and Replace in Vim Editor

To replace the word in file use s/ command in vim like

:s/searchword/replaceword/

To do replace all occurrence of word use g 

:s/searchword/replaceword/g

This command will replace the word globally.

To confirm before replacements of words use gc.

:s/searchword/replaceword/gc

To use this command in the whole file use % before the command.

:%s/searchword/replaceword/gc

Pratical Implementation of Command in Vim Editor

You can open vim by running vim command on the terminal.

vim 

vim window

There are some commands given to use the vim editor. You can see all commands, and it’s documentation by help command as follows:

:help

Now to exit type this command 

:q!

vim help

Now, Let’s Start to use Vim

To open a file in vim editor just write the file name after the vim command in the terminal as follows:

vim filename.txt

Then the file will be opened.

Write into file 

In the previous step we have opened the file now, Let’s write some content in to write data we need to go in insert mode. To go into write mode type i.  As follows:

i

After going into insert mode you will see INSERT in the status bar. After that, we can write any data in it.

vim window

Save and Exit:

We have written the data into a file now the task is to save and exit the file to do that first exit from insert mode by pressing the Esc key. To write a command first type semicolon  (  :  )  and then type the command wq!  or x! (both do the same thing) And then hit ENTER.

:wq!

vim window for save and exit

Exit without saving the file:

To exit from the file without saving the file just use the command q! As follows

:q!

Vim also comes with its own tutorial. You can see this tutorial by command vimtutor into the terminal .

vimtutor

vim window for save and exit

Now vim tutorial will be opened as follows:

vim window for save and exit

Text Editing: Insertion

We have edited some text files before by using the i key. There are Four keys used for the insertion of text. Just type the key into the normal mode in vim.

 i    ->   This key is used to put the cursor before the current position.
a -> This key is used to put the cursor after the current position.
o -> This key is used to put the cursor below the line.
O -> This key is used to puts the cursor above the line.

Motion: Motions provide context to your Operators. These execute the action in a particular way.

Here is a list of some motions

w - until the start of the next word, EXCLUDING its first character.
e - to the end of the current word, INCLUDING the last character.
$ - to the end of the line, INCLUDING the last character.

We can use motion with the d key and with many more keys

Count: Count is the number for which replete the motion for count number. Here is a demonstration of the use of count and motion

To move courser 2 words forward use the following command

2w

Here 2 is the number of counts and w is used for word

To move the cursor 4 lines forward use the following command

4$

Conclusion

In conclusion, navigating the Linux command line often leads to encountering various text editors, and among them, Vim shines as a robust yet intricate tool. This guide has provided a comprehensive overview of Vim, from its installation to basic commands, modes, and advanced features, aiming to equip beginners with the necessary knowledge to embark on their journey with this essential text editor. While Vim’s learning curve may seem steep, mastering its capabilities can significantly enhance productivity and efficiency for programmers, system administrators, and anyone spending time in the terminal. With practice and exploration, Vim opens up a world of powerful text editing possibilities, making it a valuable asset in the Linux environment.



Next Article
Vim Text Buffers in Linux
author
nishant0073
Improve
Article Tags :
  • Linux-Unix

Similar Reads

  • vi Editor in Linux
    The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor.  Table of C
    9 min read
  • Nano Text Editor in Linux
    In the vast world of Linux text editors, Nano stands out as a simple yet powerful tool for editing files directly from the command line interface. Whether you're a novice user or an experienced developer, Nano offers a straightforward and efficient editing experience. In this article, we'll delve in
    8 min read
  • How to undo in Vim editor in Linux
    Vim (short for Vi Improvised) is a popular text editor mainly used in Linux and Unix-based operating systems but due to having a CLI (command line interface), it might be difficult for new users to make use of basic features such as undo in Vim. So in this article, we will learn different ways and t
    3 min read
  • Linux Text Editors
    For Linux users, text editors are essential tools that play a crucial role in various tasks, from coding and writing to system administration and configuration management. Linux offers a wide range of text editors, catering to different preferences and use cases. In this article, we will delve into
    10 min read
  • Vim Text Buffers in Linux
    When it comes to text editing we all have heard of the VIM editor which is widely used in Linux systems. Vim is powerful and versatile which has made it the go-to choice for many linux users and programmers as well. Vim offers many features that set Vim apart and one of them is the text buffer syste
    3 min read
  • Edit Multiple Lines in Vi edtor
    In this article, we will cover how to edit multiple lines in vi editor. First, we look at what Linux and VI editors followed by editing multiple lines in the vi editor with examples and then we will look how to delete multiple lines in the vi editor. LinuxLinux is a kernel for operating system(OS) i
    4 min read
  • Macros in Vi editor
    Macros in Vi Editor are particularly handy for automating repetitive edits, such as commenting or uncommenting lines, transforming text, or making similar changes across multiple lines or files. They add a layer of efficiency to the text editing process and are a valuable tool for users who want to
    7 min read
  • Is vi editor free?
    Yes ! vi is a free and open-source text editor. Vi editor was developed in 1976 by Bill Joy, vi editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. An improved version of Vi editor was released later in 1991, popularly known as VI IMproved (VIM)
    5 min read
  • Joining Two Lines in VI Editor
    Vi is a powerful command-line text editor used by programmers, system administrators, and writers alike. They offer a vast array of features, including the ability to manipulate text efficiently. One common task you might encounter when working with Vi is joining two lines together. This article wil
    3 min read
  • How to Install Vim Editor in Ubuntu?
    Vim is a highly configurable and powerful text editor used by developers and system administrators on Ubuntu and other Linux distributions. Whether you’re coding, writing scripts, or editing configuration files, Vim offers efficiency with its vast features and keyboard shortcuts. In this guide, we'l
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences