Linux text editors vim

How to Use Vim – Tutorial for Beginners

Arunachalam B

Arunachalam B

How to Use Vim – Tutorial for Beginners

Vim is one of the most popular text editors among Linux users. Linux System Administrators especially often prefer it to other editors.

In this article, you’ll learn a lot about Vim and see how you can quickly start using Vim as a developer.

What is Vim?

Vim is an acronym for Vi IMproved. It is a free and open-source cross-platform text editor. It was first released by Bram Moolenaar in 1991 for UNIX variants.

Vim is based on the original Vi editor, which was created by Bill Joy in 1976. In the 90’s, it started becoming clear that Vi was lacking in some features when compared with the Emacs editor. So Bram implemented many missing features and released it under the name Vim.

How to Install Vim

Vim runs across various platforms such as Windows, Linux, and Mac.

To install Vim on Windows, download the executable file from the Vim site and run the file. Follow the instructions shown on the screen and you’ll be good to go.

Vim comes pre-installed on most *nix operating systems. But if it’s installed on your system, you can install it with a package manager of your choice.

Here’s the installation command for Debian-based operating systems:

image-274

Once you enter the above command, you’ll be able to see a screen displaying info about Vim and some instruction to find help and exit Vim.

image-276

Vim Modes

You should be aware of the most important concept in Vim before moving on: modes in Vim.

Everything in Vim is considered a mode. You can achieve whatever you want if you understand modes in Vim. There are many modes in Vim. But, we’ll be looking at the 4 most important modes.

Let’s explore them one by one.

What is Command Mode?

This is the default mode (also called Normal mode) in Vim. Whenever Vim starts, you’ll be in this mode. You can switch to any mode from this mode. You can’t do this in any other modes.

Basically, to switch from one mode to another, you have to come to Command Mode first and then navigate to the other mode. The commands that you run without any prefix (colon) indicate that you’re running the command in command mode.

Читайте также:  Bios из под linux

What is Insert Mode?

This mode is used to edit the contents of the file. You can switch to insert mode by pressing i from command mode. You can use the Esc key to switch back to command mode.

What is Command Line Mode?

You can use this mode to play around with some commands. But the commands in this mode are prefixed with a colon (:). You can switch to this mode by pressing : (colon) in command mode.

What is Visual Mode?

You use this mode to visually select some text and run commands over that section of code. You can switch to this mode by pressing v from the command mode.

The above 4 modes are enough to perform a basic set of file operations in Vim.

Ok the theory is done. Let’s explore Vim practically.

Common Text Editor Operations in Vim

How to Create a New File

Creating a new file with Vim is simple. You can do it from command line mode.

Run the following command to create a new file:

image-278

After running that command, you’ll be in command mode (as shown in the below screenshot), and you’ll not be able to enter any text:

image-279

To add some text to the created file, press i in the keyboard. You use the i command to enter some text in the file. Once you press i , you’ll be able to see that you entered Insert mode in Vim by looking at the bottom left of the file.

image-280

In this mode, you can type whatever you want into the file.

image-281

We’re done with writing our content. And now we’ll want to save the file. If you do not save and just close the terminal at this point, all your content will be lost.

How to Save a File

To save a file, you have to switch from Insert mode to Command Line mode. Remember I told you earlier – whenever you want to switch from one mode to another, you have to first switch to Command mode and then you can easily switch to whatever mode you want.

To switch to Command mode from Insert mode, you have to press the Esc key.

After you press the Esc key, you’ll not be able to see the —INSERT— at the bottom left. This indicates that you’re not in Insert mode anymore and you’re now in Command mode.

image-283

To save the file, type the following command:

image-284

How to Close a File and Exit Vim

Once you save the file, you can close Vim by closing the terminal. But, the proper way to close the file and the Vim editor is by using the following command:

Читайте также:  Amazon linux install docker

image-285

The above command closes the file and quits the vim editor.

Alternatively, you can use a command that’s the combination of the above 2 commands (save & quit) to quickly save and quit Vim. The command is:

image-287

We are in Command mode now. To edit the file, we have to switch to Insert mode. As we saw earlier, pressing i from the Command mode will switch to Insert mode.

