&.
nothing beautiful can last
vim, the text editor, is unintuitive by design. I think this is not a bad thing, and that intuitivity should not be put on a pedestal in the pantheon of design goals. Intuitivity is merely one goal among all the goals you could aim for. Where Visual Studio Code or Notepad++ aim to be understandable without having to read about them, vim aims to give the user total control over how they perform text editing, given that they know how to make it do what they want.
This is a really great goal, and it's what caused me to want to try it a few years back. It is also the reason I continue to use it nowadays, constantly looking for ways to introduce it even into casual tasks like my homework. It is also a goal that makes it ridiculously hard to use for basic tasks without doing a lot of research, which is why people write a lot of guides for how to use it. I am not the first one to do that, and I won't be the last either, but I felt like writing down how I, personally, perform text editing. While also helping people catch up on it.
This guide is also written from the way that I've grown to understand the program. I don't know how common or uncommon my "vim philosophies" are, but if you think this guide is weird or find yourself unable to find anything on the internet resembling whatever I talk about here, that is probably why. I'm not going off the terminology in the vim tutor or the vim help (I've actually never used the vim tutor!).
When you first open Vim, you start in normal mode. Normal mode is where a lot of the magic will happen, but it is not where you add text to your file. To enter insert mode and start adding text, simply press the letter a
. Vim commands are case-sensitive, a
does something different than A
.
Once in insert mode, you will be able to type anything you want. To return to normal mode, press Escape.
To save, type :w
while in normal mode and press enter. To quit vim, do :q
and press enter. To write and then quit, do :wq<Enter>
, and to quit and discard changes, do :q!<Enter>
By default, it is possible to move using the arrow keys, but vim has a better (although slightly weird to get used to) way of doing it: the keys h, j, k and l. h goes left, l goes right, j goes down, and k goes up. The reason this is better than moving with arrow keys is that you don't have to move your hands away from the typing zone, you can just move to where you want and continue typing.
If you write a long enough text without adding a newline, you will notice that K will skip what looks like a lot of lines. This is a fixable issue, and I will explain how it's fixed after I'm done with the basics.
There are more than one ways to go into insert mode. The four important ones are a i A I o O
.
a
and i
are similar. You will notice that, while on insert mode, your pointer isn't to the right or left of a letter, it's on a letter. This is what defines the difference between a
and i
. The former starts typing after the current pointer, and the latter starts typing before.
This also leads us into their uppercase versions. They can be thought of as extreme versions of these. A
takes you to the beginning of the line before entering insert mode, and I
takes you to the end of the line before entering insert mode.
You may notice the same issue as j
and k
: vim's definition of a line is not our visual interpretation of a line. I will explain how I fix it after I'm done with the basics.
Sometimes you just make a single-character typo. To fix it, you have to go to the character you typoed, then press a<Backspace>
and then the new character, and then go out of insert mode. This process is short, but it can be annoying. Vim makes it slightly less annoying with r
. It replaces the character under the pointer with the next character you press, and still leaves you in normal mode.
If you want to do this for several characters in a row, you can press R
, and then return to normal mode with <Esc>
.
Most vim commands let you type a number before you start in order to repeat the action several times. if you press 10k
you will go ten lines up.
Vim also includes the .
action, which will repeat whatever text editing you did last.
They say the important part is the journey and not the destination. Well you don't know anything about that, because you're about to regularly releport. Vim has a few ways of making moving in text a lot faster, so you don't have to press j or k or whatever for hours just to get to that one typo you made.
You can press 0
to move to the beginning of a line and $
to move to its end.
To me, the most important way to move around is f
inding a character and F
INDING a character. f
will take you to the next occurence of whatever character you press next, while F
will take you to the last occurence of that character.
t
does something similar. It takes you to the place right before a character. T
is the same, but it goes backwards.
To find a string of characters in vim, you press /
, then type what you want to find, then press enter. This is not case sensitive, but that can be changed.
You can also move to the beginning of the next word by pressing w
, or to its end by pressing e
. Or to the last word by pressing b
.
To perform this one, you're gonna need to press :set number
in normal mode and then press enter. This will toggle a display that shows your current line. Don't worry, this setting can be made permanent too.
Having done that, you can move to a line simply by typing that line number and then pressing gg
. For example, 12gg
takes you to line 12. If you don't give it a number, it takes you to the beginning of the file.
You can move to the last line by pressing G
.
When you're on a closed parenthesis (or a brace of any kind), you can press %
to move to its corresponding one. For example, %
on a closing curly bracket will take you back to the curly bracket that opens it.
If you're programming, you can go to the beginning of the code block you're in by pressing {[
, and to its end by pressing }]
.
Sometimes you want to get rid of a line. You type it, you look at it, you cringe harder than you thought was humanly possible, and then you delete the line. Vim makes the last step easier for you. The rest are gonna take more work though.
Simply go to exit mode and press dd
. This will delete the line. If you give it a number, you'll delete that many lines. 10dd
deletes ten lines.
Remember how w
takes you to the next word? Well, dw
delets everything up to the next word.
You can also do daw
which deletes the word and the space before it, or diw
. which does not delete that space.
You can change a line by pressing cc
, which will delete the line and then take you to insert mode.
You can do something like dw
with c
. In fact, all word-variants of d
work the same here.
In fact, if it moves you somewhere, you can put it after c
or d
. d0
should delete everything from where you are up to the beginning of the line.
To copy a line of text in vim, you press yy
. To copy 10 lines, you do 10yy
. To copy from where you are to the end of the line, you do y$
. In general, everything that repeats twice can take everything that moves you somewhere.
To paste it, you press p
. You can paste several times by giving it a number.
Remember all that text you were removing with d
? You were actually cutting it. Go ahead, press ddp
.
Sometimes you want to copy something and paste it somewhere else, or paste something you didn't copy from vim. Vim uses a separate clipboard from that of the system, so in order to paste from the system you must type "+
or "*
before the pasting/cutting/copying command.
I said vim's thing is that you can change how you do text editing. Here, all I have done is telling you how text editing should be done in vim. Now, I will tell you how you can change it. It's all in the .vimrc
file. Where it is located varies depending on the vim variant you have installed and your operating system, so all I know is that you can get to it by typing :e $MYVIMRC
and pressing enter.
In .vimrc, you can type all the commadnds that start with :
without the :
, and they will be executed every time you start vim. It's a really cool way of doing user configuration.
To make line numbers a permanent settings, just add a line that says set number
to your .vimrc and save it.
To fix these issues, you actually have to rebind some keys. I'll first show what you have to add to your vimrc, and then I'll explain what it does.
map $ g$
map 0 g0
map A $a
map I 0i
noremap j gj
noremap k gk
Firstly, the map [X] [Y]
command makes it so that when you type [X]
, it actually interpets it as you typing [Y]
. This is a very powerful feature.
Secondly, g$
takes us to the end of a graphical line, which is where a line wraps around for us to be able to see it. g0
is similar.
$a
simply moves us to the end of the line and presses a
. Normally, it's identicall to A
, however, since $
is actually g$
, it actually runs as g$a
. It's a similar case for 0i
.
nnoremap
makes it so that our bindings only affect the keys we actually press, and not the keys that are called by custom mappings. if the first two maps were noremaps, the third and fourth maps would not work the way we want them to.
Work In Progress, this guide isn't finished