15

How do you unwind this? because you wind up pressing command r all the time to reload your browser, and often you're in the terminal.

Since I haven't figured out how to re-enable the arrow keys, I currently kill all my screens and restart screen, which is a pain.

migrated from stackoverflow.com Nov 26 '10 at 3:22

This question came from our site for professional and enthusiast programmers.

28

As a good little Vim advocate, I feel bound to tell you to learn to use hjkl as your cursor keys. Halfway through exploring the problem, I thought it had mysteriously fixed itself, until I realized I was testing using hjkl instead of the actual arrow keys.

Anyway, now for a real answer.

Instead of shutting down all your screens and restarting, you can take advantage of screen's ability to move between terminals. Hit CTRL+A CTRL+D to detach screen from your terminal, then run screen -D -R to reattach. This should make your arrow keys work again.

If you're not in screen, but just in Vim, then you can CTRL+Z and type fg for the same effect.

You could also work around the cursor key problem in Vim, but the reset might have changed other terminal options, so it's safer to do as described above lest you see other random display/keyboard problems. If you want the remappings anyway, here they are:

map <Esc>[A <Up>
map <Esc>[B <Down>
map <Esc>[C <Right>
map <Esc>[D <Left>

Grubby details:

Both Vim and screen use the ncurses library to manage the terminal. One of the things ncurses is doing when it takes control of a VT100-compatible terminal is switching the arrow keys into "application mode", which changes the codes they emit. The up-arrow key, for example, changes from Esc [ A to Esc O A. The VT100 control code it emits to do this is Esc [ ? 1 h. The terminal reset command sent by Command+R resets the cursor keys to the default.

Detaching and reattaching screen causes ncurses to retake control of the terminal, which reapplies all of the settings it likes, so that's probably the most portable and reliable way to restore things to normal.

VT100 control codes: http://www.handshake.de/infobase/dfue/prgrmmer/t322.htm

  • Another issue may be the terminal type confusing vim, see Messed up keys in vim when running inside tmux – kynan Mar 16 '12 at 1:51
  • thank you for this solution! this has been bugging the crap out of me as a web developer. in the browser you hit cmd-r a lot to refresh the screen and occasionally i must have been hitting cmd-r in vim by accident. i couldn't figure out why my arrow keys were being wiped until now. and for you hjkl purists out there, the main reason i don't use hjkl is because they aren't available for cursor movement in insert mode. solve that problem for me and i'll reconsider. – rev Aug 13 '12 at 19:29
6

You can fix this quickly and easily with :!reset

1

If you're using iTerm, you can get your vim session back by pressing command+d and command+w

Why?

command+d will split the terminal pane, magically restoring the one you came from.

command+w will now close that split pane you just opened.

1

I map Command-r to "Ignore" in the iTerm key mappings.

Screenshot of iTerm2 preferences dialog

Your Answer

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