image-289

Follow the same procedure for saving the file and quitting Vim. Press Esc on the keyboard and type :w to save the file and :q to quit Vim.

You may wonder, though, what if I want to close the file without saving my changes? (Ignore the changes I made and bring the file back to the old state).

How to close the file without saving the changes

To close the file without saving the changes, you have to run :q! from Command mode.

Let’s explore this with an example:

image-292

I’ve added some more content to the file.

image-293

But, I don’t want to save the changes I made now. To close the file without saving this change, we have to switch to Command mode (by pressing the Esc key). Type :q! in the Command mode. This will close the file ignoring the changes made.

image-294

Let’s view the file and confirm if we get the expected output:

image-295

Yeah. The file does not have the last line which we added recently. So, the changes were not saved.

How to Cut, Copy, and Paste Text from a File using Vim

You can Cut, Copy, and Paste in 2 ways in Vim. They are:

Out of these two ways, Visual mode is simpler to understand. Since this is a beginner-friendly guide, let’s explore how to cut, copy, and paste in Visual mode.

Let’s open the file in command mode before we proceed:

image-231

Let’s assume you want to copy the word «Hello» from the 1st line and paste it into the 3rd line.

The first step is to place your cursor in the place from where you want to copy the text (being in command mode).

image-232

Enter Visual mode by pressing the v key on the keyboard.

image-233

The — VISUAL –- text at the bottom left indicates that we’re in visual mode.

Move the cursor to the place where the text you want to copy ends.

In this case, I’m moving the cursor to the letter o of the word Hello .

While you move your cursor, Visual mode highlights the text from the beginning up to your cursor.

image-235

Once you’re done moving your cursor to the right place, hit y to copy the text or hit d to cut the text.

Читайте также:  Cmd lines for linux

In this case, I’m copying the text. So, I press y on my keyboard.

Move the cursor to the place where you want to paste the text.

image-296

In our case, we have to move the cursor to the 3rd line.

Press p (in lowercase) to paste the text after the cursor and P (in uppercase) to paste the text before the cursor.

image-236

Press :wq to save and close the file

How to Find and Replace Text in Vim

Finding text and replacing it with some other text is simple and straightforward in Vim. There’s a one-line command that simplifies this entire process.

image-237

Our sample.txt file has 2 «Hello»s. Let’s replace «Hello» with «Hi» at both places.

The command to do that is:

image-238image-239

Let’s understand this with another example.

This time, I want to change the word «Hi» (case-insensitive search) that occurs between lines 2 and 3 and replace it with «Hello and Welcome», with a confirmation to change each occurrence.

The command to do that is:

image-297image-241

Here’s a description for each option:

  • y — Replace the match
  • n — Skip the match
  • a — Substitutes the match and all remaining occurrences of the match
  • q or Esc — Quit substitution
  • l — Replace the match and quit
  • CTRL+Y — Scroll the screen down
  • CTRL+E — Scroll the screen up

I want to accept the change. So, I press y . Here’s the output after pressing y .

image-242

Since we have only one occurrence of «Hi» between lines 2 and 3, it did not ask for any further confirmation and the operation is complete.

How to Undo or Redo in Vim

To undo a change in Vim, press u in command mode. To redo, press CTRL + R . You can prepend a number (n) with u to undo n times. for example, 2u will undo 2 times. To list the available undo options, type :undolist in command mode.

Let’s understand this with an example.

Here’s the current state of our sample.txt file:

image-254

I have my cursor at «a» in the «Hello and Welcome» text on 3rd line. Let’s remove the word «and» by typing dw on command mode:

image-257

Let’s undo to bring the word «and» back into the file. To do so, press u in command mode.

image-258

Let’s redo and take away the word «and» by typing CTRL + R in command mode.

image-298

Conclusion

In this article, you have learned the basics of Vim. This article should be enough to get you going and do some basic file read/write operations with Vim.

Just keep in mind that I’ve not even covered 1% of Vim. But I’m sure these basics will help you explore Vim quickly and confidently.

To learn more about Vim, subscribe to my email newsletter at my site and follow me on social media.

Источник

Оцените статью
Adblock
detector