1

I'm making a program for taking statistics during sports, where I script the games. I'm would like to make a VIM shortcut/remap, so every time I press that it does exactly what the ENTER-key normally does, but that it simply inserts a timestamp as well (and it should only be done in Insert-mode).

My thinking was, that I then could go into my VIMRC-file and activate the shortcut, whenever I was doing statistics.

Here's what I've tried:

 iabbrev <CR> <CR>:put =strftime('%H%M%S')^

The '^' in the end is there in the end, because when you insert the timestamp with :put =strftime('....'), that the cursor then is a the beginning of the newly inserted timestamp. But nothing happens with this in my VIMRC-file.

I've also tried:

iabbrev <ENTER> <ENTER>:put =strftime('%H%M%S')<CR>

And still, nothing happens.

I've even tried this, to try and see, if I could just make VIM insert any character, whenever I hit ENTER, but this doesn't do anything either:

 iabbrev <CR> <CR>1

I apologies, if this is a stupid question.

EXAMPLE OF USE:

Let's say that I was typing this on the keyboard:

upthrtwo
ussevfiv
tptwothr
uhsevtwo

Then I wanted VIM to have written this:

151852upthrtwo
151907ussevfiv
151917tptwothr
151949uhsevtwo

Thanks for your time and assistance.

1 Answer 1

2

Try with the following

:inoremap <CR> <Esc>:put =strftime('%H%M%S')<CR>A

This will map your enter key when in insert mode (inoremap) so that

  • It exits to command mode (<Esc>)
  • It runs :put =strftime('%H%M%S'), which inserts a newline and writes your timestamp to the new line (also note the <CR> at the end to execut the comamnd)
  • It puts you back in insert mode at the end of the new line with A
1
  • It works. When I enable it, then it works all fine and dandy. But when I comment it out in the VIMRC, then it keeps inserting the timestamp (probably because there's no active command that says 'THE ENTER KEY IS THE ENTER KEY'. So when I comment it out in my VIMRC-file, then I just have to add the line>> :inoremap <CR> <CR> Whereafter the ENTER-key functions normally again.
    – Zeth
    Nov 5, 2016 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.