Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't '2>' mean indent 2 levels on vim?

Tags:

vim

I've been using vim for sometime now but only recently started learning how to really use it. I have some questions about what seems like puzzling behavior.

Why 2> doesn't mean indent 2 levels instead of indent 2 lines? >2j already serves the same purpose and indenting 2 levels seems like the more intuitive behavior.

Another similar question is why does 'G' means jump to last line but '10G' mean jump to 10th line from the top of the buffer instead of from the bottom of the buffer.

How can these issues be fixed properly?

Should I? Could changing the behavior break plugins or anything?


Note: I'd like to mention that I know how to get the functionality I want, ie. :>> for multi-level indent and :$-10 for jump to n'th line for bottom.

like image 762
qtwtetrt Avatar asked Dec 08 '22 14:12

qtwtetrt


1 Answers

2>>

The behavior you describe is due to how {count} works with line-wise commands: it doesn't perform the command {count} times but considers {count} as the number of lines on which to act.

You can get a glimpse at how Vim does its thing by doing 2:, this is what you should get in the command-line:

:.,.+1

Doing 2>> is thus the equivalent of:

:.,.+1>

"Indent this line and the one below by one level".

In normal mode, just hit >> to indent the current line by one level and repeat until you are at the right level.

If you want 2> to indent by two levels, you must first visually select the line: V2> or v2>.

If you want to "fix" the indentation of the current line so that it matches the indentation of the surrounding lines (the most common scenario for such a task), hit ==.

10G

G expects a {count} to jump to the corresponding line. Without a {count} it defaults to jumping to the last line: you can see it as a convenient shortcut for {lines in buffer}G.

So you are inverting things a little: G is not "jump to last line", it's "jump to line {count} but without a {count} so let's go the last one".

-- Startedit --

Let's reverse-engineer G for fun:

  1. G is first conceived as the normal mode equivalent of :number<CR>.

  2. It is decided that, instead of doing G23, the {count} mechanism is used so we have 23G. G is not an operator: it doesn't wait for a target.

  3. But what do we do with G, without a {count}?

  4. Generally, a buffer has two "special" lines: the first and the last. Maybe we can decide what the default destination for G is? Why not? We hate wasted keys!

  5. G is not much shorter than 1G but it's shorter so that could be a good idea to make G jump to the first line.

  6. But what about the last line? 12G is OK when we know there are 12 lines in the buffer but what about 1257 lines? What about an unknown number of lines? Is it that efficient to look up the number and type 2431G? No, at that point, we might as well do :$<CR>. Well, why don't we use G, here? It is vastly shorter and quicker than any other way to reach the last line.

(this is pure speculation, of course, I have no idea what Bill Joy had in mind when he designed G.)

-- Endedit --

Fixing Vim

Generally, Vim doesn't need to be "fixed". Instead, you must learn how it works and how to leverage its power. Most of it makes a lot of sense: when you'll "get it" the reward will be huge.

Also, the behavior of everything is explained in :help: learning how to use it is the number one skill you must grow if you are serious about using Vim.

like image 134
romainl Avatar answered Dec 15 '22 00:12

romainl