VIM - How to I make Undo put me back in what ever

2019-07-09 04:11发布

问题:

I often go around editing things in normal mode only to realise my change was not intended, as such, I undo and then press K/L/J/H to continue but meanwhile I am already in insert mode, so I have to get out of it first.

I would like to insert a rule on my .vimrc to put me back on the mode I was on just before any changes that I did.

IE: shift+s (delete line and start editing) -> undo : Back in normal mode Bonus: ciw -> asdahsdasdh -> undo: Back in normal mode

PS: I am actually using Undo with Command+Z, the default MacOSX undo combination.

回答1:

OS X is capturing the Cmdz keyboard event and handling undo at the application level, so any mapping for <D-z> will be ignored. However, you can override this to do what you want.

First, add a mapping to your .vimrc:

if has("gui_running")
    inoremap <D-z> <Esc>:undo<CR>
endif

Restart MacVim to ensure the .vimrc has been reloaded.

To get this to work you also need to tell MacVim to not use Cmdz for the "Undo" menu item.

(These directions are based on Yosemite/El Capitan but are similar for older versions.)

  1. Go to the System Preferences app.
  2. Click the Keyboard icon.
  3. Click the Shortcuts section.
  4. Click on App Shortcuts in the left column.
  5. Click the "+" button to add a new rule.
  6. Choose MacVim from the Application pulldown.
  7. Type "Undo" (exactly that and nothing else) in the Menu Title box.
  8. Click into the Keyboard Shortcut box, and choose some shortcut you'll never actually use.
  9. Click "Add", then close System Preferences.

(I chose ShiftCtrlOptCmdx.)

Now going back to MacVim, if you pull down the Edit menu, "Undo" should have the key combination you chose, and your Cmdz shortcut should work as you described.



标签: vim macvim undo