Open vim tab in new (GUI) window? - vim

I'd like to move the current tab into a new (visual/real) window in MacVim.
It's probably difficult, as:
there is nothing in the vim help and only very few - not helpful - hits on google
MacVim does not support it (link, 2009)
So I am wondering if someone has found a way to achieve this?

The closest I think you can come is using mksession
This will have several drawbacks (like, initially the secondary session will open a few more buffers than ultimately desired).
However, it will preserve your mappings, settings, history, window layout (if you had multiple windows inside the current tab, they will all get cloned).
If this idea tickles your fancy, you could look at creating a script that will filter parts out of the session file (which is Yust Another Vim Text Script)
:mksession! $HOME/detach.vim
:tabclose
:silent! !gvim remote --servername Detach -nR +'silent! source H:\detach.vim' +tabonly
save all current windows, mappings, settings (:he mksession)
close the tab we are about to detach
clone the session (detach.vim) into a remote vim
:silent! (try not to talk too much)
!gvim remote --servername Detach; launch a new remote Vim server if it doesn't yet exist, or talk to the existing server named Detach
-nR TODO Fix This is here to avoid the use of swapfiles (because I found no way to suppress the dreaded ATTENTION messages[1]). However, this may be unsafe advice depending on your situation, which is why I also include -R for read-only mode
+'silent! source H:\detach.vim' +tabonly -- In the remote vim, source the session to clone, and keep only the active tab (that was already closed in step 1.)
A little rough around the edges, for sure, but quite close to what you intended, I feel.
If you are comfortable, you can drop the -nR flags and just click through a few annoying swapfile attention messages (keyboard: Q).
[1] :he ATTENTION
Post-scripts:
on windows you might want to use :silent! !start /b gvim .... in case you have terminal windows sticking around
also on windows, you might get annoying behaviour if the resulting gvim window is too small. The :simalt ~x sequence is one hacky way to maximize the window (assuming English UI, because x is the accelerator for Maximize)
as icing on the cake, vim 7.3 has 'persistent undo'. See e.g. :se undofile

I don't think this is possible because, when you open a new instance of (g)vim you don't have access to the undo-history of the previous vim instance. All you can do is (in command mode):
:!gvim %
It will open the current file in a new instance of gvim. At least this is all I could think of. I may be wrong

Related

Open file with vim but keep the terminal open as well?

Is there a way to open a new vim file in basically a new terminal window(urxvt) ?
For example if I am currently in ~/X/Y.txt and I write
vim --new-window(basically the command needed to open a new window) Y.txt
I still want my current urxvt session while opening the Y.txt in a vim-Session.
gvim will open up vim in a GUI (i.e., not in the terminal).
You can use vim's client-server model. Run one instance as a server and edit files with option --remote.
Another solution would be to use terminal multiplexors like GNU screen, tmux or such.
Other variants are: use a terminal with tabs, use gvim.
I highly, highly, strongly recommend downloading NERDTree. It makes your Vim terminal look (relatively, comparatively) similar to a "normal" IDE, with a narrow file-and-folder bar on the left side, and a "main" terminal window taking up the rest of the screen. You use Ctrl+W to navigate back and forth between the two screens. Plus, in the sidebar, you can use normal Vim commands to move up and down. Whoever created that plugin put a lot of work into it.

Selectively Enable/Replicate System Menu or Otherwise Show All MacVim Window Instances

