6

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?

1
  • 1
    I think, :set hidden in your .vimrc should help you. Commented Oct 5, 2012 at 7:10

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks for the quick reply. Was not expecting such a quick reply.
Is it ok if I do not add 'set switchbuf=useopen,usetab' while using vim. can you please elaborate the purpose of adding that line?
When you are jumping to an error from the quickfix window, the default behaviour is to replace the current buffer with the one with the error. If the buffer with the error is already open in a window or tab this setting will make you jump to that window or tab. With this setting, commands like :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.
1

Use :n!. This will move to the next file, ignoring the changes and not saving them.

2 Comments

I wonder if there's an equivalent for CONTROL-SHIFT-6 (which switches between the most recent two buffers)...
Hi CrazyCasta, Using :n! does not serve my purpose. What it does is discards the changes I have made when moving to the next file
0

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

This is completely wrong approach. While you do not "dealing with buffers", you can't use many Vim's useful functionalities.
@DmitryFrank could you elaborate on some of the useful functionalities I miss by using vim tabs?
Tabs is not the replacement of buffers. Of course you can use tabs, but do not use only tabs, because then you can't use Vim's windows (: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.
I agree this is the wrong approach: Vim's "tabs" don't work like the tabs in other editors at all. They are more like workspaces and should not be considered as proxies for "files".
@romainl Hmm, valid points. Should I remove my answer, edit it, or leave it as is with this comment attached? I'm open to removing it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.