How do I close all open tabs at once? - vim

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.

Related

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.

What is the Vim command to quit all open windows?

:q only closes the current window. If you are using tabs or split windows, you need to do :q for all of them. Also, plugins like NERDTree and MiniBufExpl have their own windows, which need to be closed individually.
Is there a command to quit all these open windows and quit Vim in a single stroke? However, if there is some buffer or window with unsaved changes, I should be asked to save it or not. Any command to achieve this?
I hope this is not a strange request, because this is how most non-Vim editors with tab or splits work.
You can quit all loaded and open buffers, splits and tabs with:
:qa
If you want to quit without saving:
:qa!
You could assign a mapping to do this with a single stroke, this assigns the comma to quit everything without prompting to save:
nnoremap , :qa!<CR>
:wqall writes before closing, that might be useful.
Type :he :qa in vim for more info
you can user the
:qall
:qa
quit all the tabs opened
then you can use the command
:tabo
quit all other's tabs
if your's vim is not tabs you can use
:on
quit all windows

How to save the files opened in all windows and tabs in Vim?

I’d like to save the files opened in all vertical/horizontal windows? Is it possible without going to each window and executing the :w! command?
To save only those buffers that opened in the current tab page and not
those that are hidden, run the :write command for every open window:
:windo w!
In order to save all open buffers regardless of the corresponding
windows’ locations, run the :wall command:
:wa!
There is also a similar command
:bufdo w!
but it does not behave in quite the same fashion. Both commands affect
hidden buffers, but :wall does not attempt to write the buffers
that do not have a file name set.
Yes, you can do this with :wa.
Use :wall
It writes all changed buffers (but it will also save the hidden one).

How to save all files in tabs on Vim?

If I have multiple files in tabs on VIM and I edit few of them. How to save them with one command?
The command wa (short for wall) will write all changed buffers. You can also use :tabdo w, which is definitely exactly what you want, and generalizes nicely.
Just do
:wa
(followed by return) which is a shorthand for
:wall
Also to "save everything and exit" you can do
:wqa or :xa
(="write-quit-all")
It's possible to suffix a[ll] for a number of Vim command-line commands (i.e. type : when in normal mode), include:
:wa - save all tabs / unsaved buffers
:xa/:wqa - save all tabs / unsaved buffers and exit Vim
:qa - exit vim (will warn if unsaved buffers exist)
To save all the files just use an a after the write command to write all the files.
:wa
And you can use :tabdo! w too, I'm just adding this, because it's useful for other things too (e.g. :tabdo! g/somepattern/ s/something/anything/... I use it all the time for refactoring purposes...)
Check out :wall command

How to temporarily exit Vim and go back

How could I exit Vim, not :q, and then go back to continue editing?
Assuming terminal Vim on a flavor of *nix:
To suspend your running Vim
Ctrl + Z
will suspend the process and get back to your shell
fg
will resume (bring to foreground) your suspended Vim.
To start a new shell
Start a subshell using:
:sh
(as configured by)
:set shell?
or
:!bash
followed by:
Ctrl+D (or exit, but why type so much?)
to kill the shell and return to Vim.
You can use :sh to exit to your default shell then typing $ exit at the shell prompt will return you to Vim.
You can switch to shell mode temporarily by:
:! <command>
such as
:! ls
You can also do that by :sus to fall into shell and back by fg.
If you frequently need to go back and forth between shell and vim, probably what you really want is have only one vim instance in the shell, and use it to open any file in the workspace. If so, check this question. Once you set it up correctly, you can :sus or C-z to return to the shell, then just v or v <newfile> to get back to vim.
And my answer is almost my daily routine.
If you are on a Unix system, Ctrl + Z will suspend Vim and give you a shell.
Type fg to go back. Note that Vim creates a swap file while editing, and suspending Vim wouldn't delete that file (you aren't exiting Vim after all). On dumb terminals, this method was pretty standard for edit-compile-edit cycles using vi. I just found out that for me, gVim minimizes on typing Z.
If you're using Neovim, you can do the following:
:terminal command to bring up a terminal window.
Do your terminal stuff
Type exit to kill the terminal process
Press any key to return to Neovim
Just put in fg and go back to your most recently suspended program.
There are several ways to exit Vim and have everything the same when you return. There is very good documentation within Vim itself explaining the various ways this can be done. You can use the following command within vim to access the relevant help page: :help usr_21
To give you a brief summary, here are the different methods of quitting and returning with your session intact:
Suspend and resume - You don't actually quit Vim with this; you simply hide your session in the background until you need it. If you reset your computer or issue a kill command to Vim, you will lose your session. This is good for when you want to switch to another task temporarily, but if this is the case, then you might want to look into using the GNU Screen utility instead.
Sessions - This is the true way of saving your session between instances of Vim. Even if you truly quit Vim, your session will be there for you when you return. This is probably what you are looking for.
To extend user Zen's answer, you could add the following line in your ~/.vimrc file to allow quick toggling between Bash and Vim:
noremap <C-d> :sh<cr>
If you don't mind using your mouse a little bit:
Start your terminal,
select a file,
select Open Tab.
This creates a new tab on the terminal which you can run Vim on. Now use your mouse to shift to/from the terminal. I prefer this instead of always having to type (:shell and exit).

Resources