can't exit vim

Vim for JavaScript Developers

Advantages of Learning Vim

If you're a JS developer, it will benefit you to learn Vim.

First of all, Vim's available everywhere. If you have a virtual server, it takes 1 second to install. It works every time, without customizations or anything.

Second of all, it's fast. No editor is faster to load than Vim, and because it's so stripped down, it never hiccups.

Third, it's good to know because if you're on someone else's server, it's probably the fastest thing you can open.

Fourth, I sometimes like to use vim because it's concept of "modes" (see below) makes it very hard to accidentally modify a file.

If I'm in a config file, I often want to be very sure I'm not dropping in a stray apostrophe somewhere. Vim gives you confidence that you're only editing the file when you want to, no exceptions.

The Modes of Vim

Vim isn't very intuitive to learn. If you're in it, you'll notice the usual ways you use to move around every other editor suddenly don't work.

Vim has 'modes'. The idea is that, when you're doing one thing, you should be in one mode, and when you want to do something else, it's time to switch to a different mode.

When you start, you're in normal mode, which lets you navigate through a file, but not modify it.

"Insert mode" is generally the mode newbies are trying to access. You can access it by typing the letter "i". You can exit it by typing the Escape key. (In general, hit Escape when you're lost in Vim and looking for an answer).

Also, read the modeline at the bottom of the screen. That will often tell you "type :q to quit." Many times it'll answer your question.

Sometimes you can get really stuck, if you mash the wrong combination of buttons. The modeline can help you here; read what it says and google how to exit.

This has happened to me once or twice before (even with years of experience); getting stuck in vim really does happen. Don't feel bad if you need Google search help to exit every once in a while.

The Basic Keystrokes

Fundamentals

Here are the basics, which you can get really, really fast at, with some practice.

Open a file with vim from the command line like this:

vim file.txt

Type a single key to do a single command.

If you want to do a command multiple times, prefix the command with a number. I'll sometimes type "100j" if I want to go to the middle of a file (or "2000j" if I just want to not think about it and jump to the bottom).

Navigation

Navigate around like this:

Press "l" to go right on a line.

Press "j" to go down a line.

Press "k" to go up a line.

Press "h" to go left on a line.

Type "/" and then the text you want to search for, when you're searching for text in a file. (Personally, I use this a lot.)

Editing

Press "i" to be able to enter file insert mode. Make any changes you want, and press "Esc" to exit.

Press "A" to append at the end of a line, and press "O" to insert a line before your line and put there cursor there.

Press "x" to delete the character the cursor is currently over.

Press "dd" to delete a line. There are different permutations of "d" with another letter - example, "dw" to delete a word - which you can move on to next.

Press ":w" to write your changes to file.

Press ":q" to exit the file, ":q!" to exit it without saving any changes, and ":wq" to write and exit with no further questions.

Use A Cheatsheet

You'll want to use a cheatsheet and memorize the keystrokes to become really proficient.

Devhints.io has a good one, though there are many others you can use.

vim basics

Customize for JavaScript

If your file has a common extension - like .js for JavaScript - Vim will often recognize it and do syntax highlighting on its own.

Syntax highlighting means that reserved keywords (for example, const and await) will be different colors in your code. This way they'll stand out when you read over your program.

If you already have JavaScript highlighting, you don't need do anything else.

If you don't have syntax highlighting, consider a plugin like vim-javascript.

If you need extra functionality, you can look into plugins, of which Vim has many.

Practice Using Vim Often

Remember that with Vim, the more you use it, the faster you get. So, use Vim a lot to get really fast with it.

That's when Vim becomes a joy to use, when you can move as fast as you think. The fastness and speed make it fun.

I'd encourage you to wait until you get good before you make any final decisions on whether you'll keep vim on your machine.

And if you decide to make vim your permanent editor, it's a solid choice.