Vim: How do you know what a key stroke does in vim? - vim

Is there a way to get Vim to tell you what it did so you can look things up?
For example, I am trying to get better at navigating with vim and I noticed that when I press j I go down a row, but if I press shift+j it removes the line break at the end of the current line (or something like that).
shift+k however has no such behavior and shift+h seems to take me to the top of the current window while shift+l takes me to the bottom.
What I'm looking for is some way to understand what these movements are called so that I might be able to understand how to configure them / learn more about their behavior.
Thanks!

What I'm looking for is some way to understand what these movements are called so that I might be able to understand how to configure them / learn more about their behavior.
You are attacking the problem from the wrong end.
Learn Vim instead of trying random stuff:
If you didn't already, do $ vimtutor as many times as needed to get the basics right.
As instructed at the end of vimtutor, level up to the user manual :help user-manual. It's a hands-on tutorial that will guide you progressively through every feature, from basic to advanced. This is not a novel, go at your own pace and, most importantly, experiment along the way.
Keep an eye on anti-patterns and inefficient actions, find improvements, practice. Rinse. Repeat.

Related

Why are h,j,k,l recommended over using arrow keys for vim?

I am a vim user (not a beginner not advanced). I use the arrow keys.
I have repeatedly read that it is highly recommended to use the h,j,k,l keys for increased productivity.
I have tried to use them but I can not see how they help in being more productive.
Because if I am to use the h,j,k,l keys to move up/down/right/left I need to press the ESC first to exit the insert mode.
But that defeats the purpose doesn't it? Since the original idea is that you don't need to move your fingers away.
If I am to press ESC I might as well be using the arrow keys and save a keystroke.
Perhaps I am doing something wrong here?
Old terminal keyboards like the ADM-3A terminal didn't have arrow keys.
The keyboard layout looked like this and it already had the arrows for hjkl. (By the way, also note the relation between ~ and Home key)
I think it is mainly for historical reasons that vim still uses it (and many are used to it). Perhaps also because many sysadmins are nostalgic about stuff from the 70/80's, and because they can brag about how they can type faster (like typing is the cause of slow coding...).
The distinguishing feature of vi / Vim is that it's mode-based. That means:
you only stay in insert mode for short bursts of typing
in the other modes (mostly normal mode), you have all key [combinations] available for powerful editing commands (which are highly regular in Vim, and can be combined to great effect)
So, it's crucial that you can switch modes easily. For many users, that means remapping the Esc key to a more convenient location (replacing Caps Lock or Ctrl). Then, using H J K L for navigation (note those are only the basics, and it's often better to use more specific motions like w or f{char}) is quicker, because your fingers can stay on the home row (assuming you're a touch typist, which you should invest in if you spend considerable time with a computer). The cursor keys are located so far off that switching there takes too much time (using the mouse is even worse), and isn't good for your wrists, neither.
The problem with the arrows is not that they are too far: the problem is that they only allow you to move character-by-character and line-by-line. And guess what? That is exactly what hjkl do. The only benefit of hjkl over the arrows is that it saves that slight movement of the arm to and from the arrows. Whether you think that benefit is worth the trouble is your call. In my opinion, it isn't.
hjkl are only marginally better than the arrows while Vim's more advanced motions, bBeEwWfFtT,;/?^$ and so on, offer a huge advantage over the arrows and hjkl.
FWIW, I use the arrows for small movements, in normal and insert mode, and the advanced motions above for larger motions.
mouse-using sucker everyone laughs at: (move)↓↓↓↓↓↓↓↓↓↓→→→→→(move)
hjkl-obsessed hipster: jjjjjjjjjjlllll
efficient vimmer: /fo<CR>
h, j, k, and l are on the home row. If you move your hand to use the arrow keys, you have to readjust when you're done with the arrow keys. This is only a problem for touch typists (which everyone back in the day was).
If you like using the arrow keys, by all means, use them. There is one advantage I see over hjkl: arrow keys work in insert mode. Others prefer to move their fingers only minimally. That's a good argument if you are a touch typist and need to type lots of text. At least for me, my thinking is always slower than my editing, so typing speed and minimal finger movement are not so much an issue to me.
In the end, it's a pretty subjective matter. Use what works for you but try to look around and extend your horizon now and then.
h j k l are a stepping stone to more powerful features. Do you really think that left, right, up, down is as advanced as moving the cursor gets in the 21st Century (or 1976)?
Please note that arrow keys are not recorded while using "q" macros, and bring a lot of sheanigans when you are using some esoteric options because arrows interrupt them. The idea with vim is to end up using composable actions as often as possible and arrows are not.
Besides it doesn't make much of a difference anyway. I had the same strule as a kid switching arrows for WASD, once you get used to it it's just another way, closer to where your hand already is.
And it's not just vim, you would not believe how convenient it is to go the extra mile and learn the terminal shortcuts for movement, copy-paste, history, etc. My laptop's arrows broke last year and I couldn't find the time to solve the issue (Amazon reach the space, but not the island where I live). It was a blessing. Now I rarely use arrows anymore, because ctrl-p/ctrl-n/ctrl-b/ctrl-f/etc. it's just more ergonomic. I just lay my hands on top of the keyboard and don't move them anymore. Ironically now the fartest I move my hands from standard position is to press ESC, wich happens quite often in vim.
A few times I've heard "wait, your terminal/editor can do what!?" when doing fancy things with public. Probably their toolset can do it too, but they haven't go the extra mile of truly learning them. Trust me, it's worthwhile to do so. These tools have been designed this way for a reason.

Is it a vim plugin to jump between found patterns?

Here on the video you can see a feature of jumping between found patterns. The algorithm is really different from vim default behavior.
Using vim you can find some pattern /pattern<CR> and jump to next n or to previous N. The disadvantage is: you should jump many-many-many times to get to proper position or write long pattern to reduce hits amount. Yes, you can apply something like 10n to jump over next 10 hits but it is also inconvenient because you can not count large amount of hits at first glance.
In the video you see a better approach: text editor gives a short name to each hit so you can jump in two or three keystrokes.
I do not believe that nobody implemented the feature as a vim plugin but I failed to find something relative. Is it such plugin anywhere? May be something even better is implemented?
Haha.
The very first implementation of that idea was AFAIK a Vim plugin (PreciseJump), with the second implementation being another Vim plugin (EasyMotion) that got a lot more traction than the first and itself spawned many other implementations in many other contexts (editors, browsers…).

Does Vim scrolling performance degrade with vertically split windows?

I've noticed this with my own set up, but I wanted to get confirmations from other people. Is there a way to deal with this issue?
Probably, because in the presence of folding and needing to check which pane the cursor is in, there is a lot more to figure out.
As for ways do deal it, I make it a habit to use paging instead of scrolling (^F/^B for full page, ^U/^D for half page and z. to center on current line). At least for me, that is plenty fast even with triple vimdiffs open with plenty of folded lines.

Is learning ed worth it to boost my speed in VIM?

I've learned the basic/intermediate levels of VIM (it's too vast to list). I often find that I slip back to my old ways and start using the mouse, holding down keys to get somewhere, and doing other stupid things that could be sped up.
Would it be worth spending time to learn ed to break the habits learned from years in a point and click interface? Does using ed cultivate the right type of thinking that will transfer to VIM?
You need a change in habits which is much deeper than simply dropping the mouse. You need to understand that if you really want to learn Vim you have to do it "the hard way": read and memorize text-objects (:help text-objects) operations and movements and try using arrows/hjkl as little as possible.
At first you will be a lot slower, but don't fall back to normal movement. If you need to select a paragraph, use vap, if you need to change the text inside a pair of double quotes use ci" and so on. When these things will come out of your fingers naturally, you'll realize how worth it was the time spent to get used to them.
I learned Vim and became proficient simply by using Vim itself--a lot. I think the key is practice, practice, practice.
If you want to train yourself to avoid the mouse, unplugging it or putting out of reach might help. Learning a whole different program might be overkill, and the knowledge may not translate directly.
Read this post by Bram Moolenaar on the subject of efficient text-editing.
One of the points is that it takes conscious effort to force yourself into the habit of using Vim efficiently.
So yeah, hide the mouse while you're in Vim. And when you catch yourself doing things "the wrong way", stop and do them again correctly. It won't be long before you've learned the proper habits. And has has been suggested: Text Objects. Learn them. Get in the habit of using these and you're 1/2way to being a Vim pro. Using them is addictive; it won't take long.
I think in addition to consciously avoiding the mouse, it also helps to consciously try to force yourself to keep your fingers on home row.

