DEV Community

Cover image for Vim tutorial- learn Vim the easy way
zaira
zaira

Posted on • Edited on

Vim tutorial- learn Vim the easy way

Vim is a very powerful and easy to use built-in Linux text editor. In fact, once you master it, vim is the only text editing tool you need to know. Many say it's hard to use, dull and boring but it's all gibberish! I am writing this post to change your views about vim :)

Vi or vim (vi enhanced) are text editors available in Linux distributions by default. It would be quite unfair to simply not be bestowed with their advantages.

If you are used to downloading the file, making changes using a GUI, and then uploading the file back to server, then please stop :p You'll stop doing this once you get to know vi. Besides, these steps are unnecessary and Kafkaesque*, right? This also puts the file in danger by adding extra whitespace which might soon turn into an untraceable production nightmare.

Nerd fact - Kafkaesque meaning: something that has been made complex illogically and in a bizarre way.

Advantaged of using Vim?

  • Vim editor is available by default on all Linux distros. You can start working on it right-away.
  • Some other Linux utilities, for example, editing cron jobs, work in the same pattern as vim.
  • No internet, no mercy. Let's say you are used to working in other (non-default) editing tools but suddenly if you lose access to those tools, and there is no internet access to install them, then you know where you stand. This could happen if you are trouble shooting in someone else's environment.
  • Most servers run headless, so in system administration, you don't necessarily have the luxury of a GUI. But vim has got your back; it would always be there.
  • As vim is a CLI tool, it is faster than GUI tools.
  • Vim keeps the file properties intact. Downloading the file, editing and uploading it increases the chance of whitespace addition. This could be the case with third party text editing tools as well.
  • Vim is suitable for all- beginners and advanced users. Vim supports complex string searches, highlighting searches and much more. Some developers even use vim for code editing, sounds strange right? But you can see the potential here :)

How to start using Vim ?

It's easy. You need to know the 3 operating modes of Vim and how to switch between them. Keystrokes behave differently in each command mode, and that's the magic of Vim :D Let's dive deeper.

Moving to Start or End of Line in Vim

Press ^ to move the cursor to the start of the current line. Press $ to move the cursor to the end of the current line.

Vim modes

In Vim, there are three modes to work with:

  1. Command mode.
  2. Edit mode.
  3. Visual mode.

Command Mode.

When starting vim editor, you land in the command mode by default. This mode allows you to access other modes.

To switch to other modes, you need to be present in the command mode first

Edit Mode

This mode allows you to make changes to the file. To enter edit mode, press 'I' while in command mode.

Note the '-- INSERT' switch at the end of the screen.

Insert mode in vim

Visual mode

This mode allows you to work on a single character, a block of text, or lines of text.

Let's break it down into simple steps. Remember, use the below combinations when in command mode.

  • Shift + V → Select multiple lines.
  • Ctrl + V → Block mode
  • V → Character mode

The visual mode comes into handy when you need to copy and paste or edit lines in bulk.

Image description

Extended command mode.

The extended command mode allows you to perform advanced operations like searching, setting line numbers, and highlighting text. We'll cover extended mode in the next section.

How to stay on track?

If you forget your current mode, just press ESC twice and you would be back in Command Mode.

Some common operations using Vim

1. How to copy and paste in Vim?

Copy-paste is known as 'yank' and 'put' in Linux terms.

To copy-paste, follow these steps:

  1. Select text in visual mode.
  2. Press 'y' to copy/ yank.
  3. Move your cursor to the required position and press 'p'.

2. How to search for text in Vim?

Any series of strings can be searched with vim using the '/' in command mode.

To search, use /string-to-match.

In the command mode, type :set hls. This will highlight the searches.

Let's search a few strings:

search text in extended command mode

3. How to exit Vim?

That is a million dollar question! 😃
But it is as easy as abc, trust me.

First, move to command mode(by pressing escape twice) and then use these flags:

Exit without saving → :q!

Exit and save → :wq!

Using hjkl for navigation

In early days of CLI, the keyboards didn't have arrow keys. Hence, navigation was done using the set of available keys, hjkl being one of them.

Although arrow keys would work totally fine, you can still experiment with hjkl keys to navigate. Some people find this this way of navigation comfortable.

Tip to remember: hang back, jump down, kick up, leap forward.

Time for action !! 😁

Remember learning in IT is nothing without hands-on. To quickly access a Linux shell and practice what you have learned, hop on to Replit! It's free! This tutorial was made using replit.

So, this is it ! You have mastered the basics. With this tutorial, you can easily view, edit and save your files using the Vim editor.

Wrapping up

I would love to connect with you on any of these platforms.

Image description

Top comments (0)