Vim: Basic and intermediate commands (2024)

In the Linux world, most work is accomplished from a CLI. One of our main tasks is to manage file content. To accomplish this, we need a tool to edit files. One of the most common editors in the Linux world is the Vi editor, also known as a visual editor. Let's learn about some of the basic Vi operations before looking at my favorite commands.

What is the Vi editor?

A visual editor allows users to write and manipulate text in a file in a Unix-based operating system. An improved version of Vi is called the Vim editor. We can download Vim in Red Hat Enterprise Linux8 (and similar distributions) by using dnf install -y vim.

How do I use this editor?

Using this editor is quite simple. Type vi file-name, and the editor opens. One advantage of this editor is that we can manipulate text without using a mouse. We only need the keyboard. Let's start this great tool right away.

# vi filename

This command opens up a brand new file named filename. The file looks like this:

Here you see a ~, which means those lines are unused. At the bottom of the page, we see the filename and [New File]. To start writing content into the file, simply press i (insert). Now the file goes to edit mode.

Basic commands

I have written five basic commands that I use with vim:

To copy text, use yy and yw:

  • yy - Copies the current line.
  • yw - Copies the current word from the character the lowercase w cursor is on, until the end of the word.
  • p - Pastes the copied text.

To move to the start or end of the file:

  • :1 - Moves the cursor to the start of the page.
  • :$ - Moves the cursor to the end of the page.

To search for a particular string in the file:

  • / - For example, if you want to search for the string redhat in a given file, you type: /redhat or red* to take you to the matching string.

To set the line numbers and paste complex texts inside the file:

  • set num - Sets the line numbers.
  • set paste - Pastes a bulk of text from some other location.

To move forward and backward for one fullscreen:

  • Ctrl+f - moves forward by fullscreen.
  • Ctrl+b - moves backward by fullscreen.

Intermediate commands

I have written three advanced commands that I use with the Vim editor:

To undo and delete a particular line:

u - Undo the previously executed command.

dd - Deletes the current line of text.

To rename the file:

:f filename - Helps you to rename the file.

To toggle between two open files:

:e # - Helps you to navigate/toggle between two open files.

Conclusion

In this article, I covered the most common commands that I use in my day-to-day work with the Vim editor. By no means does this include all the available commands. Feel free to check the man page for other helpful commands as per your scenario.

[ Free online course: Red Hat Enterprise Linux technical overview. ]

Vim: Basic and intermediate commands (2024)

FAQs

Vim: Basic and intermediate commands? ›

vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.

What are the 2 modes of Vim? ›

vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.

What are the Vim commands? ›

The Basics Of Moving In Vim
  • h moves the cursor one character to the left.
  • j moves the cursor down one line.
  • k moves the cursor up one line.
  • l moves the cursor one character to the right.
  • 0 moves the cursor to the beginning of the line.
  • $ moves the cursor to the end of the line.
  • w move forward one word.
May 14, 2022

What is the difference between Q and QA in Vim? ›

Using the :q! command will force quit Vim and discard any changes, while the :wq and ZZ commands will save changes before exiting. Finally, the :qa! command will quit all open files without saving changes.

What is the D$ command in Vim? ›

[. inline-code]d$[. inline-code] deletes to the end of the line, starting at the cursor.

What is Vim easy mode? ›

vim has an easy mode. You enable it by using -y flag, like this vim -y . Is kinda cool, it makes it behave like a regular "click-and-type" text editor. In this mode regular shortcuts work as expected, ctrl + s for save, ctrl + c for copy, ctrl + v paste, ctrl + z to undo, ctrl + y redo.

What are the different map commands in Vim? ›

In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and ":imap" commands to enter mappings for each mode separately.

How to use Vim basic commands? ›

“h”, “j”, “k”, “l”, “w”, “b” — Navigation Through Text Files. These commands are used to move the cursor around in Vim. h moves the cursor left, j moves it down, k moves it up, and l moves it right. w moves the cursor to the beginning of the next word and b moves the cursor to the beginning of the current word.

Why is Vim so good? ›

Vim is the only editor that optimizes editing text instead of writing from a blank page. That's another reason Vim makes you so efficient: you have different modes for each phase of your current work or task. Normal mode is for reading code and navigating quickly.

What does G stand for in Vim? ›

:g is the global command. /./ is a pattern for "non-empty lines". It matches the lines with at least one character, so it matches the lines with "const" and "console" and it does not match empty lines. normal A; runs the :normal command-line command.

What does ZZ do in Vim? ›

In Normal Mode , you can also press ZZ or ZQ to quit Vim. Where ZZ is the same as :x and ZQ is the same as :q! . Here, ZZ and ZQ are considered commands of Normal Mode while :x and :q! are Ex commands.

What does V mean in Vim? ›

The Visual mode allows you to visually highlight (select) specific text areas and run commands on them. To enter the Visual mode from the Normal mode, you can press on v—which will mark the beginning of the selection—and then use the arrow keys to move the cursor to the desired end of the selection.

How do I undo a DD in Vim? ›

Press u, :u, or :undo to undo the last change (entry). The reason why it is important to be in normal mode is because pressing u in another mode may trigger different results, like turning the selected characters lowercase.

How many Vim modes are there? ›

One of Vim's unique feature is its modality. Most editors have one mode (insert), where Vim has 6 modes (normal, visual, insert, command-line, select, and ex). I think the 3 important ones are normal, insert, and visual. In this article, I will explain what they do and how to become more productive with them.

What is the difference between ex mode and command mode in Vim? ›

Ex mode is different from Normal mode and Command-line mode, which are perhaps the two most common modes in Vim. However, the main difference is that we don't immediately see the effects of our commands on the screen. Instead, we have to press Return to execute each command and see the results.

How do I go back to normal mode in Vim? ›

To switch back from Insert mode to Normal mode, the default key is the Escape key (Esc).

What is the difference between command mode and insert mode? ›

Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc. When entering a file, vi is in command mode.

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6138

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.