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:
All I do is vim vim vim no matter what— casey (@varcharr) June 24, 2020
why exit vim when you can stay in it forever— casey (@varcharr) May 15, 2020
Do I write my blog posts in vim?— casey (@varcharr) August 23, 2020
Yes, yes I do.
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.
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
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#
|Enter insert mode where you are. You will start typing wherever your cursor is.
|Enter insert mode directly after the curser.
|Enter insert mode on a new line below the current location of your curser.
|Enter insert mode at the beginning of the line your curser is currently on.
|Enter insert mode at the end of the line your curser is currently on.
|Escape insert mode.
|Delete the character your curser is on.
|Replace the character your curser is on with whatever letter or special character you type next.
|Delete current line.
|Yank (i.e. copy) current line.
|Paste copied/deleted text.
- 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#
|Save your changes.
|Save your changes and exit the file.
|Exit the file.
|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).
Navigating in Vim#
|Move to the start of the next word
|Move to the end of the next word
|Move to the start of the previous word
|Search for [keyword]. Press n to move to the next instance of [keyword].
|Move to [line number]
|Replace 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!