Search code examples
vimtextmate

TextMate's Command + Enter in Vim?


In TextMate when you are editing a line of text and you press command + enter it inserts a newline and brings the cursor to that new line without bringing down any text from the previous line. Is there a way to create this feature in Vim? Any insight would be much appreciated. Thanks!


Solution

  • The following mapping works:

    inoremap <D-Enter> <ESC>o
    

    The D maps the command key on Mac. The answer by CatPlusPlus shows how this would work when using the Control key instead.

    Note that mapping the command key only works in MacVim.

    So in order to make this fail proof inside your vimrc do the following:

    • Check if a gui vim is running via:

      let isGui  = has("gui_running") 
      
    • Check if you are running on a Mac via:

      let isMac  = has("mac")
      
    • Now adjust your mapping accordingly:

      if(isGui && isMac)
        inoremap <D-Enter> <ESC>o
      else
        inoremap <C-Enter> <ESC>o
      endif