Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim on mac os X function key mapping not working

Tags:

vim

macos

I have recently started using a Mac OS X Lion system and tried to use Vim in terminal. I previously had a .vimrc file in my Ubuntu system and had F2 and F5 keys mapped to pastetoggle and run python interpreter. Here are the two lines I have for it:

set pastetoggle=<F2>
map <buffer> <F5> :wa<CR>:!/usr/bin/env python % <CR>

It's working just fine in Ubuntu but no longer works in Mac. (The above two lines are in .vimrc under my home dir.) I have turned off the Mac specific functions in my preference so the function keys are not been used for things like volume. Right now pressing F5 seems to capitalize all letters until next word, and F2 seems to delete next line and insert O.....

Is there something else I need to do to have it working as expected?

In addition, I had been using solarized as my color scheme and tried to have the same color scheme now in Mac. It seems that the scheme command is being read from .vimrc, but the colors are stil the default colors. Even though the .vim/colors files are just the same as before. Is this a related error that I need to fix? Perhaps another setting file being read after my own? (I looked for _vimrc and .gvimrc, none exists.)

Thanks!

like image 475
jet Avatar asked Dec 04 '11 06:12

jet


3 Answers

I finally got my function mappings working by resorting to adding mappings like this:

if has('mac') && ($TERM == 'xterm-256color' || $TERM == 'screen-256color')
  map <Esc>OP <F1>
  map <Esc>OQ <F2>
  map <Esc>OR <F3>
  map <Esc>OS <F4>
  map <Esc>[16~ <F5>
  map <Esc>[17~ <F6>
  map <Esc>[18~ <F7>
  map <Esc>[19~ <F8>
  map <Esc>[20~ <F9>
  map <Esc>[21~ <F10>
  map <Esc>[23~ <F11>
  map <Esc>[24~ <F12>
endif

Answers to these questions were helpful, if you need to verify that these escape sequences match your terminal's or set your own:

mapping function keys in vim
Binding special keys as vim shortcuts

It probably depends on terminal emulators behaving consistently (guffaw), but @Mark Carey's suggestion wasn't enough for me (I wish it was so simple). With iTerm2 on OS X, I'd already configured it for xterm-256color and tmux for screen-256color, and function mappings still wouldn't work. So the has('mac') might be unnecessary if these sequences from iTerm2 are xterm-compliant, I haven't checked yet so left it in my own config for now.

You might want some imap versions too. Note that you shouldn't use noremap variants since you do want these mappings to cascade (to trigger whatever you've mapped <Fx> to).

like image 72
ches Avatar answered Oct 19 '22 18:10

ches


Regarding your colorscheme/solarized question - make sure you set up Terminal (or iTerm2, which I prefer) with the solarized profiles available in the full solarized distribution that you can download here: http://ethanschoonover.com/solarized/files/solarized.zip.

Then the only other issue you may run into is making sure you set your $TERM xterm-256color or screen-256color if you use screen or tmux.

You can take a look at my dotfiles for a working setup, but don't forget to setup your Terminal/iTerm color profiles as a first step.

like image 33
Matt Rohrer Avatar answered Oct 19 '22 19:10

Matt Rohrer


see this answer: https://stackoverflow.com/a/10524999/210923

essentially changing my TERM type to xterm-256color allowed me to map the function keys properly.

like image 39
Mark Carey Avatar answered Oct 19 '22 19:10

Mark Carey