Search code examples
vim

Adding timestamp in vim, in config file


I have added the following line to my vim config file:

inoremap <leader>dt r! date "+\%Y-\%m-\%d<CR>

In order to add a shortcut for printing the current date. But when I use the shortcut in normal mode it only prints the string: "r! date "+%Y-%m-%d". Why is that? What am I doing wrong?


Solution

  • But when I use the shortcut in normal mode it only prints the string: "r! date "+%Y-%m-%d".

    You are doing that in insert mode, not "normal mode".

    Why is that?

    Vim is doing exactly what you ask it to do, which is to insert the string r! date "+\%Y-\%m-\%d<CR> when you press <leader>dt in insert mode.

    What am I doing wrong?

    The most salient item in the list would be that you didn't explain what, exactly, you are trying to achieve, asking instead for a fix to your flawed solution. This is known as the XY problem and a very common trap to fall into. In fact, since you appear to be surprised by Vim doing exactly what you ask it to do, it is possible that you are not sure about it yourself. Can you clarify this, please?