0

I'm learning key mappings, I know that:

:imap <c-d> <esc>ddi

means "If you are in 'insert' mode and press ctrl+d, it performs first <esc> like you have been pressed the esc key and then press d, d and i". So you escape from insert mode, delete a line and enter insert mode again.

Ok, but I find this:

:map <F2> a<C-R>=strftime("%c")<CR><Esc>

And I don't understand, if I read it like the fisrt command I get "If I press F2 it's like press the key a with ctrl+shift+r(uppercase R)? and then it prints the strftime and <CR>?

1
2

Let's take it bit by bit:

map <F2> a<C-R>=strftime("%c")<CR><Esc>

map                                     Create a mapping in several modes (Normal, Visual, Select, Operator-pending)
    <F2>                                Map the key F2
         a                              Start insert mode with `a` i.e. after the cursor position
          <C-r>                         Insert the content of a register see `:h i_CTRL-R` (http://vimhelp.appspot.com/insert.txt.html#i_CTRL-R)
               =                        Here we will use the expression register see `:h i_CTRL-R_=` (http://vimhelp.appspot.com/insert.txt.html#i_CTRL-R_%3d)
                                        That means that we will enter an expression, it will get evaluated and the result will be inserted
                strftime("%c")          This is the expression to be executed see `:h strftime()` (http://vimhelp.appspot.com/eval.txt.html#strftime%28%29)
                                        with %c as a parameter this returns something like: Sun Apr 27 11:49:23 1997
                              <CR>      To validate the expression we used in the expression register
                                  <Esc> And escape to normal mode once we are done

So indeed this mapping switches to insert mode, inserts the current date and time and switches back to normal mode

A few observations about this command:

  • Never use map instead use mode specific commands, and unless you know what you are doing always use the non recursive version of these commands, here you most probably want nnoremap (since the commands switches to insert mode with a)
  • Key notations (:h key-notation) like <C-r>, <CR>, <F2> are case insensitive.
  • You can experiment with the expression register by yourself: in insert mode just press Ctrl+r= and experiment with different expressions.
3
  • @BLayer Good point I never use map without a mode so I always forget which mode it actually creates a mapping in. Thank you for pointing that out!
    – statox
    Jul 5 at 8:15
  • 1
    NP. I hear you...I never use bare map either.
    – B Layer
    Jul 5 at 8:16
  • In this particular case, that could also be written "=strftime(…)<cr>p i think
    – D. Ben Knoble
    Jul 5 at 14:07

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.