Inefficiency in Vim

I consider myself somewhat familiar with Vim,
hate the arrow keys (let alone the mouse),
regularly look up tips and plugins to get the most out of this tool,
use it daily to manage my cloud servers, etc.
However, I always find myself doing the same mistakes probably inherited from the GUI-world:
too often switching to visual mode to see what piece of code I'm about to manipulate,
undoing changes to retrieve lost statements because I forget to utilize registers (and pasting code on temporary lines just to grab it after other edits),
relying on Ctrl-C & Ctrl-V when working with operating system's clipboard,
keep pressing j button to browse through lengthy files to find certain functions.
Probably my Hungarian keyboard layout prevents me from being faster as most of the special characters (/, [, etc.) are only available as a key combination (with Shift or Alt Gr).
Given this specific situation, what pieces of advice could you give me? Have you faced similar bad habits when you were a Vim-novice? I'd like to see my productivity skyrocket (who wouldn't?). Thanks in advance.
I've found a simple, effective strategy. Choose one action, one task or one set of keys that you think is unnecessarily slow. Figure out a better way of doing this using the vim manual or googling or a plugin etc. Force yourself to use this every time. Rinse, and repeat. The path to efficiency is one-by-one elimination of the slow parts.
I'd also recommend just reading the vim manual from time to time - even if you don't remember everything, knowing something's out there is very helpful.
This probably applies well beyond vim, but
something that worked for me was finding a specific feature that I knew would
be more efficient and concentrate on using that for a week or two.
Just one feature at a time, and possibly using it excessively.
After a couple of weeks it becomes automatic and you can move on to the
next thing.
I learn programming tricks the same way. eg. I'll have a month of using lambda expressions for everything, then a month of mapping and filtering.
(not on production code though)
Probably my Hungarian keyboard layout prevents me from being faster as most of the special > characters (/, [, etc.) are only available as a key combination (with Shift or Alt Gr).
I'm sitting in front of german keyboards all day long and know this problem very well. Some keyboard layouts are simply not very suited for programming / using vim. I think its safe to assume that most programming languages and keyboard shortcuts were designed with the us-layout in mind.
My advice: reset your keyboard layout to us-english and practive touch-typing on that layout (typing without looking at the keys). It won't matter that the keyboard labels are wrong and you'll be much more comfortable using vim hotkeys.
The only problem that still remains for me is to produce language specific characters (german umlauts such as ä,ö,ü) wich i assume will also be a problem for hungarian. For that I use a combination of vim-digraphs, linux window manager digraph-key and windows layout-switching hotkeys.
just keep using it. The more you use it, the better you become at it. VIM isn't too bad. The main thing is you just have to remember that it isn't always in edit mode.

Resources