Five minutes to love Vim

Hello,

Today’s article is a result of my sympathies and love for Vim. Sympathies because not a single person on internet would leave Vim without mocking. Even exiting Vim is considered as tough as getting an interview with the company you like (hottest thing on my mind πŸ˜› ). Jokes apart. Vim is the best editor you can work with on a Linux system. Here I present you the basic HowTos in Vim which is going to be enough if you want to use it everyday for writing codes.

  1. Want to insert something in a file?
    Use the Insert mode. How?
    Open up the file with vim:

    vim _filename_

    and then,
    Press ‘i’ or Insert key on your keyboard. (There are other ways too but this would suffice. πŸ˜‰ )
    You are ready to insert any characters now.
    Vim even shows what mode you’re in (if configured like that), see below:
    screenshot-from-2016-09-20-00-42-55

  2. Want to run a command?
    Get to the command mode by pressing the Esc key on your keyboard.
    Now, the screen looks like:
    screenshot-from-2016-09-20-00-48-28
    Then,
    Press colon (:) and write the command.
    screenshot-from-2016-09-20-00-50-03
  3. vimrc
    You must have seen an rc file for different utilities that you use in your system. This file is used to rc or “run commands” before invoking. It contains the configuration settings for your utility. The .vimrc that you create after the installation contains all the commands which are run every time you try to open a file using Vim.
    Now, the question is why do we need to have a .vimrc file when we can get into the command mode and run all the commands that we want to?
    Because, the changes you make in .vimrc are going to be persistent, however, whatever you do in the command mode for a particular file is valid only for that instance. See the below example.Suppose you want to see the line numbers of the file you’re working with. What do you do? Get into the command mode by pressing Esc key, and then,

    :set number

    Result:
    screenshot-from-2016-09-20-01-02-30
    Now, the next time you try to open up the file using vim, result:
    screenshot-from-2016-09-20-01-05-16
    Conclusion: The command you used the last time showed results for that instance only. However, if you write the same command in your .vimrc. Every time you open up a file using vim, you’ll get the line numbers.
    Now, you do understand what these rc files are for, right?

  4. Exiting vim
    No, it is not that O RLY? tough. ( πŸ˜› ) It is as simple as, getting into the command mode by pressing Esc key, then colon and “q”. YES. q for Quit. That’s it.
    If you want to write file to the disk (Save the changes you made to the file), you do:

    :w

    write file to the disk (Save) and quit:

    :wq

    quit without showing any warnings (Otherwise, Vim would show a warning if you’re trying to exit a modified file without saving it):

    :q!
  5. What if you want to copy some text into a file without disturbing the alignment?
    Answer: The paste mode.

    :set paste

    This is for you copying codes. ( πŸ˜› )
    Note that this is going to convert tab characters to space characters, so if you’re making a patch for Linux kernel, you better be careful.

    To get out of this mode, you do

    :set nopaste
  6. Copy
    Forget Ctrl+C, Vim lets you do it even more easily. (Did you notice, it rhymed? 😎 )
    OK, kidding. May be not that easily but it isn’t tough either.
    You need to
    Get into visual mode. How?
    Press Esc and then ‘v’. Make sure you are at the character where you want to start copying from before getting into this mode.
    screenshot-from-2016-09-20-01-45-41
    Then, you select (simply move the cursor using arrow keys) all the text you want to copy and press ‘y’ (Yank).
    Text copied.
  7. Cut
    Get into the visual mode. Select the text and then press ‘x’.
    Text cutted. (This was to make you frown, you Grammar freaks! πŸ˜‰ )
  8. Paste
    After copying or selecting the text, without having changed any modes, go to the character where you want to paste, and press:
    * ‘p’ to paste after the character you’re on.
    * ‘P’ to paste before the character you’re on.
  9. Undo
    Go to the command mode and,

    :u
  10. Want to see the difference between two files?
    Just do:

    $ vim -d _file1_ _file2_

    Result:
    screenshot-from-2016-09-20-02-05-17

That’s all you need for an easy work around with Vim. Sure there are many many things you can do with Vim and many commands that you can use but mostly for a simple use, you just need this 5 minutes guide.

Thanks for reading.

4 thoughts on “Five minutes to love Vim

  1. Vim is beating me up all the time, whenever I try to use it. This guide makes it look less wild, somewhat. The latest smack I got from Vim was when I was peacefully editing something and all of a sudden Vim stopped working. Completely. Since my laptop is quite old and prone to having memory issues, I thought – oh, well, let’s just sit and wait and it will eventually come back to life. After a little while, I tried again. Nada. After finding out that all other applications were working just fine and googling a bit, I learned, with some exasperation, that my “beloved” and almost absentmindedly used `ctrl+s` would freeze all terminal output, until you hit `ctrl+q`
    I think that’s an extra tip to save precious minutes to newbies like me πŸ˜›

    Liked by 1 person

Leave a comment