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:
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.
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#
Keypress | Purpose |
---|---|
i | Enter insert mode where you are. You will start typing wherever your cursor is. |
a | Enter insert mode directly after the curser. |
o | Enter insert mode on a new line below the current location of your curser. |
I | Enter insert mode at the beginning of the line your curser is currently on. |
A | Enter insert mode at the end of the line your curser is currently on. |
esc | Escape insert mode. |
x | Delete the character your curser is on. |
r | Replace the character your curser is on with whatever letter or special character you type next. |
dd | Delete current line. |
y | Yank (i.e. copy) current line. |
p | Paste 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#
Keypress | Purpose |
---|---|
:w | Save your changes. |
:wq | Save your changes and exit the file. |
:q | Exit 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). |
Navigating in Vim#
Keypress | Purpose |
---|---|
k | Move up |
j | Move down |
h | Move left |
l | Move right |
w | Move to the start of the next word |
e | Move to the end of the next word |
b | Move to the start of the previous word |
Extras#
Keypress | Purpose |
---|---|
/[keyword] | Search for [keyword]. Press n to move to the next instance of [keyword]. |
:[line number] | Move to [line number] |
:%s/[x]/[y]/g | 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!