I'm gonna organize mechanisms that I've understood using vim. Might be useful for those who're familiar with keyboard shortcuts but struggling with complicated actions so selecting with Visual Mode.
First, I think Visual Mode(v) isn't mainstream. Sometimes we need this, but it blocks ways to efficiency while this can replace almost every function. I think vim's efficiency comes from the impossibility of being inefficient. So I will exclude Visual Mode from explanations after this.
Shortcuts
When starting to learn, it seems easy to memorize if you think there's arrow keys and shortcuts in vim, but actually it is not. All actions in vim are shortcuts, and these shortcuts can be linked1 by their function.
First let me explain about all things are shortcuts. HJKL
for move are also shortcuts. Whether these are shortcuts or not is important because these can be used in combinations.
Link
The structure is as follows. Can be omitted depending on the type.
[REGISTER][REPETITION][COMMAND][SCOPE]
Register
(Can be omitted.)
Vim can store information somewhere. We can copy without this, but information will be stored at temporary register. Vim's all deletes includes copying. There's no just deletes. All cuts. So if you delete something between copy and paste, copied text will be overwritten.
So we need register. Starts with "
(double quotation mark), choose where to store after. This 'where' can be any letter of the aphabet. Seems like case-insensitive. Just to add, there's +
register, clipboard. This can be used when you copy for external use or bring in from outside.
"+
Would be like this if you use clipboard.
Repetition
(Can be omitted.)
Decides how many times to repeat.
But copied results are merged. For example, 2yy
copies two lines. It won't overwrite while repeating.
If you repeat input type, multiple texts will be typed simultaneously like writing on paper with multiple pens in a hand.
Command
What to do. Just one letter. Shift or ctrl can be used together with but there's only one letter.
Wait, but in other tutorial they teach me something like dd
and dw
? That latter part belongs to the scope right after.
So there's no different behavior that have the same first letter. As far as I know. So you can consider only the first letter as the body of the command.
Scope
Here's the point. Scope is decided by move commands. Do you remember that HJKL
are also commands? They are used here.
Typical move commands are HJKL
, w
, b
, b
, f
. Combination with ctrl can't be used here as I just checked.
Importantly, this can be repeated.
I'll give you some examples. I'll show you from beginning to avoid confusion.
y2w
: Copy two words.dl
: Cut letter at cursor.d5k
: Cut 6 lines including recent line.c3Fa
: Input after cut up to the 3rd nearest letter 'a' in the line, excluding the letter at the current cursor.
The last one is kind of bizarre, though it seems to start before the cursor when moving backwards in a line. So it's little bit confusing.
You can select the entire line by double tapping the same command. I'm not sure which comes first, inability to select only one line with JK
or the opposite case. So the 5k
above selects 6 lines. Because cursor goes up 5 times.
All At Once
Let's do all at once as complex as possible.
"a3d3fb
This would be "Repeat a command that cut up to 3rd 'b' after cursor to register a 3 times". But you don't need to do like this. Just do "ad6fb
.
Command-line Mode
You can change to the command-line mode by using :
(colon) to do some complex things. There's plenty of commands but I'm not going to deal with it today. I will talk about use shortcuts in command line.
Yes. You can use shortcuts as is in the command line. Except the move commands. You can tell just by looking at the save command w
.
If keys are same, is there a reason to use command line? In the command-line mode, you can use line number.
You can use like this.
Scope
It can be not used depending on which command is.
[START LINE NUMBER],[END LINE NUMBER]
It would be like this.
You can use some symbols instead of numbers.
%
: Whole lines in the file.
: The line where cursor is./[PHRASE]/
: The line where[PHRASE]
is.
You can use something like this. Also you can do plus and minus calculations. For example .+3
refers the 3rd line down from the line where cursor is. This can be used with //
. But you must specify both start and end. This can't mean range by its own.
Command
Usually a letter. Same as the shortcuts. There's commands not in the shortcuts, so look them up.
Input
If the command needs parameters, write. For example a s
command that replaces strings, put the string to find and replace.
Register
You can type the register where to store when copying. Except that you don't use "
(double quotation mark). Instead, you have to place space.
All At Once
:12,/target/-3y +
This will be "Copy from the 12th line to 3rd line above from the line includes 'target' to clipboard register".
End
You won't use Visual Mode even just knowing this much. Sometimes it comes out habitually but there's no inevitable situation for me. Can use more complex commands and macros as you go deeper, I don't know further so I'll wrap it up here.