Tuesday, May 25, 2010

Re: editing an existing buffer

On 25/05/10 17:11, Ben Fritz wrote:
>
>
> On May 24, 6:20 pm, Kirill Igumenshchev<kirill_i...@yahoo.com> wrote:
>> hi,
>> suppose i have a vim session (vim1) with multiple buffers. I open another session vim2 in another terminal and would like to edit one of the files that is already opened as a hidden buffer in vim1.
>>
>> is there a way to do so that both vim1 and vim2 are synced?
>
> Only by saving your changes in one Vim before switching to the other.
> In general I would advise against this, but you could do it by making
> sure you only ever have a single unsaved copy of this file. Write the
> file, switch to the other Vim, and execute :checktime if Vim doesn't
> automatically detect a file change. Then you can reload the file.
>
> I'm not sure how well this works for hidden buffers, if at all...I
> don't use them often myself. And I rarely ever have multiple Vims
> editing the same file intentionally.
>

Different Vim sessions have no knowledge of each other's 'modified'
buffers, so I would recommend *not* to use 'hidden' (which hides buffers
in memory without saving them when you edit another file) in this case.
What you could do is:

:set nohidden autowriteall autoread

'hidden' = false: don't hide modified buffers

'autowriteall' = true: when leaving a modified buffer, save it, unless
the opposite is explicitly requested by an exclamation mark (i.e., :qa!
never saves anything, even with this option) Note: [NoName] buffers
cannot be written implicitly, you will have either to use an exclamation
mark to abandon them (as in :q! :enew! :e! someotherfile) or else, the
:saveas command to save them.

'autoread' = true: if a file was modified outside of Vim (or in a
different Vim), and it is not modified in this Vim, read it.

Then you can add the following autocommands:

:au FocusLost * wa|wv
:au FocusGained * rv|checkt

(so that all editfiles and viminfo will be saved and read back when you
switch Vim instances) but be warned that only gvim, and a very few
terminal versions, can detect gain and loss of focus: so instead of
opening vim1 and vim2 in console mode in different terminals, you would
have to open gvim1 and gvim2, which can still be launched from the
terminal(s).

Of course, to see whether your console version can detect focus, you can
set up

:au FocusLost * echomsg "Focus Lost!"
:au FocusGained * echomsg "Focus Gained!"

but don't put your hopes too high: the help says that most console
versions cannot detect focus changes, but doesn't say which ones can.
Here, my GTK2/Gnome2 vim can detect focus changes when in GUI mode, but
the same executable cannot detect them when in console mode in konsole;
and even in the GUI it seems unsystematic: at the bottom of the
":messages" list I see

Focus Lost!
Focus Gained!
Focus Lost!
Focus Lost!
Focus Gained!

which seems to indicated that one focus-gain event was, well, lost (when
at the hit-enter prompt, I think).


Best regards,
Tony.
--
A CONS is an object which cares.
-- Bernie Greenberg.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment