Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I map return in insert to go to normal mode and then do the o action

Tags:

vim

I am currently using vim and I'd like to map the Return key (I'm on a mac. I believe that this is generally represented by <ENTER> in maps) to leave insert mode, and then perform the o action. I was trying to put something like this

imap <ENTER> <ESC>o

However this is not performing the desired action. Any help would be fantastic.

Edit: The desired action is that if I am typing in insert mode, each new line is a new action. So if I press undo in normal mode it just undoes the last line instead of all the lines typed while in insert mode.

like image 584
Eli Sadoff Avatar asked Dec 19 '25 14:12

Eli Sadoff


1 Answers

I'm not sure why this isn't working. I do not know why escaping and using o to open a newline does not add to the change-list. However, lucky for us, there is a command for explicitly adding the current state of the text to the change list. That command is (in insert mode) <C-g>u. From :h i_ctrl-g_u

CTRL-G u    break undo sequence, start new change        *i_CTRL-G_u*

Conveniently, this command doesn't even leave insert mode! Putting it all together, the mapping you're looking for is:

:inoremap <cr> <C-g>u<cr>

Or, you could also do

:inoremap <cr> <cr><C-g>u

which will leave you with a blank line after undoing.

like image 156
DJMcMayhem Avatar answered Dec 23 '25 02:12

DJMcMayhem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!