How to prevent quitting vim regardless of the reason it quits? - vim

I've read this question, but it doesn't answer the issue I have.
So I have F2 button mapped to save changes. Escape is for leaving insert mode. So to save changes I hit escape and then press F2 (usually several times - to make sure I really hit it :) ). And I can't even figure out how this happens, that I hit some button (probably Escape) at some moment, that it closes vim with all my open tabs and buffers, as everything is usually saved. This happened to me so many times, that I finally decided to post a question.
How to prevent exiting Vim, regadless of the way or reason (:q, triple Escape, etc) it's going to quit, so that I couldn't exit from it without confirmation?

To your case: It is strange that triple Escape quits VIM that is not standard behavior. Even :q would not quite all tabs. You can check what is mapped on your Escape:
:verbose map <Esc>
There must be something like qa!.

Related

How to undo pressing Ctrl-U in Insert mode to avoid accidentally losing my work

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

What is the difference between :q and :qa! in Vim?

I'm quite new to Vim so I first checked the help.txt file to inform myself about Vim. Here I saw the following:
Close this window: Use ":q".
Get out of Vim: Use ":qa!" (careful, all changes are lost!).
The first one closes Vim. The second one also. Wouldn't all changes also go with :q? To be clear, I use the vim GUI not a command prompt.
edit: It's more about the difference, not the actual meaning. The almost same explanation in the help.txt file confused me.
The key difference is the exclamation mark here. :q will warn you about unsaved changes and will not let you exit. :q! will not warn you.
See also :help quit (type that in vim)
I don't see any of the answers specifically addressing the meaning of 'a' so thought I'd contribute:
:q is quit, as you know, but warns you didn't save
:qa is quit, all buffers, without saving but you'll get that same warning
:qa! is quit all buffers, without saving, and without a warning
When you have some changes and use :q, it fails and throws an error stating No write since last change. In order to quit from the Vim without saving changes, you should write :q!, it will quit the Vim and ! will work as a negation, which will negate the write operation.
When you fire :qa!, it quits the vim and doesn't throw an error mentioned above as you have added !. And there is no argument like a if you see man vi. (Just to note, arguments are case sensitive and -a and -A are treated differently)
In order to save the file and then quit the vim, you should use :wq, as it will first save the file and then quit the Vim.

How do I cancel a paste operation in Vim?

I had rather a lot of text on my clipboard whenever I accidentally right clicked inside Putty (with Vim open), and Vim has initiated a paste operation which has been going for around ten minutes now.
I don't want to lose my unsaved work, is there a way to instruct Vim to stop pasting text?
If you're in normal mode, Ctrl-C aborts the current command in progress. Then press u to undo anything that changed before you stopped it.
Depending os your vim configuration, there's chances that you have a swap file (backup) in .nameOfTheOpenedFile.swp (substitute nameOfTheOpenedFile with the name for your file).
To recover the file :
vim -r .nameOfTheOpenedFile.swp
I know this is really old but the top answer is not right and I was clearly having a similar issue to OP. (accidentally pasted like a million lines of json into vim)
Keep in mind this may not allow you to save your work (but you can probably salvage something from the .swp file)
All you need to do is open a new terminal window and enter pkill vim into the command line.

Abort keystroke sequence?

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.

Readline's vi-mode in vim ex mode

Let's see if I can explain myself.
I use vi-mode in bash, which is really great since I'm used to Vi.
When I'm inside vim and type : (to go to ex mode), since I'm used to the vi-mode from bash, I feel the slowliness of having to use this mode like the "regular" way of using bash.
Question is: is there a way of using vim's ex-mode like bash's (or readline) vi-mode?
Not sure if I understand what you're trying to do, but it might be something like hitting q: in normal mode?
For users that use Vim or vi bindings almost everywhere, including on their shell command line, it really hurts when you leave that environment. If you're used to the vi bindings hyperdrive, going back to chords for skipping words and other manoeuvres is painful and slow. Operating systems also differ on their default bindings so Mac, for instance, supports option-arrow instead of control-arrow, adding to the pain.
But there is one place where this also happens where it's really upsetting: in Vim itself. When working in Vim and entering command mode using : the default readline editing returns. Chords all over again. How to fix this?
Simple: When in "normal" mode, that is, when navigating around, type q:
Vim will drop you at the bottom of a full Vim full screen editing experience, go for your life
Additionally the command history is available on previous lines in the buffer
You can yank and paste lines and edit the commands as much as you wish
To execute a command in "command" or "ex" mode just hit ENTER on the line you want to execute
Hitting enter on an empty line closes the buffer and does nothing
But this is just another buffer so you can quit it as usual with :q as well
Although ESC leaves the "ex" command line, ESC in the buffer will not leave the buffer, because it's an actual buffer
The q prefix is used to introduce macro recording, so the q: variant is perfectly mnemonic for entering recording of an "ex" command line.
Note that q: to enter the buffer editing mode is very similar to :q ! You may have hit that by accident sometimes ;-) Now you know how to get out of it!
Zigdon had this answer a long time ago, of course, but it's pretty darn sparse, but then again, so is the question. If Zigdon adds this extra detail to his answer I'll be happy to delete this answer so that there can be one good answer.

Resources