How to save all files in tabs on Vim? - 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

Related

VIM command to explore buffers

I often use the command :Explore to switch to another file. I also use a lot the command :buffer to switch between previously opened files, but it is not always convenient when a lot of files are opened.
Is there a way to display a list of all opened files (buffers) in the current window, in a "explore" way, without using plugin?
:help :ls is the closest you can get with basic built-in tools.
I would recommend creating a normal map like this in your ~/vimrc file.
" list buffers and jump to a chosen one
nnoremap <Leader>b :ls<CR>:b<Space>
It triggers your <Leader> plus b to execute two commands at once, first it shows all open buffers, then it allows you to type the buffer number to open it. It wort reading :h leader.

How can I delete the current file in vim?

How can I delete from the disk the current open file from within vim? Would be nice to also close the buffer.
I see you can use NERDTree for that, but I don't use this plugin.
Using an external tool such as rm(1) is fine, but Vim also has its own delete() function for deleting files. This has the advantage of being portable.
:call delete(expand('%'))
An alternative way of expressing this is :call delete(#%), which uses the % (current file) register (tip by #accolade).
To completely purge the current buffer, both the file representation on disk and the Vim buffer, append :bdelete:
:call delete(expand('%')) | bdelete!
You'll probably want to map this according to your preferences.
Take a look at Delete files with a Vim command. The Comments section should have what you're looking for:
Basically, Rm will delete the current file; RM will delete the current file and quit the buffer (without saving) in one go.
Alternatively, you could just issue a shell command to remove the current file:
:!rm %
Sometimes a plugin can be an attractive solution even for a simple problem. In this case we're lucky as there is eunuch.vim by the almighty Tim Pope.
In its own words eunuch.vim provides
Vim sugar for the UNIX shell commands that need it the most. Delete or rename a buffer and the underlying file at the same time. Load a find or a locate into the quickfix list. And so on.
Perfect. It has what we need, plus some additional tools if we're on a UNIX system.
The command you are looking for is
:Remove!
Again, remap it if you need it a lot, e.g. :nnoremap <Leader>rm :Remove!<CR>.
I like being able to delete files from within vim, but I am also paranoid about accidentally deleting important work that, for one reason or another, is not yet under version control. I find it useful to combine the previous information from #glts and #accolade with this answer on how to use the confirm command to prompt before quitting vim.
Putting these together, I added a function to my ~/.vimrc, which prompts before deleting the file and closing the buffer, and mapped it to a key combination:
nnoremap <Leader>d. :call DeleteFileAndCloseBuffer()
fun! DeleteFileAndCloseBuffer()
let choice = confirm("Delete file and close buffer?", "&Do it!\n&Nonono", 1)
if choice == 1 | call delete(expand('%:p')) | q! | endif
endfun
If you are one keystroke less paranoid than I am, you can append <CR> to the first line
nnoremap <Leader>d. :call DeleteFileAndCloseBuffer()<CR>
and only have to press return once.
You can do it with two steps:
save as a new file
:w newfilename
delete the old file
! rm oldfilename
This may be an unpopular opinion but your file tree explorer (Netrw/NerdTree) is going to be the simplest and safest way to delete a file. Even if OP is not using NerdTree, the in-built plugin Netrw will work just as well.

Edit multiple files without splitting window

I am new to VIM.
It seems like, in order for copy and paste functionality to work between different files, one must open the files in the same VIM instance. Doing this, however, splits the terminal screen into 2. Opening a new file, splits it into 3, etc.
what happens if i have to open, say, 10+ files? how do developers who use VIM deal with this issue?
You can open files in the same vim instance and in the same window (without spliting it). That's what is called buffers. The view you see is current buffer, but the others buffers are still opened.
Here you have an introduction on how to work in vim with buffers.
Or type :help buffers in vim.
You can use some plugin to work more user friendly with vim buffers. I recommend you bufexplorer or minibufexpl.
You can as well use tabs, but I have been always more confortable with buffers, but it's just my case.
Use tabedit instead of split to open more files.
Open all your txt files in tabs:
$ vim -p *.txt
Use gtgT to switch between tabs.
Or you can put these key-mappings in your .vimrc:
nmap <C-H> gT
nmap <C-L> gt
nmap <leader>t :tabnew<CR>
Most users use splits only for simultaneous viewing of two files, when it is needed. You can open as many files as you want in different buffers, while only displaying one buffer on screen.
:e File1.txt
:e File2.txt and so on ...
and then switch through buffers with :bprevious and :bnext (and a variety of other commands). That is really the Vim's way of work.
There are many plugins for manipulating and navigating buffers.
One tab per file is really the wrong way to go ...
You will find that VIM is a very flexible and customizable tool, so there are probably several approaches to this. Personally, I like to only have one buffer open at the time (I rarely need to split up) and use the Minibufexpl plugin to keep track of how many buffers are open and switch more efficiently between them.
If your only requirement is to copy paste between files. You can do this between different instances of vim if you set clipboard=unnamed
This causes vim to use the system clipboard instead of it's own internal buffer. so you can <C-c> from firefox and then p into vim, y from one vim p into another etc.
See http://vim.wikia.com/wiki/VimTip21
If you like having the files open in the same window I would use one of the other answers here and use either buffers (instead of splits) or tabs. I personally often have 10 or more buffers open in a single gvim window and 2 split windows that I use to view the buffer that I switch between using :bn and :bp.

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