7

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!

puk
  • 16,318
  • 29
  • 119
  • 199
jet
  • 698
  • 6
  • 12
  • 1
    It may or may not solve your problem, but you may want to try using [MacVim](https://code.google.com/p/macvim/) rather than Vim in Terminal. – icktoofay Dec 04 '11 at 06:22
  • @icktoofay hmm, I guess I had too high of an expectation of the "UNIX core" under OSX, lots of things actually have to be Mac specific. I will give MacVim a spin then. – jet Dec 04 '11 at 06:39
  • It's not a difference with the "UNIX core", but rather with Terminal. If you use `xterm` then it should work as it did on Linux, but `xterm` isn't OS X-native; it has to run under the X11 helper application. – icktoofay Dec 04 '11 at 06:40
  • 1
    This isn't much help, but it's working for me. I went to System Preferences -> Keyboard and checked "Use all F1, F2, etc. keys as standard function keys" (which it sounds like you did), put your lines into my .vimrc, and now pressing F5 runs Python. I'm running under OS X 10.6.8 and vim 7.2.108. – SSteve Dec 04 '11 at 06:41
  • @icktoofay that makes sense, I thought it had to be a problem that terminal isn't actually firing `` when it's pressed, but just couldn't find any way of fixing it. – jet Dec 04 '11 at 06:44
  • @SSteve yeah, I did that already. That's really strange. Do you have the identical line for assigning `` in your vimrc file? (I'm running Lion, so that could be making the difference, although a really bizzarre one.) – jet Dec 04 '11 at 06:46
  • Yes, my .vimrc contained exactly (and only) the two lines from your question. Maybe try putting something obvious at the end of your .vimrc file (like "help") to make sure your .vimrc file is getting loaded. – SSteve Dec 04 '11 at 07:13
  • MacVim fixed both problems. although it is kind of annoying that I have to open another window.... =( good to go for now – jet Dec 04 '11 at 07:40
  • When you're on the console your keyboard may not be sending Vim what you think it's sending. See my answer to this question for more details: stackoverflow.com/questions/9950944/ – Andrew Langman Apr 11 '12 at 14:37

4 Answers4

4

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).

Community
  • 1
  • 1
ches
  • 6,382
  • 2
  • 35
  • 32
1

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.

Matt Rohrer
  • 137
  • 6
1

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.

Community
  • 1
  • 1
Mark Carey
  • 1,567
  • 10
  • 9
0

I used the following in my vimrc to copy and paste

if &term =~ "xterm.*"
    let &t_ti = &t_ti . "\e[?2004h"
    let &t_te = "\e[?2004l" . &t_te
    function XTermPasteBegin(ret)
        set pastetoggle=<Esc>[201~
        set paste
        return a:ret
    endfunction
    map <expr> <Esc>[200~ XTermPasteBegin("i")
    imap <expr> <Esc>[200~ XTermPasteBegin("")
    cmap <Esc>[200~ <nop>
    cmap <Esc>[201~ <nop>
endif

I got it from here https://stackoverflow.com/a/7053522

Community
  • 1
  • 1
SysCoder
  • 715
  • 7
  • 18