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

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.

Related

How to map cmd+s to save all?

I'm developing some front-end project with the help of Yeoman.
I run the developing webserver by grunt serverand, because there is livereload.js in the project, for watching files change and refreshing the webpage, sometimes, I need to modify multiple files for one purpose(.html, .css, .js ...) and it's kind of waste for browser when each file saves.
So is there a way for MacVim, map cmd + s to :wa ?
nnoremap <D-s> :wa in your .vimrc should do the trick.
nnoremap means this mapping is only available in Normal mode, and is not recursive.
:wa means write all, i.e. save all the current open buffers.
If you don't want to use a new mapping, you can simply type :wa to achieve the same things, but this is a matter of preferences only.
I know this is old but I came searching with the same question.
tl;dr: ⌥+⌘+S to save all.
You can, supposedly, map <D-s> to :wa but it's not as simple as adding the mapping to your .gvimrc file. See the third bullet below.
From the MacVim docs:
cmd-key cmd-shortcuts
Creating key mappings that involve the Cmd key ( in Vim notation) can
sometimes be slightly involved. Here are all the things you need to consider:
Make sure the shortcut is not used by a menu item by looking through the
menus. If it is then you need to unbind it before you can map to it. This
is described under the help for the |:macmenu| command.
Bindings to are case sensitive: is not the same as . If
you want to map something to Cmd+Shift+d, then you need to use , not
or .
Some command key shortcuts are reserved by Mac OS X and cannot be mapped to
(e.g. ). However, some of these shortcuts can be freed up in the
System Preferences under Keyboard (e.g. Cmd+Space).
The good news? In trying to sort this out I realized that MacVim is a very courteous MacVim indeed and implements, by default anyway, many standard Mac shortcuts including: ⌥+⌘+S for "Save All". Open MacVim, click the file menu and then hit the option key to see it in action.

Vim: Is it possible to bind a key to open search (i.e. put me in the command mode having entered a slash, but do not type anything)

