How to map cmd+s to save all? - vim

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.

Related

Vim :Explore, don't edit file when pressing <Enter>

in Vim, I type :Explore to open the vim browser.
When I press on a directory, I enter the directory as desired.
When I press on a file, I start editing that file. I would like to do something else.
So basically I would like to map to 'enter directory' for directories, and something custom for files.
The netrw plugin allows opening of file in the current window, (vertical) splits, and a remote Vim instance. The closest built-in functionality to what you want is customized browsing with a special handler, which is triggered with the plugin's x mapping; cp. :help netrw-x.
You can customize via an external command, or a Vimscript function that you define (:help netrw_filehandler). In this, you can do "something else", with all the power of Vim to implement it.
As for the distinction between directories and files, this mostly falls flat due to the different keys used. If you need to distinguish in your custom function, the isdirectory() function is there to help.
alternative
The NERD tree plugin offers an alternative to netrw, and also offers a similar extension, either via custom mappings, or a menu. As it's mostly a drop-in replacement for netrw (but with additional functionality), it might be worth to check it out before investing too much in customizing netrw.

Preview images in vim explorer

How can I preview images in vim's explorer (the :Explore command)? One of the solutions would be to execute a specific command with the filename under cursor as an argument, but I don't know how to get it. I searched through the help of :Explore, but can't find anything that can help. I know I can do this in other file managers, but I want this functionality inside vim.
The netrw plugin (which provides the :Explore command) has a built-in preview (cp. :help netrw-preview), which uses Vim's preview window. But of course, most image files are binary, so it probably doesn't make sense to open it in Vim itself; you probably want to open this in an external image viewer (like eog).
This is the mapping (in the explorer buffer) that powers the preview:
:nmap <buffer> p
n p *#:<C-U>call <SNR>731_NetrwPreview(<SNR>731_NetrwBrowseChgDir(1,<SNR>731_NetrwGetWord(),1))<CR>
The inner part (<SNR>731_NetrwBrowseChgDir(1,<SNR>731_NetrwGetWord(),1)) yields the full path to the current file. You could use that to launch an external viewer:
:call system('eog ' . shellescape(<SNR>731_NetrwBrowseChgDir(1,<SNR>731_NetrwGetWord(),1)))
Unfortunately, those netrw functions are script-local (and the <SNR>731 part probably is different on your system), so you'd have to directly modify the netrw plugin to add your "external image preview" shortcut, also because the mapping is not a global one, but specially set up by the plugin for its explorer buffer. I can provide you more details if you decide to follow that route. It's also possible to extend the plugin without modifying it, but that's a lot more work.

How to map keys for IdeaVim's normal mode to editor's action?

I am using JetBrains' phpstorm with the IdeaVim plugin.
I am wondering if I can bind keys in normal mode to editor actions.
For example, I used to have mapped Ctrl+B to Navigate > Declaration. Yet Ctrl+B is a vi motion to go one page backwards and that is ok.
I know I can configure a keyboard shortcut to a different one, e.g. Ctrl+Shift+B , yet to keep things simpler I want to have a key in ideavim's command mode mapped to that functionality, e.g. ;.
So that pressing ; in command mode would trigger the action of Declaration witin phpstorm.
How can I achieve this?
To give a specific answer for exactly what you asked to map: put this into your ~/.ideavimrc:
nnoremap ; :action VimGotoDeclaration<CR>
To find the action name, I typed :actionlist declaration which gives a subset of action names that include the word "declaration" in the action name.
As others have noted, you might also prefer to use one of the existing mappings rather than adding a new one.
what you wanted go to declaration is built in command in vim. You don't have to use IDEA's actions.
gd (goto declaration) is the thing you are looking for.
So you just press (normal mode) gd, to see what is gonna happen.
In a normal vim, do :h gd to check details.
You can use <C-]> (Ctrl+]) for following references (jumping to the declaration is an example of a reference) and <C-O> for going back. You can also map these Vim-style shortcuts using the map commands similar to the original Vim.

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.

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>

Resources