Getting Started with Vim

If you’ve found this post through my Twitter, you likely already know my affinity for vim. For those who don’t, here’s a general idea:

That’s right, I’m writing this in vim. Pretty neat, huh?

Vim, or vi improved, is a text editor. Vi is also a text editor, but as you could infer, vim was made to have additional functionality. Vim is specifically known for providing a methodical way to make keyboard designs more efficient for editing text in a terminal (or command line). When you aren’t in insert (editing) mode, different keys will help you move around your file without having to defer to the arrowkeys. And, since you can’t click to a certain place when in a terminal, these keypresses will help you move around more fluently than waiting for the backspace arrow (or its equivilent) to get you there.

In this post, I’m going to introduce everything you need to know to start working in vim. We won’t get to every vim trick that exists, but by the end of this post, you’ll be able to create, edit, save, and exit your files in vim, all whilst moving around your files in a nice, elegant fashion.

Installing Vim#

Vim doesn’t necessarily come pre-installed, so you may need to install it yourself. The following commands can be used with a CLI (Command Line Interface) to install it.

To install in Linux: sudo apt get vim

To install on MacOS (using Homebrew): brew install vim && brew install macvim

To install on Windows: Download from link below

You can also download any OS version from the browser here.

Creating or Opening a File with Vim#

To open an file with vim, all you have to do is type vim [file_name].

If the file already exists, it will open that file. If the file does not exist, it will create a file with that name.

Editing in Vim#

KeypressPurpose
iEnter insert mode where you are. You will start typing wherever your cursor is.
aEnter insert mode directly after the curser.
oEnter insert mode on a new line below the current location of your curser.
IEnter insert mode at the beginning of the line your curser is currently on.
AEnter insert mode at the end of the line your curser is currently on.
escEscape insert mode.
xDelete the character your curser is on.
rReplace the character your curser is on with whatever letter or special character you type next.
ddDelete current line.
yYank (i.e. copy) current line.
pPaste copied/deleted text.

Notes:

  • Deleted text can be considered as cut text. If you paste after deleting something, it will paste whatever was deleted.
  • If you forget to enter insert mode before editing the file, you may end up pressing some wild collection of keypresses (yes, I do still do this sometimes).

Saving and Exiting Vim#

KeypressPurpose
:wSave your changes.
:wqSave your changes and exit the file.
:qExit the file.
:q!Exit the file no matter what (e.g. without saving despite having made changes or if you accidentally have the file open in two locations).
KeypressPurpose
kMove up
jMove down
hMove left
lMove right
wMove to the start of the next word
eMove to the end of the next word
bMove to the start of the previous word

Extras#

KeypressPurpose
/[keyword]Search for [keyword]. Press n to move to the next instance of [keyword].
:[line number]Move to [line number]
:%s/[x]/[y]/gReplace all instances of [x] with [y]

Get 1337 Vim Skills#

If you want to continue improving your vim, there’s an awesome site called Vim Adventures where you can play an interactive game that teaches you how to use vim along the way. I recommend checking it out.

Be sure to let me know if you decide to join #TeamVim!