What is the difference between :q and :qa! in Vim? - 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.

Related

Hard refresh in vim (not :so %)

Sometimes in vim I'll need to exit the vimrc file and enter back into it to update changes (for example if I'm editing other files related to it). To do this I'll normally do:
:wq
$ vim
ctrl-o ctrl-o " in vim
Would there be a way to do this all within vim? Something like:
:wq | !vim %
Additionally, why does it require me to do ctrl-o two times to go to the previous buffer (it almost seems like the first ctrl-o does nothing)?
Update: Please note that I am aware of doing :so % or :so $MYVIMRC, etc. My question here is how do I basically reset 100% of the things to whatever are in my current files? That is, unset ALL mappings, variables, etc. that may have been updated, removed, etc; update ALL files that may have changed (functions, plugins, colorschemes, etc.). I don't think "Running :so % on 20 files" is a good solution here, which is why my current solution is to close the file and re-open it.
As others have mentioned, you can source your .vimrc, but that doesn't completely reset Vim. If you want to just restart Vim, then you can do so by re-execing it.
Vim doesn't provide a built-in way to exec processes from within it, since typically one doesn't want to replace one's editor with another process, but it is possible to do so with Perl or Ruby, as you see fit:
:perl exec "vim"
or
:ruby exec "vim"
This may or may not work on Windows, and it of course requires that your Vim version have been compiled with support for the appropriate interpreters. Debian provides both in the vim-nox, vim-gtk3, and vim-athena packages, but not in vim or vim-tiny; Ubuntu, last I checked, did not provide Ruby support but did include Perl.
If you want to re-exec with the same buffer, you can use one of these:
:perl exec "vim", $curbuf->Name();
or
:ruby exec "vim", Vim::Buffer.current.name
Note that re-execing may cause your screen to be slightly messed up when exiting, so you may need to use reset to set it back to normal.
I don't know if you had tried this but you can source your vimrc file from vim itself by typing
:so $MYVIMRC
In order to apply the changes, you don't have to exit vim and open it again, no need for a "hard refresh" :)
If you want to apply in on the .vimrc file itself, you can type
:so %
to apply the changes in another file, you can type:
:so ~/.vimrc #path to your .vimrc file
in normal mode, Ctrl-O takes you to where your cursor has been backward and Ctrl-I forward. You can check your jump list by typing :jumps, to clear your jumps :clearjumps
in insert mode, Ctrl-O escapes to normal mode and lets the user to do one normal mode command.

a single vim command for "edit a new file" or "switch to existing buffer"?

Problem:
If the file "tmp.rb" has been modified, the command "e tmp.rb" will fail.
I'm seeking for a command to succeed in both following conditions:
If "tmp.rb" is not in the buffer list, the file will be loaded;
If "tmp.rb" is already in the buffer list and been modified, the
buffer will be loaded(in current window).
Clarification:
Sorry for all the misleadings. Indeed a single command e tmp.rb will achive both as long as the modified "tmp.rb" is not loaded into the current window, in which situation I prefer e tmp.rb does not cause an error and has the same effect as b tmp.rb. Maybe this little concern will make scripting around buffers, windows etc. a little more easily.
The :edit command fails when the buffer is already loaded and modified to prevent you from accidentally overwriting the changes. If you do want to override, use :edit! instead. In many Vim commands, an appended bang (!) forces the command. Alternatively, if you want Vim to ask you for confirmation, use :confirm edit.
I couldn't reproduce your problem.
What I do to try reproduce your problem is
Open tmp.rb with vim, write some thing and save.
In other shell,
$ echo > tmp.rb
Enter :e or e: tmp.rb in vim and it reopen the file without any problem.
If other program is accessing the tmp.rb at the same time this other program maybe are locking the file and because of it you are having the trouble you describe.
The title of your question says "OR" but its body says "AND".
Vim's commands are usually very precise, it's rare to see an ambiguous command that does multiple mutually exclusive things. The number of keys you can use to enter insert mode (iaIAsScCoO) is a perfect example of this.
You should use the appropriate command for each situation:
:e tmp.rb
:b tmp.rb
If you don't mind installing plugins, CtrlP's :CtrlPMixed may help you with its nice abstraction.

Vim "show my last command" command?

Is there a command which shows what was the last command in normal mode?
Suppose I accidently hit random key and got some unexpected result.
Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?
Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.
q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.
The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.
To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).
Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.
But none of these actually answer your question. I'm curious if it can be done.
Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.
This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.
It is difficult to know it. You can play with the variables:
v:operator
v:count (and v:prevcount)
v:register
But you cannot fully get the last normal mode command issued.
However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.
If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.
On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.
Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.

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.

How do I close all open tabs at once?

If I have 10 tabs opened, I have to close each one using ":q" separately.
How can I close them all at once?
Shortest/simplest/fastest way would be:
:qa
To save work in all tabs and quit:
:wqa
I often use :tabo (:tabonly) to close all other tabs.
That can be done with the following
command (in normal or escape mode):
:tabdo :q
"tabdo" apparently executes the
command for all the open tabs.
You can use any of these Vim Ex commands to Exit Multiple Windows And Buffers:
:qa :qall
Exit Vim, unless there are some buffers which have been changed. (Use :bmod to go to the next modified buffer). When 'autowriteall' is set all changed buffers will be written, like :wqall.
:conf qa :confirm qall
Exit Vim. Bring up a prompt when some buffers have been
changed. See :confirm.
:qa! :qall!
Exit Vim. Any changes to buffers are lost. Also see :cquit, it does the same but exits with a non-zero value.
:quita :quitall :quita! :quitall!
Same as :qall.
:wqa :wqall :xa :xall
Write all changed buffers and exit Vim. If there are buffers
without a file name, which are readonly or which cannot be
written for another reason, Vim will not quit.
:conf wqa :confirm wqall :conf xa :confirm xall
Write all changed buffers and exit Vim. Bring up a prompt
when some buffers are readonly or cannot be written for
another reason. See :confirm.
:wqa! :xa! :wqall! :xall!
Write all changed buffers, even the ones that are readonly,
and exit Vim. If there are buffers without a file name or
which cannot be written for another reason, Vim will not quit.
To read about these in Vim, type the following Ex command
:help window-exit
Adding to what fuentesjr said:
:qa!
Will force quit all tabs, if you don't care about saving.
:qall
This closes all tabs and open buffers.
here is an Dark Side way of closing ALL VIM INSTANCES on Linux/Mac
:!killall vim -9
Do not use it. It does what you ask but probably not the best way but fun way
I'm using the VIM plugin in VSCode and I was looking for a way to close all the tabs open on the current window.
The commands :qa and :wqa didn't work because they closed all the tabs from all the windows.
The command :tabonly closed all the tabs from the current window except the current tab.
Because I'm usually only using 2 windows at the same time, the closer I managed to get to my need was to focus on the other window and run the command :
:on
(:only) it closes all the windows except the current one.

Resources