Home Downloads Quick reference: Basic Vim commands for new users

Quick reference: Basic Vim commands for new users

Vim is a powerful text editor used in Linux. This guide covers basic Vim commands that are essential for every user, from navigating and editing text to saving and exiting files. Learn these foundational commands to improve your efficiency and proficiency in Vim.

by Divya Kiran Kumar
basic vim commands

Vim, an acronym for Vi IMproved, stands out in the world of text editors for its efficiency, flexibility, and enduring popularity, especially among programmers and system administrators. In the present day, its relevance remains unchallenged, offering a powerful set of features that facilitate quick and efficient text editing.

This blog post aims to demystify Vim for beginners and enhance the toolkit of more experienced users. From basic commands, understanding different modes, to FAQs, we’ve covered essential aspects to help you navigate and utilize Vim effectively, regardless of your experience level.

Getting started with Vim

First things first, how do you get into Vim? Simple! Just open your terminal and type vim. This command launches Vim, and you’re greeted with its default screen. It might look daunting at first, but don’t worry, we’re here to demystify it.

Entering and exiting Vim

Input:

vim

Output:

VIM - Vi IMproved
version 8.2
...
~
~
~
~

To exit Vim (one of the infamous jokes in the programming world), simply press Esc to ensure you’re in normal mode and type :q!. This command forces Vim to quit without saving any changes. Speaking of normal mode, let me explain a bit about vim modes.

Understanding Vim modes

One of Vim’s unique features is its mode-based editing, which can be puzzling for beginners but offers great efficiency once you get the hang of it. Understanding these modes is crucial for effectively using Vim. Let’s break down the primary modes you’ll encounter:

Normal mode

When you first open Vim, you’re in Normal mode. This mode is all about navigating and manipulating text. It’s not for inserting text – think of it as your command center. Here, you can move around the file, delete text, copy and paste, and execute other commands.

Insert mode

As the name suggests, Insert mode is for inserting text. You can enter this mode by pressing i while in Normal mode. Once in Insert mode, Vim functions like a regular text editor. To return to Normal mode, just press Esc.

Switching to Insert mode:

Input:

i

Output: Start typing your text right away.

Visual mode

Visual mode is for selecting text. You can enter it by pressing v in Normal mode. Once you’ve highlighted the text, you can perform operations like cutting, copying, or formatting over this selection. Press Esc to exit Visual mode.

Using Visual mode:

Input:

v

Output: Move the cursor to highlight text.

Command-line mode

You enter Command-line mode by pressing : in Normal mode. This mode is used for executing Vim commands, like saving files, searching, or quitting Vim. You’ll see the cursor move to the bottom of the screen where you can type your command.

Saving a file in Command-line mode:

Input:

:w

Replace mode

To replace existing text, you use Replace mode. Press R in Normal mode to enter this mode. Each character you type will replace the existing character under the cursor. It’s like overtyping in traditional text editors.

Replacing text:

Input:

R

Output: Start typing to replace the existing text.

Basic navigation

Navigating through Vim can be achieved with a few simple keystrokes. Remember, Vim is modal, which means you need to switch between different modes for different actions.

  • h moves the cursor left.
  • j moves the cursor down.
  • k moves the cursor up.
  • l moves the cursor right.

Editing text

To start editing text in Vim, you need to enter the insert mode. Press i while in normal mode.

Input:

i

Now, you can start typing like in any regular text editor. To stop editing and go back to normal mode, press Esc.

Saving changes

To save the changes you’ve made, you need to write the file. Press Esc to ensure you’re in normal mode and type :w. This command saves the file but keeps you in Vim.

Cutting, copying, and pasting

These are crucial commands in any editor, and Vim is no exception.

  • To cut (or delete) a line, press dd in normal mode.
  • To copy a line, press yy.
  • To paste the line, press p.

Searching text

Searching in Vim is straightforward. Press /, type your search query, and hit Enter. To navigate through search results, use n for the next occurrence and N for the previous one.

Customizing Vim

Vim is highly customizable, and that’s where its true power lies. You can start by editing the .vimrc file in your home directory. This file allows you to set various options to personalize your Vim experience.

Input:

vim ~/.vimrc

Here, you can set your preferences, like line numbers (set number), syntax highlighting (syntax on), and more.

10 basic Vim commands with practical examples

Let’s delve deeper into Vim’s capabilities with 10 more commands. These are particularly useful for everyday tasks and can significantly enhance your efficiency while working in Vim.

1. Opening a file

Command:

:e filename.txt

Description: This command opens filename.txt in Vim. If the file doesn’t exist, Vim starts with a blank file named filename.txt.

