Every programmer starts a new language by writing hello world, and as you’ll see, Vim is a small DSL for text editing. So, here goes.
On the command line, tell Vim you want to create a new text document called
hello_world.txt
. Note, there are some great
grapical Vim clients if you’re not a fan of
working on the command line.
$ vim hello_world.txt
Once you’re in vim, tell it you want to insert some text into this file
i
Now, insert some text.
hello world
Tell Vim you’re done inserting.
<ESC>
Tell vim you want to write the file out to disk and then quit.
:write
:quit
Congratulations, you’ve just written “hello world” in Vim. The “write and quit”
command will work in any Vim, even when you’re ssh
’d into a remote
server.
Things are even easier on the
grapical Vim clients though. Instead of
:wq
you could have just hit your standard save and
quit key combos. On macOS that would be ⌘+s
and ⌘+q
. You don’t even have to
exit Insert
mode to save with Command+s
.
Abbreviated Commands
Vim’s got a lot of wordy commands, like :write
and :quit
, but almost
all of them have shorter variants. The alternatives for :write
and :quit
are :w
and :q
Because this is such a common thing to do Vim’s got a
combo command for doing both. :wq
Tip
Sooner or later you’ll find a post online with a strange combination of
letters that you aren’t familiar with like :nohl
. People like to use the
really short, and obscure variants, even in their config files. I encourage
you to look them up in Vim’s help docs where you can see the
expanded forms and learn what they do.