Beginners guide to VIM editor!!

Beginners guide to VIM editor!!

Learn enough to exit Vim without the help of Google.

If you use a Linux system, Vim can be an advanced and efficient editor. Vim is highly configurable and powerful, but, in this article, I will walk you through Vim so you get the basics right. Once you get comfortable with it, you are free to explore more of its capabilities on your own.

Getting Started

Before we start with Vim, firstly make sure that it is installed on your machine.

To install Vim on the ubuntu system, open the terminal and run the following command:

sudo apt-get install vim

Now that we have Vim installed and ready to be used let's start with creating a file. To create a file, type vim helloworld.txt in your terminal and hit enter and that's it you have successfully entered Vim.

Screenshot from 2022-10-01 17-32-30.png

Before we proceed any further, here I have used the .txt extension but Vim supports several other extensions as well, for example, to edit python code type vim main.py.

When you first enter Vim, it will be in normal mode. To make any changes or to write something into the file we will have to switch to insert mode.

Writing into the file

To write something into the file, enter insert mode by pressing i on your keyboard. In the bottom-left corner, you should see --INSERT--. Which means you are in INSERT MODE. Screenshot from 2022-10-01 16-41-00.png

Save and Exit

Here comes the most important part "Exiting the Vim". To exit first you will have to come to normal mode from insert mode, for that press Esc.

Before exiting, saving the changes you have made is important, for that type :w , it will save your file. Further, type :q to exit Vim. BeFunky-collage.png

Congratulations!! You have successfully created your first file in vim. This was the most basic way to save and exit from Vim. There are multiple other ways as well:

  • :q! exit without saving any changes
  • :wq or :x!save and exit
  • Alternatively, you can also type ZZ ( double Z in uppercase) to save and exit. Just make sure the caps lock is on.
  • :qa exit and close all files

That's all for saving and exiting from Vim. Now, let's move on to the editing and navigation part.

Navigation

While you can easily navigate using the arrow keys, it becomes a very tedious process when you are dealing with several lines of code. Let's make it a little easier by adding numbers to the lines.

Press the Esc key to make sure you are in Normal mode, then type :set number and hit Enter. Type :set nonumber to disable it.

final number.png

Now, to navigate to any line type:<line number>. For example type :5 to quickly navigate to line 5.

  • Type :$ and press enter to go to the end of the last line of the file.
  • Press gg to go to the start.
  • Press $ to quickly move to the end and 0(zero) to move to the start of the current line.

Now that we are comfortable with navigating throughout the file, let's move to the actual code editing part.

Editing

  • Press lowercase o in normal mode to go to the next line in insert mode. Similarly, if you press uppercase O in normal mode it will take you to the previous line i.e. upper line in insert mode.
  • If you are at any particular line and want to delete the whole line, press uppercase S, it will delete that line.
  • If you deleted that by mistake don't worry, just press lowercase u and it will undo that. Similarly use uppercase C or R to redo the task.

Copy and Paste

We all know that we can use the mouse to select text and right-click to copy or paste. Now let's do all this with just the keyboard.

To copy the text first, let's learn how to select the text. Press v and move your Left and Right arrow buttons to select and deselect the text. Press y to copy the selected text, go to the next line, and press p to paste.

Tip: Open the terminal on your machine, type vimtutor, and hit enter. Enjoy learning Vim.