I have this in my '~/.vimrc' (among other things):
set guioptions += M
set guioptions -= m
to disable the menus in MacVim. I find that leaving out the '+=M' results in lots of keys being mapped (the menu hot keys etc.).
There is one facility provided by the system menu that I really would like: the ability to show all current MacVim "windows" (in the operating system sense, and not in the Vim sense of a "split"). For example, I could do something like this (perhaps in separate shells, at separate times):
$ mvim foo.txt
$ mvim bar.txt
$ mvim baz.txt
This will result in 3 separate MacVim "windows". With "set go=+M" not set, I can go to the "Windows" menu and see a these three instances listed and, optionally, select one of them.
Is there any way to do this without enabling the system menu, or at least the rest of it?
My motivation is this: after a while, I tend to end up with lots of different MacVim "windows" across multiple desktop spaces (I also tend to use a lot of desktop spaces). Sometimes I try to open, e.g., "foo.txt", only to get the notification that "foo.txt" is already open. Hunting down window in which it is open is a hassle. Yes, yes, I suppose I could change my entire way of dealing with this --- restricting myself to just one MacVim window, being disciplined about quitting files that I am not immediately working on etc. etc. But short of that, is there something else I could be doing?
I would also be happy if there was a command that I could issue either from the shell or from within Vim, that lists the windows and allowed me to activate one of my choice.
Edit
Turns out that MacVim has a ':macaction selectNextWindow:' command that at least allows me to cycle through the various instances:
nnoremap <silent> <M-`> :macaction selectNextWindow:<CR>
nnoremap <silent> <D-`> :macaction selectNextWindow:<CR>
nnoremap <silent> <M-S-`> :macaction selectPreviousWindow:<CR>
nnoremap <silent> <D-S-`> :macaction selectPreviousWindow:<CR>
I mapped both the command ('<D->') and meta ('<M->') because the former mapping only works with MacVim windows in the same desktop space. Weird. A partial solution in that I (eventually) find the window/instance I want, but at O(n) complexity. A window list would make it O(1).
It's generally possible to access menus from the command-line with a command like:
:amenu Edit
but adding M to 'guioptions' means that no menus are loaded whatsoever so :amenu is useless in your case.
Anyway, that window listing feature is provided by MacVim's Cocoa wrapper: it is part of the default functionalities any GUI application gets for free when created in XCode. MacVim doesn't appear to expose it to the Vim core, though, so you are kind of fucked:
the windows list is not available through :amenu,
set guioptions+=M gets rid of all menus, Vim-provided menus and Cocoa-provided menus.
There's another way to look at your problem: MacVim GUI windows are in fact separate Vim instances that all run as servers (see :help clientserver) and you can issue:
$ vim --serverlist
or:
:echo serverlist()
But I don't know of any high-level solution to manage separate servers and I have a feeling that it wouldn't be exactly trivial to come up with a solid solution.
Another way to look at your problem would be to selectively unmap all those undesired mappings without messing with the menus.
So yeah… my advice is to find another, more focused workflow.
If you use mvim often but don't want those menu-related mappings you might as well use mvim -v and work directly in your terminal which would almost certainly fix your mappings issue and your "same file opened in several instances" issue in one shot.

Tmux/Vim plugin that shows a list of open files

When I have many vim files open in different tmux panes, it becomes difficult to keep track of where they all are. I would love to see a list of open tmux panes labeled with the filename of the vim file being edited. Does such a plugin exist? Is it possible to create one?
For such a plugin to work you would need all your vim instances to run as servers on the same machine in order to query all the visible instances for their bufferlist. This sounds like a pathetically complex solution to a really dumb problem, IMO.
Unless you have a very specific need you shouldn't run multiple vim instances. Vim is perfectly able to deal with dozens of files and has many plugins designed to make buffer management easier.
Start with :help buffers.
Also, tmux is a fine piece of software but you should probably take a moment to weight the complexity it adds to your setup and workflow against its actual benefits.
tmux choose-window
will show the name of current directories of all buffers.
(0) 0: ssh "u35#localhost:~"
(1) 1: vim- "u0_a105#android:~/K/20081121"
(2) 2: vim "u0_a105#android:~/K/2014"
(3) 3: vim "u0_a105#android:~/K/20120430"
(4) 4: bash* "u0_a105#android:~/K/20081121"
I personnaly use
bind-key '"' choose-window
so that ^A" gives this list like screen did.
If you want to see the name of current file, you might configure vim to change the buffer name, as bash is doing.

Working with vim on multiple monitors

