DEV Community

Abdelrahman Mohamed Abdelnabi
Abdelrahman Mohamed Abdelnabi

Posted on

Why Vim is Great and why use it!

My first impression of vim was rather not impressive. The first time I saw or used vim was around 4 years ago when I was in my first year of university. I didn't imagine how could someone use a text editor in a terminal with no GUI, no mouse, no buttons, and no clear way how to exit it. But here I am four years later, using vim like a maniac and loving it. So what made me do the switch?

Just to make sure were are on a common ground, Vim is a text editor rather than an IDE. So it is similar to Sublime rather than VSCode or IntelliJ. This means you won't get the features offered by various IDEs, however you will get a tool that will make text editing absolutely enjoyable and efficient.

Vim is designed so that your fingers will rest on the home row of your keyboard most of the time, with no need to switch to the arrow keys or to the mouse. All editing can be done using just keystrokes including switching tabs, running commands, copying and pasting text, and any other thing a text editor can do. By eliminating this context switch between the keyboard, arrow keys, and the mouse, you gain a significant improvement in efficiencyj once you get your head around Vim commands. I definitely admit that Vim has a steep learning curve but surpassing it is totally rewarding.

A Glimpse of what you can do with Vim

It would take a book to cover the things vim can do, so here I will try to mention some of the most useful vim features that hopefully will convince you to give it a try and possibly make the switch.

inserting text

Let's start with inserting text. In Vim if you are standing with your cursor in the middle of a line on any character in normal mode you can:

  • use O to insert a new line above the current line and jump to it and go to insert mode
  • use o to insert a new line below the current line and jump to it and go to insert mode
  • use I to jump to the beginning of the line and go to insert mode
  • use A to jump to the end of the line and go to insert mode
  • use i to insert at the current position of the cursor
  • use a to insert at the next character just after the cursor

This is very handy when writing code as you usually need to insert code at one of these locations.

Repetition

In vim you can combine numbers with commands to repeat the command a specific number of times. For example, typing "15k" in normal mode will jump down 15 lines.

In vim you can also use the "." (dot) command to repeat the last command.

Think of how easy it is to repeat the boring stuff in vim this simply.

Navigating the code

Code navigation in vim is very powerful either in the same file or across multiple files. In vim you can just to the next or previous instance of the word under the cursor using * and #, which is useful if you are searching for the declaration/usage of a variable/function.

In vim, there is a thing called a jump list which records the jumps you have made from a location in the file to another location. You can access it using :jumps. You can jump in or out of you jump list using Ctrl/I and Ctrl+O. This turns out very helpful when are jumping between multiple files/functions in the codebase.

You can go to the declaration of a function using "gd" (short for go declaration).

You can jump to the beginning and end of a file using "gg" and "G".

You can jump to the top, middle, and bottom of the screen/viewport using H, M, and L respectively (short for high, middle and low).

You can scroll half a page up or down using Ctrl+u or Ctrl+d

You can jump to the beginning or end of a line using ^ and $.

You can even define bookmarks on any line, or more precisely if you want, a character on a line and give this bookmark a letter which you can use to jump to it.

You can scroll the viewport/screen so that the current line (line with the cursor) becomes at the top, middle, or bottom of the screen using "zt", "zz", and "zb" respectively.

This is just the beginning, the list goes on.

Combining Commands and Text Objects

Text objects are like the operators of an operand, the text that the command will act on. Text objects include:

  • a word (aw)
  • a paragraph (ap)
  • a sentence (as)
  • inner word (iw)
  • inner paragraph (ip)
  • inner sentence (is)
  • text inside parentheses/brackets/quotes/html tags ...

You can combine commands with text objects to do more powerful stuff. So for example, to copy the word the cursor is currently at you can use "yiw" (y for yank and iw for inner word). Similarly, to delete the paragraph the cursor is at you can use "dip". Also, if you a have a string delimited by double quotes and your cursor is anywhere inside the string you can change (delete and go into insert mode) by typing "ci"" (short for change inside ") this also works with other delimiters like [], {}, (), '', ... Very efficient, isn't it?

More about text objects and the difference between each one in this amazing guide here.

recording macros

If you find yourself repeating a sequence of commands to edit some text you can record them and replay them later as much as you want with two keystrokes. You can start recording a macro using "q" then the letter that the macro will be saved at then type the sequence of commands you want to perform then "q" to stop recording. You can then repeat the macro using "@letter" where letter is the letter you saved the macro at. That's just one of the powerful features of Vim.

Plugins

Barebones vim may not have the features provided by powerful IDEs like IntelliJ or PyCharm for example. Features like source control integration, linting, whitespace highlighting, multiple line commenting, file tree navigation sidebar are available as plugins that can be easily installed for Vim through a plugin manager.

You can browse plugins for vim here.

Windows and Tabs

Vim organizes buffers (files) into windows and windows into tabs. You can open a file for editing using :e filename. Window commands start with Ctrl+W then a windows command. You can open windows jump between windows, close windows, swap windows, split windows, resize windows all with Ctrl+W and a single keystroke.

You can open a new tab with :tabnew or :tabn for short and jump between the next and pervious tab using "gt" and "gT".

This logical grouping of windows and tabs is very convenient and makes it easy to organize revelant files into the same tab to together and makes navigating the codebase efficient with a few keystrokes.

Find and Replace

Find and replace in vim is very powerful. It accepts "sed" like expressions and you can apply them to a single line, a sequence of lines, a file, or a directory, or files matching a specific pattern. You can search forward using "/" and backward using "?". You can search for characters forward using "f" and backward using "F".

Defining your own commands (Advanced)

You can define your own commands and bind them to a sequence of keystrokes. Vim has this thing called the leader key which is configurable key that you can define and then use to define new commands and use them by first typing the leader key then the key (or sequence of keys) you have bound the command to. So this leader key allows you to reuse all the keys that vim already uses but use them to perform your own set of commands.

Vimifying your computer

I found the Vim life using the keyword very efficient and found myself trying to adopt it in other tasks. "Surfing keys" is one chrome plugins that allows you to use vim like commands to navigate the web.

Tmux is a terminal multiplexer that allows you to manager multiple terminal windows and sessions in a single window and manipulate them using the keyboard. It is very powerful and it will make your terminal life easier and more efficient and enjoyable.

Where to Start

The vim learning curve might be steep at the beginning but once you are through it you will feel enjoyment when using it. I definitely recommend it if you are a software engineer who is trying to optimize his development and coding time.

I recommend this video series on YouTube by "The Frugal Computer Guy". The tutorials are very easy to follow and will make you a vim user in no time. I've watched many Vim talks but this playlist is very comprihensive.

If you are still reluctant to try it out, most IDEs will have community built plugins that will bring vim editing into it. So this may help you ease yourself into vim and get you up and running without leaving your favourite IDE.

The vim manual is also the go to documentation when you are looking for ways to do a specific task in vim. You can launch it by typing :h <topic> inside vim

Conclusion

Vim is very powerful and what I've just talked about here only scratches its surface. People usually spend years to become experts in vim. It is very normal to find yourself learning new things about vim everyday for years. You just have to have enough courage to take the first step and get past the initial phase of getting yourself around, and I believe you will find it very rewarding.

Top comments (0)