I have multiple file open in Vim. When i want to switch from one file to another Vim does not allow me to switch unless I save the file or quit. Is there a way such that I do not have to save everytime when I want to switch? Is this a problem with Vim or am I making a mistake? If that's the way Vim works can anyone tell me the logical reason?
3 Answers
Just add
set hidden to your ~/.vimrc. It makes it possible to have multiple unsaved files open at all times.
While you are at it, you should add this line as well:
set switchbuf=useopen,usetab It forces Vim to jump to an already open buffer where it is (right there, in another split-window, in another tab) instead of "hiding" the current buffer to replace it with the target buffer. This is useful for quickfix-related jumps but also for :sb.
These two lines are the key to use Vim's buffers efficiently.
3 Comments
:sb, :sbn or :sbp can also be used to move efficiently between buffers while :b, :bn and :bp don't care about this setting and replace the current buffer with the other one. So, if you use windows and tabs you should add this line but not if you only use a single window.Use :n!. This will move to the next file, ignoring the changes and not saving them.
2 Comments
You can also use tabs in vim; use :tabnew file.txt to open a new tab with file.txt loaded. Then you can use gt and gT to navigate forward and backwards through your open tabs. I find this easier than dealing with buffers, but I'm a fairly new vim user.
6 Comments
:help windows), which is extremely powerful Vim feature. And there are many plugins that make switching buffers easier. Off the top of my head, "ctrlp" or "buffet" plugin, but there are many ones.
:set hiddenin your.vimrcshould help you.