Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using MacVim, I'd like Command-f to not show OS X' Find/Replace dialog, but instead beginning a normal search in Vim, i.e. sending / from normal mode.

I.e. I'd like MacVim to use Vim's internal search instead of OS X' Find/Replace.

(I hope there's a MacVim-specific setting that I can simply add in .vimrc)

share|improve this question
You could always fall back to defining application specific shortcuts in tools like Butler. – Daniel Beck Aug 5 '11 at 18:16
I'm not trying to be difficult, but why not just press /? – wfaulk Aug 5 '11 at 18:20
I've never tried MacVim, but can't you just type / like you do in normal Vim to do searches? I'd say instead of trying to use macros/defines to make Command-f map to /, just type / and do your search that way, or is that not possible? – sbtkd85 Aug 5 '11 at 18:20
wfaulk & sbtkd85: True, but typing / involves two keypresses, with two hands – Shift and 7 – on my keyboard layout (Swedish), whereas: 1.) Command F is only a one-hand job (don't get this one wrong). 2.) this shortcut is standard throughout the rest of the operating system. 3.) MacVim is already providing shortcut extras specific to OS X (e.g. Command S for saving, etc). This is certainly up for argument and personal preference, but I only intend to complement, as opposed to replace, Vim's standard / search shortcut, should I ever need to import my vimrc in a Command-key-less environment. – Henrik Aug 7 '11 at 22:37
up vote 7 down vote accepted

Unbind ⌘F and Remap It

You can “unbind” ⌘F (Command+F; written as <D-f> in Vim) from the Edit > Find > Find… menu item with MacVim’s :macmenu command and then map it with a normal :map command.

if has('gui_macvim')
  macmenu Edit.Find.Find\.\.\. key=<nop>
  map <D-f> /

This particular :macmenu command is only effective if you put it in your ~/.gvimrc, so the above lines should be placed there.

The Mac OS X find dialog will still be displayed if you invoke the menu item itself (clicking on it or using :emenu).

Redefine the Menu Item

An alternate approach is to redefine the menu items themselves. This would be inappropriate if you wanted to bind ⌘F to some different function (since the Edit > Find > Find… menu item would do something other than a search!), but it might be acceptable since you just want to start a different style of search.

You can use variations on the :menu command to redefine the menu item to be (more or less) equivalent to typing /:

if has("gui_macvim")
  nmenu Edit.Find.Find\.\.\. /
  vmenu Edit.Find.Find\.\.\. <C-C>/
  imenu Edit.Find.Find\.\.\. <C-\><C-O>/
  cmenu Edit.Find.Find\.\.\. <C-C>/
  omenu Edit.Find.Find\.\.\. <C-C>/

The best place for the these commands is your ~/.gvimrc (though for slightly different reasons than for the :macmenu command above).

These commands cover normal mode, the visual and select modes, insert mode, command mode, and operator-pending mode.

This approach means that both pressing ⌘F and selecting the menu item will start a /-based search (i.e. the mode indicated by the cursor just after / in the command line area instead of using the GUI-based :promptfind).

Command Placement

As noted in each of the above suggestions, the placement of the commands is important. They should each work correctly if you put them in ~/.gvimrc. But some of the commands can also be effective in other contexts.

The biggest influence on the effectiveness of the menu commands is their position with respect to the sourcing of $VIMRUNTIME/menu.vim. This file contains the commands that build the menus. It is automatically sourced between ~/.vimrc and ~/.gvimrc, but it can also be source manually (e.g. see :help console-menus). Most of the commands in this file are protected by an if statement so that they are only ever run once. This means that you could source it manually and then redefine some of the menu items in your .vimrc so that you could use the changed menu items in both the console and GUI modes.

Placement of nmenu, …

The commands from the “Redefine the Menu Item” solution can be effective in your ~/.vimrc if you put source $VIMRUNTIME/menu.vim before them. If you do not source menu.vim first, then your definitions will be overwritten if/when it is sourced later (i.e. later in ~/.vimrc; automatically, when switching on syntax highlighting; automatically, before ~./gvimrc for GUI-based instances of Vim; or manually, at a : prompt).

Most GUI instances of Vim use Edit.Find\.\.\. for the menu item, but MacVim inserts an Find submenu in the hierarchy to match the usual Mac OS X arrangement, so the commands should be protected by has('gui_macvim') (so that they do not cause errors if you ever use your startup file with a non-MacVim build of Vim).

Placement of macmenu

The macmenu command is only effective at startup time. You can type it in at a : prompt, but it does not actually change any setting if you use it that way. This is mentioned in :help :macmenu, but I missed it the first several times I skimmed the documentation:

This command must be used in a startup file, for example in "~/.gvimrc". It has no effect otherwise.

In menu.vim, after the menu items have been created, some macmenu commands are used to establish the default shortcut keys for the menu items and the GUI “actions” they trigger. However, unlike the menu definition commands in the same file, the macmenu commands that establish these bindings are not protected from being run multiple times. This means you can use macmenu commands in ~/.vimrc to establish bindings/actions for menu items that do not have default bindings/actions, but that you must use ~/.gvimrc to modify the bindings/actions that are established in menu.vim. This seems like a mild bug in the MacVim version of menu.vim.

So, the only (currently) valid place for macmenu command is in ~/.gvimrc, after the final startup-time sourcing of menu.vim. And again, some of the menu hierarchies are specific to MacVim, so the commands should be protected with has('gui_macvim').

Placement of map <D-f>

The <D-…> key notation looks like it is valid in most builds of Vim (though it is probably only actually usable on “Mac” builds). So, you should be able to safely map Command keys in your ~/.vimrc without worrying about causing errors for other builds of Vim. Of course, in MacVim, if you are mapping a key that is usually a menu item shortcut, you will also have to unbind it from the menu item (which means an accompanying macmenu command in ~/.gvimrc).

share|improve this answer

You can unbind Command+F using macmenu. Sample code for you .gvimrc below:

if has("gui_macvim")
  macmenu &Edit.Find.Find\.\.\. key=<nop>
  map <D-f> /

As pointed out in the question below, this needs to be in you .gvimrc and not in your .vimrc

MacVim: Re-map command key combinations like <d-f>

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .