Can I use the `<cmd>` key in terminal Vim at all?

22

4

Following on from my other recent question: How do I map <cmd>-<shift>-f to run Ack plugin in Vim?

Is is possible to use the <cmd> key in Vim at all?

If Vim doesn't recognise the key, is it possible to remap the key outside of Vim (somehow?) to something else and map that key within Vim?

I'm using iTerm 2 on Lion with Vim 7.3.

Thanks!

jordelver

Posted 2012-02-09T12:08:07.707

Reputation: 1 781

Answers

12

<D-> only works in MacVim.

A quick google will return a bunch of hacks and third party solutions. All the solutions I know either work system-wide or are application-specific: if you remap Cmd in iTerm2 only, how are you going to open a new window or a new tab? With… with… the… MOUSE?

I can't imagine a good reason to remap the Cmd key to anything on a Mac. That would be insane.

Vim is not TextMate or whatever other editor you have used before. There is no way to transfer all your old habits seamlessly, point blank.

Instead of wasting your time trying to make Vim and your terminal into something that it's not and can't be you should learn how to use and customize Vim wisely. All your Cmd+Shift+Ctrl+Alt combos won't lead you anywhere. Especially in a terminal where Alt is often problematic and Cmd unusable.

Consider using <leader> (:help leader):

  • it's portable on every platform
  • it's less prone to conflicts with your OS or terminal emulator
  • it opens a lot of easy to remember possibilities

romainl

Posted 2012-02-09T12:08:07.707

Reputation: 19 227

You can remap Cmd to Control in iTerm2, and then in the same window, add shortcuts to open a new tab with ^t, etc. Then everything works! – Sean Clark Hess – 2016-02-19T12:04:24.237

Haha, thank you grawity, it's much cleaner now. – romainl – 2012-02-09T14:14:20.333

Thanks for the reply. I had heard of the <leader> but I wasn't sure how it should be used. I agree with your point about portability, this is part of the reason I am moving to Vim. Thanks. – jordelver – 2012-02-10T23:39:46.747

5

I found that iterm2 has an option in the keyboard settings to do this kind of thing. For ex below I mapped command-enter to be :wq

enter image description hereenter image description here

Neil McGill

Posted 2012-02-09T12:08:07.707

Reputation: 206

Thank you for this solution. This is just what i was looking for. I know about <Leader>, but i think that some things like saving a file should be universal. – Dmitry Biletskyy – 2018-02-16T17:04:54.300

4

For simple commands there is a solution via sending the appropriate hex code to the terminal. For instance, I wanted to save in vim by pressing cmd-s. I mapped cmd-s in iterm2 to send the hex code 0x13 which is the code for ctrl-s, then I mapped ctrl-s to :wq in vim.

jhickner

Posted 2012-02-09T12:08:07.707

Reputation: 141

How exactly do you do such a thing? Thanks. – nunos – 2015-03-31T00:59:52.007

Did not work for me the 0x13 hex code :/ https://imgur.com/RjxOoLY

– michalzuber – 2015-09-29T11:32:38.567

For the how to do this, see the pics below - thanks @jhickner for the idea to look at iTerm2 – Neil McGill – 2017-09-02T21:38:29.643

3

The 2016 solution is to use Karabiner open-source program which allows you to remap modifier and other keys with very fine granularity, for example

  • Remap only the left ⌘ Cmd or Option key.
  • Remap a key only for specific applications, e.g. only inside Terminal, Emacs, or virtual machine.

For example, here's how to remap left ⌘ Cmd key to act as Ctrl only inside Terminal (and leave the right one unaffected so that you could still use e.g. ⌘ Cmd + Tab to switch between apps):

karabiner-screenshot

dorserg

Posted 2012-02-09T12:08:07.707

Reputation: 221

Awesome. Now I can use that in Vim properly, though (left) command+tab doesn't "alt tab". I can use right command but is there anyway to keep that somehow? – meder omuraliev – 2017-05-10T21:49:03.390