While vim's way of doing things is generally good, I feel like there are aspects that really get in the way as somebody who is not completely committed to vim for text-editing.
This means that I am hitting Esc+/ and typing slashes into files in Sublime Text, and hitting Ctrl+F in vim, scrolling down a page.
Similar things happen with Ctrl+S and :w and custom mapping ; to : still doesn't make it as convenient and fail-safe as Ctrl+S. Not to mention in PuTTY, Ctrl+S does something rather upsetting (it suspends output from the terminal till Ctrl+Q is pressed). Basically, IMHO some things (like switching to a "find mode" and saving the file) are good to attach to a global left-hand home-row shortcut chord rather than requiring the user to mess about in a modal command line. It simply is too often used to subject the user to these mode changes. I suppose this isnt a legitimate complaint because the entirety of vim is constructed around the modal concept, and these functionalities are a result of that.
In any case I'd like to make it a bit easier on my brain because I've tried for a while now to keep it all intact and it's simply too much (I have more or less internalized the switching between Ctrl on a windows machine on a desktop keyboard and the use of the thumb for the Cmd on my Macbook, but this modal text editor business seems to be too much). It did not take too much deliberation for me to decide that global homerow key chords are simply more intuitive.
Mapping Ctrl+S to :w is trivial, so the question here is about the find mode. I want to bind Ctrl+F to put me in find mode, but repeating it should not type slashes into the find command. Can this be done? Is there perhaps a colon-command that when executed simply drops me into the current find-command-buffer-thing?
In your .vimrc add:
inoremap <C-f> <Esc>/
noremap <C-f> <Esc>/
In whatever mode you are in, this will put you in "find" mode (it actually just types a slash in the command line which you can use to search). With these bindings it will work for all modes, even if you are in insert mode.
do you want this?
nnoremap <c-f> /
if you like you can also try
nnoremap <c-f> q/
the 2nd mapping brings you to a search history window. you could choose previous search keywords/patterns. also available to type new pattern (by pressing i)
btw, there is no find mode
Mappings can be defined separately for the different modes in Vim, see :help map-modes. This automatically takes care of your proposed <C-f> mapping: Initially, you're in normal mode, then the search pattern is entered in command-line mode.
If you so far only occasionally use Vim, and find it too "different", several things may make the switch easier:
easy Vim (evim or vim -y) defaults to insert mode
on Windows, :behave mswin and the $VIMRUNTIME/mswin.vim script create some common Windows shortcuts
graphical GVIM has menu items like Edit > Find... and toolbars.
Some things like the flow-control freeze on <C-s> in terminals are unfortunate, mostly historical accidents; you can't blame Vim for those.
You don't need to map anything. / is expressive enough and is definitely not slower than <C-f>. Same for <C-s> which, despite a certain familiarity, is not much better than :w<CR>. You are learning a new tool, remember?
What you should focus your efforts on is getting confortable with Vim's modality. The simple fact that, for you, doing a search involves hitting <Esc> is a sign that you didn't get that part. Your other comments on using Vim shortcuts in ST and ST shortcut in Vim are not good signs either and the part where you complain about having to adjust to your Mac's Cmd is the icing on the cake, IMO.
It may sound brutal but, from your question, it seems like you are doing too much at the same time, without organization and for, probably, no good reasons.
Switching from Windows to Mac OS X and switching from ST2 to Vim at the same time is sure to be confusing and I suggest you slow down a little and make a choice before you loose your mind and waste more of your time.
Which leads me to this: Why do you use a Mac and why are you using Vim?
Windows is a fine OS. Sublime is a fine editor. Even if you switch to the Mac, Sublime works very well there. So Why Vim?
If you are tired by some Sublime limitations and want the awesome power of Vim, fine. But you must wrap your head around its modality and awkwardness and accept to change your habit.
If you don't approach Vim with more flexibility, you'll never get full benefits.
If you decided to use Vim because it is trendy, I'll respectfully suggest you to stop there and go back to Sublime. Vim is weird, Vim is different, Vim is huge, Vim is a rabbit hole… if you are not ready to work with it and insist on not accepting its core design, you won't get far so why bother? You can pretty much live all your professional carreer without ever touching Vim. There's no shame in that.
Remapping / to <C-f>, on the other hand…

MacVim: Any idea why I can't override the Command button?

I am trying to use the command button in MacVim instead of Ctrl and other buttons.
I tried something like :imap <D-space> <Esc> to override the normal Command-space functionality while in MacVim but it still performs the spotlight.
Any ideas?
In these cases its usually the window manager intercepting the key-stroke before the application ever sees it. There may be an option to turn off certain global key-bindings when a specified app is active but I've been unable to find it on 10.5 - linux window managers have this functionality, as does windows, so I'd expect its hiding somewhere in the OS WM..
The Spotlight shortcut takes precedence over your own shortcut.
To be able to use it in MacVim you must change Spotlight's shortcut in its preferences.
However, the Command key won't work in Terminal.app and won't be sent to remote servers connected by SSH. If you use Vim on a server you won't be able to use it and you will be forced to either create and learn lots of machine-specific mappings or simply use the defaults.
Another possible issue, one I encountered at the beginning, is that shortcuts with multiple modifier keys (like <D-M-something> or <C-S-something>) don't work in MacVim.
Because of that and the number of native shortcuts using these keys you won't have many possibilities.
When I decided to learn Vim (with MacVim), I tried a lot of things to make it (and Vim) more "Mac-like" or "TextMate-like" to no avail.
Instead i suggest you to do things the Vim way. It may seem weird but it's worth it.
In your case, the "Vim way" would be to use mapleader, it's a regular key on your keyboard that is used for custom mappings. The default key is \, if you want to change it (to , for the example, that's my settings but YMMV), add this line to your ~/.vimrc:
let mapleader=","
After that you can create mappings like this:
inoremap <leader><Space> <ESC>

Open vim tab in new (GUI) window?

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

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