I have three monitors.
I normally run one maximized xterm on each monitor, attached to the same GNU screen session.
Can a similar model be used for vim? Is it possible to have three vims running, all sharing the same "vim session":
Each vim window showing a different vim tab
vim settings shared between all windows
Not getting the "Swap file ... already exists!" error message if I open the same file on two tabs.
I have a working solution where two vim instances communicate.
http://github.com/codeape2/vim-multiple-monitors
It uses the SwapExists autocmd to instruct the other instance to open a file if an existing swap file is detected.
You can use vim under screen.
$ screen
$ vim
# on another terminal
$ screen -x
# the same vim screen
If your terminal emulator supports tabbing (e.g. gnome-terminal), you can use it as a tabbing (IMHO, gnome-terminals' tabbing support is better than vim's own, except perhaps you can't copy and paste among different vim sessions, however you can instead use the system copy-paste buffer: "+y and "+p).
This does not work if you're using gvim though.
No, a Vim instance is limited to a single application window on your desktop. The different Vim instances have independent sessions.
In addition, be aware that if you open the same file in two different Vim instances, not only do you get the 'Swap file exists' message, but the two instances do not share a buffer, so changes made in one are independent of the other.
If a changed file is saved in one instance you will get a message when you return to the file in another Vim instance that 'the file has changed since editing started' and asking you if you want to reload the file (which would load changes as saved by the other instance, disregarding any changes you had made in the current instance).
Neovim is a project that aim to give vim attach/detach feature like tmux.
This feature isn't yet implemented (june 19th 2015), but this may come soon.
There is many other core features for this project that you can see at neovim.io.
:set noswapfile
:set autoread
Buffers aren't shared this way but vim reads file changes automatically if you save it. And you don't get that 'Swap file exists' message.

What's the best (Linux-compatible) buffer/session manager for Vim?

Is there a good project / session manager for vim? A session (or project) is a named lists of files, e.g. "bitonic_sort" could identify files "~/A/bitonic_sort.sk", "~/B/bitonic_sort.smt2", etc.
(rationale) I have a project where I need to edit files from many different locations, and it is too cumbersome to open them manually each time I resume work. (so, it looks like things like nerdtree brought up at this sister question, Favorite (G)Vim plugins/scripts?, won't work). I also need separate sessions (i.e. lists of files) for different projects, not just a recent document list.
After all documents have been loaded as buffers, any enhancements to switching between them is a plus (e.g. start typing a name, and matching documents are displayed). Thanks in advance.
Vim has built-in session manager. To save your current session use:
:mks session1.vim
This basically create a Vim script named session1.vim, which will restore your opened file if you source it or start Vim like this:
vim -S session1.vim
To overwrite your saved sessions, use :mks! your_saved_session.vim. Combine with a custom key map and this will be the solution. For more about Vim session read :help :mks. Vim also has views manager which is quite similar. Read more from: :help :mkview
For switching between buffers, you can use FuzzyFinder; but I prefer this key map:
nmap <C-tab> :bn<CR>
imap <C-tab> <ESC>:bn<CR>i
Add it to .vimrc and I can use Ctrl + Tab to switch between buffers just like Firefox tabs. Hope this help.
I'm not sure exactly what you're asking for... but if you want to turn on tab-completion when opening files in vim, add this to your ~/.vimrc:
" Auto-complete file names after <TAB> like bash does.
set wildmode=longest,list
set wildignore=.svn,CVS,*.swp
Also, take a look at screen. From the man page:
When screen is called, it creates a single window with a shell in it (or the specified command) and
then gets out of your way so that you can use the program as you normally would. Then, at any time,
you can create new (full-screen) windows with other programs in them (including more shells), kill
existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between
windows, view the scrollback history, switch between windows in whatever manner you wish, etc. All
windows run their programs completely independent of each other. Programs continue to run when their
window is currently not visible and even when the whole screen session is detached from the user's
terminal. When a program terminates, screen (per default) kills the window that contained it. If
this window was in the foreground, the display switches to the previous window; if none are left,
screen exits.
It's pretty much like having several xterms open, except unlike graphical xterms you can access your screen session if you access your machine remotely (e.g. by sshing to it). You could leave up several different instances of vim in separate screens with all the files you want open, and just never exit them.
The very basic setup I use is one vim window, one compile window, and one testing/debugging window.
And since we're talking about vim, check out this post: Post your Vim config. Lots of cool tweaks and spiffy stuff in there.
You can try the vim-workspace plugin, its session management features are automated and relatively simple (compared to vim-session): https://github.com/thaerkh/vim-workspace

Resources