What can follow "d" in normal mode? - vim

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

Related

Is this Dvorak for programmers Vim cheat sheet correct?

I am a long-term user of Dvorak for programmers layout and do not want to change this habit. And I have a serious interest in learning vim, as soon as possible. I have found a cheat sheet which seems very handy for printing it as a set of keyboard stickers compatible with my layout. This is the greatest option because it does not require me to have some saved document or printed picture apart from a laptop with the editor being run in full-screen. Here is the image:
But before making the stickers, I want to clarify if the picture is correct. The cheat sheet seems to have some mistakes, for example:
There is no #2 note on any key. As far as I understand the wording
of note #2, this note has to be on the next buttons: p, w, i, j. For
example, d2w command means delete 2 words where w can
have some number of repeatings.
Some numbers claimed to have some options, I agree with 0 as the
beginning of the line but I do not understand how 5,3,1,2,4 became
loaded with commands.
One thing you might consider useful to understand, digits on keyboard using Dvorak for programmers without a numpad can be typed only with Shift or CapsLock, sometimes (in heavy GUI applications with lots of hotkeys) this might make some hotkeys with digits unreachable or altered. But maybe this image just has not been reviewed enough times to see the mistake, I am too beginner in Vim to do it myself.
This picture is easy to find on Google, but the layout is far from popular, despite it being really superior in some cases. I wish this cheat sheet to be correct because I believe I am not the only Vim user with this layout. Please report any mistakes that you see.
I am not a Dvorak user so I can't comment on that aspect of your question. But note #2 applies to every motion/operator/command so I'm not sure how useful or practical it would be to put it on almost every key. FWIW, d2w doesn't "delete 2 words": cheat sheets are not substitutes to proper learning.
Other issues with that thing:
"WORDS" and "words" are not differentiated correctly.
$ shouldn't have a dot after it as it doesn't "need a char argument afterwards", same with %, [, {, } and plenty of others.
"Goto match" doesn't accurately describe what % does.
Funnily enough, F should have a dot but it doesn't. Same for T.
A shouldn't have a dot and it should be red, like a.
Same story for S.
Q shouldn't have a dot.
B and b are not described correctly.
m should have a dot.
I would recommend you toss that cursed cheat sheet to the bin.

VIM hjkl navigation keys, aganist existing muscle memory of arrow keys

I recently migrated to Vim.
But I am having diffuculty making use of "hjkl" keys for navigating because of my existing muscle memory of arrow keys.
1. Is there a way to effectively use other keys like "ijkl" ( near
to Arrow key layout ) without causing much conflict to other
functionality in VIM
, For ex: http://vim.wikia.com/wiki/Use_ijkl_to_move_the_cursor_and_h_to_insert
and http://ergoemacs.org/misc/on_vi_keybinding.html
2. Is it worth the efforts to retrain the muscle memory with "hjkl" ? But the problem is, I have to use arrow keys again with
Web-Browser, Outlook, and word processors ( So,the shift is causing the conflict) .
Can you suggest ( More interested to know about #2) , How did you deal with this ?
I have been using Vim for five years (after many more years of computer usage) without ever doing any conscious or unconscious effort to use HJKL for navigation.
I don't touch-type so the "home row" means nothing to me.
Even if I did, jkl; would make a lot more sense anyway.
I hit the arrows with the side of my right hand so there's not much cost in "reaching for the arrows" for me.
hjkl are just as bad as the arrows because both only move character-by-character and line-by-line unless you use modifiers (numbers for hjkl and Ctrl/Shift/Alt/Cmd for the arrows). Vim has vastly better means of transportation than either hjkl or the arrows and that's what you should spend time learning:
wW jump to next word/WORD
bB jump to beginning of word/WORD
eE jump to end of word/WORD
fx jump to next x on the line
tx jump to character just before next x on the line
Fx jump to previous x on the line
Tx jump to character just before previous x on the line
/foo<CR> jump to next foo
?bar<CR> jump to previous bar
} jump to next blank line
and so on… :h navigation will blow your mind.
So my answer is: don't bother forcing yourself to use hjkl, it's not worth the hassle.
It's a good idea to train your hand for hjkl. You could try playing rogue for a while on a daily basis until it's comfortable. It comes pretty quick.
Arrows are awful for taking your hand so far out of position. Even <Enter> is too far. I usually get away with Ctrl-J instead (a comfortable Ctrl is really important too!).
Using custom overrides for hjkl will only "help" your vim, but not other vim-like tools. In vim, remapping i will cause other issues, like what to use instead of i/I, which are very common. And you might not want to take over ; for this since right-pinky is prime real estate which I (and I think many others) map to the very frequent : (at the OS level so as not to ever be confused).
Learning hjkl will also help when you're in other places: You probably want to start using vi-mode in bash (or hopefully zsh). Gmail/mutt will be more natural. REPLs with readline support also support vi-mode. Maybe you'll do less word processing if you can replace it with text editing in vim.
Sometimes it's useful to remap things, but it's rare and often to more obscure keys/sequences. It can cause problems when you work in multiple environments, and can make sharing/pairing difficult. It can break macros and other mappings, and make recipes and articles harder to interpret. Try learning the vim-native ways first, and then deviate with experience when really justified.

Delete everything AFTER the cursor (or delete everything after the current word)

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 ;)

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.

What is correct usage to go around vim missing "insert one char" command?

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.

Resources