When using Vim I sometimes find myself midway through entering a key combination and change my mind or realise it's incorrect (I'm still learning).
For example I may have typed d, 4 and be about to press d in order to delete 4 lines and realise this is not what I want to do. How can I abort the current sequence of keystrokes?
Press ESC.
(no more text)
Ctrl+C also aborts the current command.
I prefer CTRL+[ over escape at all times, including aborting the current command. It's quicker and easier to type, particularly if you remap caps lock to ctrl.
Related
If you're like me and often use Ctrl+Shift+U to enter special characters (like Ctrl+Shift+U 2014 to enter —), you may have occasionally accidentally gotten the timing wrong and pressed Ctrl+U+Shift instead (pressing the U while holding Ctrl but just before pressing Shift).
(For me, it's even easier to fat finger since I usually press all 3 of those keys with my left hand (I use Dvorak) so that I can leave my right hand on their home keys.)
But if you make this mistake while in Insert mode, you can inadvertently and permanently lose an entire line of input—without any way to get it back. Wait, wat!?
(Ctrl+U "Deletes all entered characters before the cursor in the current line." See :help i_CTRL-U or this question)
Pressing Esc U does not undo this deletion, like it would if I had deleted the line with dd. (Even pressing Ctrl+O U without leaving insert mode does not work).
Needless to say, if you've just spent several minutes or an hour composing a really long paragraph (which will be considered a single line if you haven't inserted any line breaks) without leaving Insert mode, it can be very disheartening and disconcerting to vainly press u, expecting to get your line back but having it instead revert back to an even earlier undo state! The confidence you've placed in your trusty editor, vim, suddenly gets dashed to pieces as you dolefully realize that you now have to try to remember what you just wrote and compose it all over again.
I can think of a few workarounds to try to make editing safer, like trying to remember to exit insert mode frequently so that a new undo block is created (see :help undo-blocks)...
But I shouldn't have to tread so carefully when composing something. I should be able to stay in the flow (that is, keep composing without leaving Insert mode) as long as the words keep flowing. (And it's probably safe to say that most editors don't have this problem. In most editors, Undo would undo your changes one character at a time since they don't have any way to group multiple characters together as a single "insert").
Since I probably won't remember to be so careful, I ended up simply disabling use of Ctrl-U altogether in Insert mode, for now, with this:
imap <C-U> <Nop>
But what I'd like to know is if there's any decent way to make Ctrl-U still work in case I ever want to use it, but create an undo state so that this deletion can be easily undone with U. Is this possible? (And why isn't this the default behavior?)
Sorry for a noob question, but i find it struggling to just put a ";" at the end of line after writing a function. For example, I am coding in C and many time i need to write things like:
f(a);
what i usually type is (from normal mode, using bracket autopair-like feature):
if(a<ESC><SHIFT-a>;
and it need changing mode twice! Comparing to normal editor (sublime):
f(a<right>;
does anyone have more efficient way do do those typing? thanks for any help.
I think you have some "auto-close" plugin installed.
I have that kind of plugin too, and I don't press arrow keys either, since I don't have them on my keyboard. I have this:
" moving cursor out of (right of ) autoClosed brackets
inoremap <c-l> <esc>%%a
So with your example: it would be (assume already in INSERT mode)
f(a<ctrl-l>;
Thus, your fingers never leave the home row.
If you're a vim user, you can hit Shift-a.
Shift-a takes you from normal mode to insert mode, and starts your cursor at the end of the line.
(If you want to be an efficient vim user, you should remap esc to something like caps-lock.)
Comparing to normal editor (sublime):
f(a<right>;
Well… that's exactly how you would do it in Vim if you use Delimitmate or some other "autoclosing" plugin. Why do you insist on making things more complicated than they are?
When I'm in VIM insert mode, it wonderfully adds matching end characters. E.g. if I type " it will add another " immediately after the cursor. Similarly for parenthesis, braces etc. when programming.
How can I quickly skip over the inserted character, while staying in insert mode? The best I've found is to use the forward arrow key, but that's not conveniently located.
Accordingly, I either type the closing character, or I <esc>li (exit insert mode, move right one character, re-enter insert mode). This reduces the convenience of the auto-insertion quite dramatically, so I figure I'm missing something obvious.
(Note, for convenience I'm using the handy SPF13 curated collection of plugins and running MacVIM. Edit: This is the autoclose script providing the matching.)
There's basically no way to get out of an autoclosed pair that doesn't involve pressing at least one key.
The standard mechanism provided by all the autoclosing plugins is simple: type the closing character. You can also press <Right> or, if you are at the end of the line, <End>.
Maybe your plugin gives you another mechanism but you'll have to find out for yourself.
Whatever key you press, you'll still do at the very least exactly the same amount of typing as you'd do without autoclosing.
Autoclosing is not about saving typing, the only practical use of that feature is to prevent unmatched pairs. That's all and, I think, the "obvious" thing you are missing.
As you are using a SPF13 and don't know which plugins brought the mapping. There are two things that we can do
1) I usually esc followed by A. This will kept you in insert mode after the closed character if it is the last character. I usually prefer this over the second one.
2) You can circumvent the automatic closing by ctrl - v before the character, for instance ". This will not autoclose the corresponding character and you are responsible for the closing.
I would like to have vim fill a short sequence of characters into a longer string in insert mode. Example, say I write the word "sub" and it writes "subroutine" as soon as I press space. The problem I find is that when I use :imap sub subroutine, every time I start typing sub, the cursor does not continue moving, but hangs in the same position waiting for more keystrokes in order to decide what to do. I find this behavior annoying, although not wrong (it does what I need).
Is there a way to have vim continue typing single characters, and eventually replace ?
Just try to use abbreviate :ab sub subroutine
Search the web for "vim completion" (or the help for "completion"). The feature is in v7.x, but you may want to change the keystrokes with ìmap.
I've been trying out vim, and the emphasis on speed and accessibility makes sense for the veteran programmer. I've previously used emacs, and currently use a combination of nano, and stuff like gedit or geany.
I'm confused by the need to constantly switch modes, and that returning from insert mode to normal mode requires leaving the home row to press Esc. I've read that previously, this key was Tab on some systems where vim was first used, which makes a lot more sense, but not on current systems where tab is expected to participate in smart indenting when coding.
Returning to normal mode is an operation that you need to perform all the time. Is there some alternative key mapping that makes this quicker, or mappings that I simply don't know about that do this?
Ctrl+C also return you to normal mode
You can also use ^[. If you've mapped your caps lock key to control (highly recommended), this becomes a rather easy keystroke.
I personally use the Capslock key as the second Esc key, so it is very comfortable. You can do it by adding the following lines to your .xmodmap file:
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
You can map whatever you like to escape:
imap kj <Esc>
I haven't yet run across a situation where I need to type kj next to each other, besides when I talk about this mapping. I've seen other people use jk or jj, but kj works best for me.
I personally use ii to get out of insert mode while staying on the home row, but it is only a matter of personal preference:
inoremap ii <Esc>
i to go in insert and ii to go out. This is easy for your finger to remember. (I tend to still use <Esc> though)
I personally use Left Control as escape and Caps Lock as Left Control. It is not vim mapping, but some xmodmap+setxkbmap magic:
! ~/.Xmodmap
! Control_R
keycode 37 = Escape
and ctrl:nocaps in X keyboard options. You may have different keycode though.
Use Alt/Meta In a Terminal
If you use Vim in a terminal, simply press alt/meta+normal_mode_key. Most terminals send an escape character followed by the normal_mode_key that you pressed, removing the need to press escape yourself.
The terminals konsole and gnome terminal send the escape by default when you press alt/meta+normal_mode_key. For Xterm you can ctrl+click and select the option "Meta sends escape" or "Alt sends escape".
Thus in insert mode pressing alt+h alt+j alt+k alt+l all take you to normal mode and move in the expected direction. You can hold down alt when moving even while in normal mode since the additional ESC that is sent does no harm.
The advantage of using this scheme is that you can use the alt/meta+key combination with any normal mode key, e.g.
Alt+o opens a new line below the one you are currently editing,
Alt+A appends to the end of the current line,
Alt+p pastes at the current insert location.
Alt+k moves up
Thus using vim via a terminal gives you these short cut powers on any stock standard system without the need to edit each systems vim mappings.