Vim for starters - the minimum you need to know

Hamza Tamenaoul - Dec 25 '17 - - Dev Community

Originally posted in my portfolio.

The first time I heard about Vim, it was 5 years ago. And my natural reaction was : " Why in God's name, will I sacrifice the comfort of a modern text editor like sublime text, for an outdated, ugly, and complicated text editor ? " (You can't even use the mouse !).

Last year, I immersed in the fabulous world of Linux customization, this fixed the ugly part. So I decided that maybe I should give Vim another shot. And now, I am a happy Vim user for six months.

In this article I will not try to convince you to switch to using Vim, I will only give you the basics, the "what you need to know as beginner" to dive in the fabulous world of Vim.

Let's start !

Installation

Windows:

Well you need to download the installer from the vim website, install it, and you are good to go !

Linux ( Debian based distributions ) :

It's as simple as it seems !

sudo apt-get install vim
Enter fullscreen mode Exit fullscreen mode

Mac :

You can use Homebrew.

brew install vim
Enter fullscreen mode Exit fullscreen mode

A little bit of theory (just kidding)

Before you start using Vim, you should be aware that vim has a different philosophy. For starters, your mouse in the beginning is useless. Secondly it interprets what you type on your keyboard based on which mode you are in. Depending on the mode, your input will trigger different Vim behaviors. The default mode, the one you'll find yourself using when you open vim is the normal mode. it's in this mode that you'll be navigating in your file or executing vim commands. The second one is the insert mode, the most friendly, since it makes vim do what you expect a text editor to do, write text. There are other modes that I won't cover in this article since they are more advanced. As a reminder, if you find yourself lost in a mode, then the escape key is your friend, it will put you in the normal mode.

The fun part !

Opening or creating a new file:

Now open your favorite terminal (or CMD/PowerShell), and type the command :

vim name_of_your_file
Enter fullscreen mode Exit fullscreen mode

This will open the file you specified, or the create a new one if it doesn't exist. Once you do this you'll find yourself in the normal mode. We are not writing text right away, first you need to know some basic Vim commands. A Vim command usually start by typing ':' then the command, and the first one is exiting Vim since it's the most asked question about Vim.

  • ":q": To quit Vim. Note here, Vim won't allow you to quit if you didn't save your file.
  • ":w": To save (write) a file.
  • ":wq": To save and quit.
  • ":q!": To force quit if you want to force quit without saving the file.

Writing some text (Finally ...):

Well if you survived until here, you'll find your reward by typing "i". It will put you in the insert mode, and now you can type text as you please. And when you are done, you can use your escape key to go back to the normal mode to save your changes or quit.

Some other useful shortcuts (In the normal mode) :

  • "dd": To cut the line where the cursor is.
  • "yy": To copy the line where the cursor is.
  • number_of_lines_you_want_to_cut + "dd": You guess it, it will cut the number of lines you specifies starting from the current line of the cursor.
  • number_of_lines_you_want_to_copy + "yy": Again the number of the lines you want to copy starting from the line with the cursor.
  • "p": To paste starting from the next line of the cursor.
  • "/" + term_you_want_to_search: it will make the cursor go to the next occurrence of the term.
  • "n": To go to the next occurrence of the term.
  • shift + "n": Go to the previous occurrence of the term.
  • "u": If you messed up something, this will undo the last change. You can press it multiple times to go older in the changes' history.
  • ctrl + "r": To redo the changes.

Bonus (Showing line numbers):

:set number
Enter fullscreen mode Exit fullscreen mode




Conclusion

Vim, or not Vim, this is not the question. A text editor is ,first of all, just a tool to achieve bigger things. But a great tool, makes them easier. I hope that, with this article, I have helped you in your quest of finding the adequate text editor for you. And that this quick introduction to Vim, have opened your appetite to learn more about Vim, or a least have expended a little bit the horizon of your knowledge.

PS: This is my first blog post ever, and english is my third language, so I am open to feedback and good criticism, and thanks for reading !

. . . . . . . . . . . . . . .