2. Moving to the beginning of the file

Command:

gg

Description: In Normal mode, gg takes you straight to the beginning of the file. It’s an instant scroll to the top.

3. Moving to the end of the file

Command:

G

Description: Similarly, G takes you to the end of the file. It’s the quickest way to reach the bottom.

4. Jumping to a specific line number

Command:

:25

Description: This command takes you directly to line 25. Replace 25 with any line number you need to jump to.

5. Deleting a single character

Command:

x

Description: In Normal mode, position the cursor over a character and press x to delete it.

6. Undoing changes

Command:

u

Description: Made a mistake? Simply press u in Normal mode to undo the last action. It’s a lifesaver.

7. Redoing changes

Command:

Ctrl + r

Description: If you undo too much, Ctrl + r will redo your last undo.

8. Splitting the window horizontally

Command:

:sp

Description: This command splits your Vim window horizontally. You can open another file alongside by typing :sp filename.

9. Splitting the window vertically

Command:

:vsp

Description: To split the window vertically, use :vsp. It’s great for comparing files side by side.

10. Closing the current window

Command:

:q

Description: If you have multiple windows open (due to splitting), :q will close the current window.

Real-world example:

Suppose you are working on a Python project and need to reference another file frequently. You can open the main file (main.py) and then split the window to open the reference file (utils.py).

Input:

:e main.py
:sp utils.py

Output: You will see main.py in the top half of the screen and utils.py in the bottom half.

These commands showcase Vim’s versatility and efficiency. Whether it’s navigating large files, editing text, or managing multiple files simultaneously, Vim offers a command for almost every need.

Basic vim commands cheat sheet

This table provides a concise overview of some of the most commonly used Vim commands, suitable for both beginners and experienced users looking to refresh their memory. I hope it’s useful to you.

Command Description
:e filename Open filename for editing
gg Move to the start of the file
G Move to the end of the file
:number Jump to a specific line number
x Delete the character under the cursor
u Undo the last action
Ctrl + r Redo the last undone action
:sp Split window horizontally
:vsp Split window vertically
:q Quit the current window
i Enter insert mode
v Enter visual mode
:w Save the file
dd Delete (cut) the current line
yy Yank (copy) the current line
p Paste the yanked or deleted text
/text Search for text
n Go to the next search result
N Go to the previous search result
:%s/old/new/ Replace all occurrences of old with new

Frequently Asked Questions about Vim

1. What is Vim?

Vim is a highly configurable text editor built to enable efficient text editing. It’s an improved version of the vi editor distributed with most UNIX systems. Vim is often used by programmers and system administrators for its powerful editing capabilities.

2. Why is Vim so popular among programmers?

Vim is popular because of its efficiency, portability, and powerful features. It supports numerous programming languages and file formats, has extensive customization options, and can be operated entirely with the keyboard, which speeds up editing tasks.

3. Is Vim difficult to learn?

Vim has a steep learning curve initially due to its unique modes and keyboard-centric operation. However, once these fundamentals are mastered, many users find Vim to be an efficient and powerful editor.

4. Can Vim be used for programming?

Absolutely! Vim is great for programming due to its support for syntax highlighting, code formatting, and integration with various programming tools. Many plugins are available to extend its functionality further.

5. How do I save and exit in Vim?

To save changes to your file, type :w and press Enter. To exit, type :q and press Enter. To save and exit simultaneously, type :wq or ZZ.

6. How can I cancel a command in Vim?

To cancel a partially typed command in normal mode, just press Esc. This will return you to normal mode without executing the command.

7. Are there any graphical versions of Vim?

Yes, gVim is the graphical version of Vim. It offers menus and toolbars for commonly used commands, making it more accessible, especially for beginners.

8. How do I copy and paste in Vim?

To copy (yank) a line, press yy. To paste it, press p. This works in normal mode. For block selection, use visual mode by pressing v, then move to select and y to yank.

9. Can Vim be customized?

Vim is highly customizable. You can edit the .vimrc file in your home directory to change settings, key mappings, colors, and add plugins.

10. Where can I find more resources to learn Vim?

The Vim website (www.vim.org) is a good starting point. Additionally, running :help in Vim provides a comprehensive user manual.

Conclusion

We’ve went through the essential commands, delved into the distinct modes of Vim, and answered some common questions that arise when using this editor. I hope the information provided here serves as a springboard for greater efficiency and a deeper understanding of this powerful editor. The path to Vim mastery is continuous learning and practice, and what we’ve discussed today lays the foundation for a skill that will serve you well in your coding and text editing endeavors.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.