34

I recently upgraded from OS X 10.6 (I think) to 10.9. Since then it seems, while editing in vim, the arrow keys will "spontaneously" stop working.

At one point, in frustration I "mashed" one of the arrow keys and was eventually shown a "E388 Couldn't find a definition" error. All other times I've experienced it, the arrows, having worked for awhile, suddenly start dinging at me!

Quitting and reopening solves the problem temporarily. But, I'd like to prevent it!

Anyone know what this might be? And how to fix it?


It looks like my default vimrc was modified during the update. If my memory is correct, it was a pretty big file previously. Now, it just contains this:

" Configuration file for vim
set modelines=0         " CVE-2007-2438

" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible        " Use Vim defaults instead of 100% vi compatibility
set backspace=2         " more powerful backspacing

" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup

I have no idea what these options do yet. I'll look into it -- but, hopefully someone here knows more quickly than I can google and read ...

9
  • I also just realized there's an Apple-centric forum here that this question might be better suited for. So, if this is more likely a condition of OS X than vim, perhaps a mod could migrate this for me.
    – svidgen
    Feb 22, 2014 at 3:55
  • 2
    Reminds me of working on a "Prime" mainframe in college. The professor taught that you should never use the arrow keys in vi. Always use hjkl. That's where the "arrow" keys use to be on ancient hardware. Feb 22, 2014 at 3:56
  • OK... But, the arrow keys should work. Regardless of whether your professor thinks I should use them. And I find them convenient at times. So, looking forward to see the solution.
    – svidgen
    Feb 22, 2014 at 15:28
  • 1
    what happens when you insert the key literal into a buffer? go into insert mode, and press Ctrl+v, then an arrow key. Vim on my Mac shows the up arrow key as ^[OA, down as ^[OB, right as ^[OC, and left as ^[OD. check before and after the problem presents itself Feb 26, 2014 at 18:21
  • @NathanWallace not sure how I overlooked your question. I'll try that when I'm at my Mac next.
    – svidgen
    Mar 4, 2014 at 4:53

8 Answers 8

31

Found in James Hodgkinson's blog, the following command works for me. Note it will refresh the vim screen.

:!reset
4
  • Not sure how I overlooked this answer, but it looks very promising!
    – svidgen
    Mar 17, 2015 at 17:07
  • 1
    In fact ... Hitting Ctrl+R as described in the article reproduces the issue in full consistency with the wrong arrow-key mappings noted in my response to Nathan above. And the reset command solves it in that case. And .. I can't reliably reproduce the problem any other way, so this is definitely the most useful answer!
    – svidgen
    Mar 17, 2015 at 17:15
  • does not fix the problem for me, but I do think the Cmd+R (OS X in my case) is what gets me into this state. Running inside screen Apr 5, 2020 at 17:52
  • FWIW, I had the arrow key problem only in VIM inside screen, other windows in the same screen session were working. Disconnecting and reconnecting the screen session fixed the problem for me. Apr 5, 2020 at 17:55
22

I have run into this problem a few times as well. I happened to be running GNU screen on another computer that I was logged in to via SSH. To fix the problem, I detached my screen(s) (Ctrl-a d), logged off of the machine, logged back in, and reattached using screen -x. It may work without logging off of the machine, just by detaching and reattaching screen. I cannot remember though.

4
  • 1
    Closing my screen session and restarting it fixed it for me.
    – Eno
    Aug 12, 2015 at 18:50
  • 1
    that was the problem for me. Somehow screen got in a state of funk (over an ssh connection). Closing that terminal, then ssh again, and screen -rd did the trick. Thanks!
    – Pierre D
    Mar 31, 2016 at 12:40
  • 1
    @PierreD correct, I've run into this issue a few times already as well - also in a screen session, Detach & Reattach solved the problem, thanks!
    – stdcerr
    Jul 11, 2019 at 18:34
  • 1
    For me I think it was triggered by the GPG tty agent being launched from vim in a screen session - detach / reattach fixed it. Feb 5, 2020 at 18:57
13

In the path /usr/share/vim/ I found the .vimrc file... There I added the line

set term=ansi 

It worked for me. Now the arrow keys are working properly in Vim editor.

3
  • 4
    This fixed it for me too, since latest Windows 10 WSL/Ubuntu update broke it.
    – Gray Fox
    May 27, 2018 at 16:25
  • Worked for me but add the above in home directory ~/.vimrc Aug 12, 2020 at 4:34
  • This worked for an Ubuntu Docker instance using Docker Toolbox Windows 8.1.
    – Navigatron
    Apr 12, 2021 at 15:05
4

This sounds very much like your issue. The article is titled: Cursor Keys in Vim: You Mac’ing Me Crazy!.

excerpt

Vim is useful in many situations, particularly for quick edits (especially as root), and editing files on a remote server. One annoyance I've had during my nine months with a Mac Book Pro is cursor keys in Vim ringing the bell in both insert or command mode instead of changing lines. Upgrading to version 7 via MacPorts yielded the same results. The cursor keys worked after switching my TERM to VT220 from rxvt! The downside was syntax highlighting stopped working.

Luckily, adding VT220 to my Google search parameters turned up a reference to vt100-cursor-keys in the Vim documentation. Add the following code to your .vimrc file to fix the problem:

    :set notimeout          " don't timeout on mappings
    :set ttimeout           " do timeout on terminal key codes
    :set timeoutlen=100     " timeout after 100 msec

In the same article there's another suggestion of setting your $TERM to linux instead on OSX.

excerpt

A better solution is to change the default value for TERM to either linux or dtterm since the TERM value will be picked up on the remote server. In this day and age, I would bet most servers support both these terminal types.

4
  • Hmm ... I'm even more confused now. I'd thought I had global/default .vimrc files. Would these have been moved or removed during an OS X update?
    – svidgen
    Mar 5, 2014 at 2:35
  • @svidgen - No I don't think that would've changed, but that issue sounded er-rely similar to yours, and the fix is easy enough to try out, so I thought I'd share it with you. Incidentally the $TERM could've gotten changed on you.
    – slm
    Mar 5, 2014 at 2:39
  • Whelp, this is definitely the most relevant answer here. +1 for that. Not sure whether it fixes it yet, unfortunately, since I can't consistently reproduce the problem.
    – svidgen
    Mar 5, 2014 at 3:07
  • After having applied this fix, the issue has occurred again. Still a relevant answer, leaving the +1. But, please see my comment above if you're interested in supplying an additional answer.
    – svidgen
    Mar 9, 2014 at 3:43
0

Congrats on the update. I did my own update over the weekend and have been dealing with some of my own odd keyboard issues.

Here are a couple of things I needed to to:

  • Remove old Logitech gaming software and upgrade. Prior to doing this I was having issues using my square and curly braces. Special keys often send esc-[ sequences in the VIM context so this might be relevant.
  • Go into System Prefs:Security and Privacy:Privacy:Accessibility and enable a couple of apps (synergy_server and Logitech gaming software). Prior to this change I was unable to control my other computers with synergy or use macros with my G11. I doubt this is a direct connection for you but the Accessibility options are new and might apply to some other app of yours.
0

If you are working in .sql files in vim (you don't mention) this could help you. I just dealt with this the other day.

In file ~/.vimrc

let g:omni_sql_no_default_maps = 1

this particular issue is discussed here

If you're not experiencing this problem with .sql files in vim then I apologize, my only thought would be to verify the mapping within vim when the problem occurs.

:map Ctrl V → when you lose your ability to use your arrow, do that command and see if the mapping disappeared. if it did, this gives you a place to start digging in.

0

I had this problem in an SSH session and solved it by logging out then back in again:

exit
ssh ...
2
  • Thanks for weighing in. The selected answer has consistently worked for me though. Have you tried it?
    – svidgen
    Oct 19, 2018 at 17:52
  • Yes, the accepted solution didn't work on Ubuntu 18. I even uninstalled and reinstalled vim (in the same ssh session) and no dice. Only logging out then back in again restored arrow functionality.
    – duhaime
    Oct 19, 2018 at 18:12
0

I had this issue when I used a "system environment variable" in Windows, setting TERM to xterm-256color. The issue went away (and my vim returned to normal) when I deleted the variable again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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