1

I would like to have an abbreviation (xdate in this case) expanded when I use it in nnoremap. Is this possible? Here is what I've tried:

iab xdate <c-r>=strftime('%c')<cr>
nnoremap <leader>w i#xdate<space>

When I use the above mapping (\w) I get:

#xdate

But what I want is:

#Wed 28 Sep 2016 12:03:18 PM MST 

which is what I get if I type #xdate in insert mode.

  • 1
    I may be wrong but I have the feeling that you are trying to solve an XY problem. So just so you know, you can create a mapping inserting #timestamp without using abbreviation: nnoremap <leader>w "='#' . strftime("%c")<CR>P – statox Sep 29 '16 at 9:52
  • @statox I thought about something like this, but I figured either way was valid, so I just answered his direct question. – Tumbler41 Sep 29 '16 at 15:01
  • @Tumbler41 I wasn't criticizing your answer at all since it answers directly the question. I simply feel like OP's question come from a previous workaround he found and I thought it would be interesting to show him a simpler way to do it. I would argue that my solution is simpler since it uses only one instruction, it is still readable and less likely to confuse someone reading the code (what if the abbreviation is not next to the mapping?) but if OP is satisfied with it it's all good :) – statox Sep 29 '16 at 15:10
  • @statox Thanks for your useful comment. The reason I want to avoid doing it the way you've shown is because I use the timestamp abbreviation on its own, and also want to use it in additional maps. Consequently, if I decide to change the format, I only have to change it in once place. – equant Sep 29 '16 at 18:32
  • 1
    @equant Ok so I guess your solution is the best one :) Welcome on our site by the way! – statox Sep 29 '16 at 18:44
2

nnoremap is a non-recursive mapping. This means that it will not take your abbreviation into account. Change it to:

iab xdate <c-r>=strftime('%c')<cr>
nmap <leader>w i#xdate<space>

and it should work.

| improve this answer | |

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.