Basic tips about vim

1,344 阅读5分钟

Why I love Vim: It’s the lesser-known features that make it so amazing

Since I started using Vim in 2016, I’ve discovered several lesser-known features that Vim offers out of the box without any plugins.

Can you cover some basics before you start rambling about these new things?

Oh sure! Before I copy paste a few commands from a cheatsheet, I am going to make a bold assumption: you wouldn’t be reading this if you wanted a cheatsheet and you already knew Vim basics.

You might have just heard that Linux distributions ship with a default command-line text editor called Vim, and may want to just give it a try.

So, let’s assume you are completely new to this whole game and start from just what we need as basics (without history/boring theory).

NOTE: If you know the basics, click here to scroll past them

What’s your deal here compared to tons of other articles on Vim?

Most of intro articles on Vim begin with modes of Vim, inserting, saving and exit. If you’re really in the mood of theoretical perfection learning mode, feel free to read whatever helps in wikibooks.

There are also some great books and articles that tell you that there is a philosophy behind the way Vim works and that the commands in VI/Vim are meant to be combined. Absolutely true and I’m sure you will appreciate it once you get used to the editor and the power it provides.

I have heard funny stories and seen funny images about learning curve of Vim. Is that true? Is it actually that bad?

Well, haters gonna hate 😜 However, according to me, the image that somewhat gives a proper representation of Vim is:

Courtesy: pascalprecht.github.io/2014/03/18/…

The majority of the articles on Vim refer to the learning *curve* as a learning *wall*, but hey, there’s some positivity: look at the other side of the wall!

For beginners, it’s literally a wall since they have never done anything like this before to use an editor on command line. The thing that appealed most to me when I started as a beginner was the ubiquity of Vim.

Log in to any (non-windows) machine from any terminal and you can literally get an editor by typing *vi* with your eyes closed. The editor will appear in front of you!

Another thing that appealed to me is the ability to work without a mouse and without wasting any productive time on touchpad or getting a mouse for laptop.

I know, I know, I can hear some of you yelling “Emacs! Emacs!” I get it. But once I was hooked to Vim, I just never really had any interest in emacs (may be because of the installation required). So, yeah emacs is also great I guess. Feel free to jump ship before you start sailing on this beautiful journey with VI(m).

I just opened my terminal and typed vi and hit return key. All i see is a welcome screen. I can’t type and I don’t know how to get out of it. Are you sure it’s a powerful editor with capabilities?

100% sure. The behavior you just witnessed is the *wall* we saw earlier. Trust me VI(m) can do a lot of other things. It just has its own ways of using it. You can edit files, open tabs, split screen horizontally or vertically, browse file system, run linux commands without leaving your file, trigger make builds from your source code without exiting the file, bookmark directories, even better: bookmark lines of a file, find and replace words, of course copy-paste and a lot more.

Yeah! Like that’s a big deal for an editor to support those. Meh! Everyone does that. What’s the big deal?

There’s no big deal, the only deal I see is the ability to focus on your file/code without leaving keyboard. Really, if you don’t mind using a mouse, then go ahead and open your MS word/GUI Editor and do all the editing you wish to do.

Fair enough. But, seriously why not an IDE for some work?

Okay, so you are a developer and have had some liking/love for an IDE. No, VI(m) is not a replacement for your shiny IDE. VI(m) does not have the out of the box awesome capabilities of your IDE. VI(m) is just small in size (package and installation) compared to the bulky IDEs and is available to use without any configuration and installations. Seriously, VI(m) is no match for some great things your IDE provides.

Enough talk, show me the basics?

Sure, before you begin, just keep in mind that any Vim user has to basically deal with command mode and insert mode. There’s no escape (literally, not the Esc key).

Let’s say you are using some editor and you want to delete a long function in C language. The simple steps you do are: Position your cursor at the beginning of the line, then press Shift + Down arrow till end or use mouse. This action that you had to do to select those lines required you to *stop* typing and press keys. Isn’t it? Don’t tell me you were typing something and simultaneously pressed keys to magically select the body of your function.

Be reasonable. You paused typing and did the selection work to tell your editor that you want to do something with this text (copy/cut/Bold/italics/anything).

This pause that you took is equivalent to being in command mode in VI(m). This is the time when you tell VI(m) that you want to do some actions on some lines/word/anything and you are not going to type. Now, VI(m) throws you out of insert mode and you are locked out of typing text in your file. Obviously, the other mode in which you can actually type in your file is the insert mode.

BTW, if you were wondering how are you selecting body of function without selecting text or using mouse, I accomplish that using keystrokes: :d%

Yes, that deletes the contents of your function body. No, that’s not some creepy combination of keys to remember! Just so you don’t think it was nonsense, all specific operations you perform begin with : . d indicates you want to delete something. % is going to move the cursor to the end of the matching brace.

Now, that we have established the basic modes, let’s dive into basic VI(m).

If you know the name of the file you are writing:

$ vi myfile.c

If you are not sure of file name and want to start typing:

$ vi

As soon as you open vi, you will be in the command mode. To enter insert mode, press i . Type whatever you wish. Press Esc to return to command mode. Now you have a few options to exit depending on how you opened vi.

If you gave a file name: :w will write those changes safely to disk. :q will quit the editor. You can combine these actions with : :wq and Return key

If you did not give a filename: :wq filename.c will write the contents to the file filename.c and quit the editor. If you are not interested in the text you wrote and wish to exit without saving anything: :q! and you are out! The ! is required at the end to say : “Yes, I am sure I don’t want to save the contents and I want to get out urgently”

[DEMO] Basic vim usage
To start the vim editor: Use vim command on shell To start editing a file using vim, use : vim filenameasciinema.org

There you go! You just created, edited and saved(or may be not) your first vi file. Congratulations 😊

As I mentioned earlier, this is not a beginner’s introduction to VI(m). There are plenty of other articles (I will provide reference at the end of article) to get started. I just inserted that intro so that you’re not disappointed after landing on this page and finding nothing to learn😜

This is the line where beginners say goodbye to intermediate users and head to the reference section for more brilliant intro articles.

Welcome to the intermediate users. These are some cool capabilities of VI(m) that I wasn’t aware of. But now I use them daily to be more productive.

For those of you who prefer TL;DR:

  • tab-pages
  • sessions
  • line numbers (+ marks) and copy/paste
  • folds
  • indention with =
  • insert-completion
  • netrw
  • splits/windows
  • :! and a little bit about :make

Vim tab-pages

Did you mention tabs in Vim? I didn’t know that existed!

I know, right! A tab page is a page with one or more windows with a label (aka tab) at the top.

If you are interested in knowing more about windows, buffers, tab pages: technical details

Have a look: