Vim is a highly configurable, free and open-source, text editor and the fifth most popular developing environment in 2019.
It's completely keyboard driven — which means once you get comfortable with the commands, you can edit at an incredibly efficient speed. There are vim plugins and extensions for the browser and for other popular editors like VsCode that allow you to use these same commands in other environments.
Vim seems to have something like a cult-classic kind of following. I remember when I began my coding journey, it was one of the first things I learned about and it felt like magic to me —
and was just as hard to figure out how to get out of...
This is a common memeable issue with beginner Vim users. You get into vim and you can't get out.
Computer exit holodeck
Computer exit holodeck
Computer exit
Computer ?
So first things first:
The command is :q
If you're in insert mode first esc
If you press ctrl
+ s
and haven't disabled the sticky in your startup script, you'll have to press ctrl
+ q
before you can then :q
:
colons follow most commands
and !
at the end of a command forces it
:q!
is the equivalent of force quit
Vim is really useful and worth learning the basics of in case you need to use it. A lot of programs will have it as a default editing tool. The other day I was working on an AWS deployment and the shell used vim by default. Luckily, I already knew how to make the necessary exits, write and exit the program.
I will be reviewing some of the basic commands found in vimtutor, but I recommend starting there if you're new to vim and maybe repeating it a couple of times in the beginning.^1
Other than exiting,
- entering
- movement
- insertion
- deletion
- writing (saving)
- replacement
- undo/redo
are pretty important text editing needs you'll come across as a beginner vim user.
Entering vim is done through the terminal, as a command vim [filename]
So vim with a new file name like vim program-dixon-hill
would open a new, empty file and a vim command with an existing file name would open that vim file vim holodeck
To move around in vim you want to make sure you're in normal mode, not in insertion mode, meaning you can't type anything and use j
to move down, k
to move up, h
to move left, and l
to move right.
When I say insertion
mode vs. normal
mode, Vim as 12 different editing modes, 6 basic modes w/ variations:
(This is from Vim's wikipedia page)^2
- Normal mode - used for editor commands. This is also the default mode, unless the insertmode option is specified.
- Visual mode - similar to normal mode, but used to highlight areas of text. Normal commands are run on the highlighted area, which for an instance can be used to move or edit a selection.
- Select mode - works similarly to visual mode. However, if a printable character, carriage return, or newline (or line feed) is entered, Vim inserts the character, and starts insert mode.[32]
- Insert mode - similar to editing in most modern editors. In insert mode, buffers can be modified with the text inserted.
- Command-line or Cmdline mode - supports a single line input at the bottom of the Vim window. Normal commands (beginning with :), and some other specific letters corresponding to different actions (including pattern search and the filter command) activate this mode.
- Ex mode - similarly to Cmdline mode, it takes a single line input at the bottom of the window. However, in Cmdline mode, entering a command exits the mode when the command is executed. Entering a command in Ex mode doesn't cause the mode to change.
Insert mode is entered by using i
and allows you to type in the file.
You can use the arrow keys to navigate when insertion mode, but it's good to get used to toggling back and forth. HJKL are the basic movement keys, but you'll learn other ways of moving around quickly and efficiently in normal mode. For example 10j
would move your cursor 10 lines down in normal mode, 10k
would move you ten lines up. To escape insertion mode you use esc
used for returning to normal mode from any of these modes.
Returning to normal mode, you can delete a character by moving the cursor under that character and using x
. To delete an entire word, move to the beginning of that word and use dw
for delete word. To delete multiple words, but not an entire line, use d<number of words>w
like d2w
would delete two words, d3w
would delete three words — and so on. Type d$
to delete to the end of the line and dd
to delete the entire line.
To write or save what you're doing, :w
. Using :w [filename]
will write a new copy of that file under the given filename, but will not delete your old file. (There are many plugins to use with vim. Here's one I encountered researching this dilemma)[3]
To replace a character use r
with the cursor placed under it just like you would delete. Capital 'R' will put you in replace mode, replacing each next character with the next one you type until you exit the mode.
To undo you would use u
for a single change, U
for the whole line. ctrl
+ r
is your redo. Vim also keeps track of versions of a file, something like a Git history. I won't go into these details here, but look them up. Also, let me know in the comments below what your best practices are for this and general vim usage.
If you're like me and you like to use vim for other kinds of writing also, check out this vim for writers article. I usually use the WordProcessorMode plugin when I'm writing or editing something other than code and have found it super useful.[4]
[1] a program literally called vimtutor and opened with the command vimtutor
— once installed and run will provide a walkthrough tutorial that you can practice keyboard shortcuts by following along with. It's usually installed with vim, but if not, can be easily installed.
*view on the web
[2] vim wiki
[3] saveas plugin
[4] vim 4 writers
more vim keyboard shortcuts
Top comments (0)