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…).
Related
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.
I find myself in this situation quite often in vim:
response.status == 404
^
My cursor is on the r character and I want to delete everything after the current WORD. I can do ElD to move to the end of the word, shift the cursor, delete until EOL. Alternatively I could do something like WDx to move to the next WORD, delete the whole line, and delete the extra space.
Additionally it would be nice to keep my cursor in relatively the same position as I started (I guess I could use marks for that)
Are there any shortcuts that might make this easier (e.g. less capitals, less strokes)? I'm also just trying to not use h, l, and x as much. I could add something custom, but I'd prefer to use the native keybinds
Vim golfing
Now it is common practice in Vim to try and reduce keystrokes for certain tasks, commonly referred to as golfing. Often this practice uses the following general techniques:
Use lesser used motions or commands. e.g. ZZ, ]), gi
Use an operator and motion instead of a count. 2dd vs dj
Using counts to do more at a time. e.g. 4j
Use repeatable motions. e.g. fX and use ,/; to repeat the f motion
Reuse patterns via //
Take advantage of given text. This often fails with more generalized solutions.
Use ex-commands with clever ranges. e.g. v/./,//-j
Use marks, but especially use marks that are vim set for you. e.g '`, '^.
Premature optimization is the root of all evil
Now Vim golfing is a "good thing" as it typically helps the vimmer to find new motions or techniques, but it only so helpful in the average day-to-day workflow as golfing usually require a decent amount of forethought. One cannot worry about every single keystroke for every task all-day every day (aka premature optimization).
Instead focus on optimizing the common or lengthy tasks. Do this by creating a custom mapping or command to make such task quick and painless as possible and get on with your day.
Typically I use WDD for that purpose, which will avoid pressing a third key.
Obviously, you lose the relevant part of the deleted code, so you can't use this for cut&paste.
I don't know if you'd want this, but - if you do it, this use-case becomes easier, and uses only 'native' keybindings. So - you can add, for your desired FileType, this:
:setlocal iskeyword+=.
Then, you can use elD instead of ElD, (easier on the wrist).
But, frankly, if it bothers you enough to ask about it, it means it happens often enough that it should really be accessible from an even easier/shorter, custom key binding ;)
In an answer to this question, I learned that all empty lines around the cursor can be deleted using the key sequence dip in normal mode. Astonished, I looked into the vim help, but the vim help only says that d may be followed by a movement, which clearly i isn't (it brings you to insert mode normally).
What exactly is dip doing? What does i and p stand for in this context?
Where can I find it documented? What other "magic" characters can follow d?
Think of dip as "d-elete" "i-nner p-aragraph", where "inner paragraph" is a special kind of motion called "Text object selection".
There are many, many "motions" in Vim, which you can learn about by reading :help motion or (as I did many moons ago) by using vimtutor (and using Vim every day).
You use Vim as you'd think, speak or write with a normal spoken language.
You have verbs like d[elete], y[ank], p[ut], c[hange], v[isually select] and so on.
You have all sorts of objects like w[ord], p[aragraph], t[ag] and so on.
You have modifiers like i[nner], a[round], t[ill] or /foo and many others and the ability to use a {count} as quantifier.
dip means "delete inner paragraph". vat means "[visually ]select the current HTML tag", c2t; means "change from here to the second next semicolon"…
Like with a real language, learning full sentences won't get you very far. The basic vocabulary is not that large so it is a good idea to forget about dip and learn about d, i and p instead. Learning dip, ciB, gU/foo and their millions of friends as single commands takes more time and brain cells than learning the individual commands and the simple grammar that make it all work.
Once you know dcyv (verbs), ia (modifiers) and p (object), learning a new object like ) gives you instantly di), da), ci), ca), vi), va) and so on. Each time you learn a new object or modifier (most often a single character), your vocabulary increases dramatically. Isn't that freaking cool?
Also, :help d and :help ip would have given you your answer.
As I like to say, ":help motion.txt will blow your mind".
Generally, "d" can be combined with characters that move the cursor, with the result of deleting to between the point and the destination.
In this case the "magic" is that 'ip' stands for "inner paragraph". As far as I can tell, it means "apply this to the paragraph you're in". (but it's not one that I've really come across before, sad to say)
The vim documentation is spotty, but this might be useful:
ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
This is possibly not a good question for SO, but it's been bugging me for years, and Google didn't know, so let's give it a shot, as it does affect my programming work on weekly basis:
I often find myself in situation where one char is missing, like "=" instead of "==", a missing space, surrounding something with quotes/brackets, etc.
So, why doesn't vim have a proper command to insert a single character? By proper I mean, supports count and repeating with ..
What is the rationale, and what is the correct usage pattern that I am missing, which makes this feature unnecessary? I seem to need the all the time, so there must be some reason it has not been added to original vi already.
I know adding a simple basic keybinding like :nmap <Space> i_<Esc>r is easy enough, but when doing just a quick edit in a new environment, that's rather inconvenient, and this simple version does not work quite properly anyway.
PS. If there in fact is a default binding to insert just one char with total two keystrokes and remain in command mode, similar to r to replace one char with two keystrokes, I promise a bounty of 100 to the first answer which tells me what it is.
To me, i <Char> Esc (3 keystrokes without modifier keys) is pretty short and built-in. You've already discovered custom mappings that reduce that to two keystrokes; I also started with yours and over time made it more advanced to suit my needs, and added mapping variants to insert a single space at or after the cursor position.
Presumably, there's no built-in command because the key space (especially for single unshifted keys) is very limited, this one doesn't justify such prime space, and any multi-key alternative (like the Vim ones that start with g) would be worthless in terms of efficiency.
No. There's no default keybinding for that (do :viusage for a complete list of normal mode commands).
If you want to know why, you'll have to ask Bram Moolenaar or Bill Joy, I'm afraid.
But here is an idea: r and s work on the character under the cursor. What they do is fairly limited and one dimensional but how would your command work?
Would it work like i, inserting that single character before the current character or would it work like a, inserting that single character after the current character?
Because "inserting text" can happen before or after the current character, we have i and a and, rather obviously, we need two commands for quickly inserting a single character.
Which makes the problem a little more complicated.
What keys should we use since all the alphabetical keys are already taken? <C-something>? <C-i> is taken, and <C-a> is also taken. <C-S-i> and <C-S-a> are both non-practical and not guaranteed to work everywhere so what? <M-something>? It won't work everywhere as well. Maybe a two-characters mapping? But which one and following what mnemonic logic?
I also struggled with this question and I found somewhere on the internet (I can't remember where) someone who did this:
:nnoremap s :exec "normal i".nr2char(getchar())."\e"<CR>
It is not perfect because it doesn't support count but it can be repeated with . I use that now so maybe other users will be satisfied with that too.
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.