4

I mostly use Vim (or MacVim) to edit text, but occasionally (such as in writing this question), I use the Mac OS text editing keybindings built in to all text entry fields accessed through a Mac.

I find these binding extremely intuitive for writing text (whereas Vim bindings are better focused on editing text.)


Specifically, for those unfamiliar with a Mac:

Option-left moves left a word.

Option-right moves right a word.

Command-left moves to the soft wrapped beginning of the line (akin to g0 in Vim).

Command-right moves to the soft-wrapped end of the line, after the space (since soft-wrapped lines are broken between words in Mac text boxes).

Option or Command delete will delete to the beginning of the word or soft-wrapped line, respectively. Adding the Fn key as well means deleting to the end of the word or soft-wrapped line. (The delete key on the Mac is what PCs call backspace; Fn-delete on a Mac means to delete forward one character.)

Option up and down work on actual lines, irrespective of the soft-wrapping positions, and additionally move to the start or end of the line moved to, respectively. In other words they align to k0 and j$ in Vim. In usual text documents this is considered moving to the start or end of a paragraph.

Command up goes to the beginning of the entire text document, and Command down goes to the very end of the entire text document.

Shift can be combined with any of these motions to highlight text.


How can I emulate these behaviors in Vim, for insert mode only?

(Note: While writing this question I discovered that in Vim, the shift key modifies left or right arrow key presses the way that the option key does in Mac text fields.)

Note: MacVim specific answers are okay, but I do use Vim in the terminal a lot as well. (More than I use MacVim, in fact.)

2 Answers 2

3

MacVim GUI

Opt-Left and Opt-Right already do what you want by default, as well as Alt-Backspace.

Cmd-Left and Cmd-Right are mapped to <Home> and <End> by default. You can remap them to do whatever you want:

inoremap <D-Left> <C-o>g0
inoremap <D-Right> <C-o>g$
nnoremap <D-Left> g0
nnoremap <D-Right> g$
xnoremap <D-Left> g0
xnoremap <D-Right> g$

Alt-Backspace already works the way you want but you will need another mapping for Cmd-Backspace:

inoremap <D-BS> <C-o>dg0

Fn-Alt-Backspace could be approximated with this mapping:

inoremap <A-Del> <C-o>dw

As far as I know, Fn-Cmd-Backspace has never done anything in Cocoa widgets (I am a bit behind, though) but you could create a Vim mapping if you really want:

inoremap <D-Del> <C-o>dg$

Opt-Up and Opt-down are mapped to { and } by default, not k0 or k$. The original Mac OS X behavior has always been a bit complex and non-deterministic, though, so I think you will need a bit of vimscript to reach your goal.

Cmd-Up and Cmd-Down already do what you want but Shift-Cmd-Up and Shift-Cmd-Down don't do anything. Once again, custom mappings to the rescue:

nnoremap <S-D-Up> v<C-Home>0
nnoremap <S-D-Down> v<C-End>$

CLI Vim

The Cmd key is only visible to the MacVim GUI so you won't be able to use it in CLI Vim at all.

The Alt key is usable but not very dependable. Some things will work directly in Vim, some things will require you to create shortcuts directly in your terminal emulator, some things won't ever work.

Additionally, CLI Vim can't register multiple modifiers at all.

Conclusion

While it is relatively easy to make the MacVim GUI behave like standard Cocoa text editors, it is virtually impossible to reach that goal with CLI Vim.

Furthermore, Vim's command language being vastly more expressive than any "native" set of shortcuts makes it actually counter-productive to try to make it adhere to your platform's limited conventions. Vim is internally consistent and that's what makes it valuable. Dumbing it down will only deserve you.

This means that your goal is simply not worth the hassle.

3
  • There is an option in the Mac Terminal, "Use option as meta key." It makes it fully dependable. I use it all the time. (Without this option set, it will often send Unicode characters rather than intended escape sequences, but I handled that long ago.)
    – Wildcard
    Nov 12, 2016 at 18:21
  • Also, got it on the advice but I find that Vim's command language is much much better for editing text than for writing new text, where it is of limited value. This would be a very helpful set of keybindings for getting Mac users who are developers to start using Vim more often.
    – Wildcard
    Nov 12, 2016 at 18:24
  • 1. Option as Meta is an abomination if you don't use plain QWERTY. 2. Who cares if Mac users use Vim more often?
    – romainl
    Nov 12, 2016 at 19:13
1

Have a look at Tim Pope vim-rsi, a very useful Readline key bindings for Vim in insert mode. You can map cmd-key to meta-key easily in iTerm2. I would prefer it instead of Terminal.app.

rsi-maps:

<C-a> Go to beginning of line

<C-e> Go to end of line

<C-b> Go backwards one character

<M-b> Go backwards one word

<C-f> Move forward one character

<M-f> Move forward one word

.... and so on ..

I think, this is, what you are looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.