1

I want to have a special file where each line starts with a timestamp, right now I could do it with an abbreviation for example:

:iab <expr> ts strftime("%F %T") 

This works if I manually type ts when I add a new line. But is there some way to automatically create new lines with pre-filled timestamp at the beginning, and if there is no text on that line then not to include the timestamp at all.

Trying to make an audit log for myself to capture any interruptions from my environment during programming, and would really like to just type text and have it timestamped.

1 Answer 1

3

If you want the insertion to be automatic, you need to overwrite the commands you use to insert a new line, usually o:

:nnoremap <buffer> <expr> o 'o' . strftime("%F %T") 

To make this apply only for your special files, use <buffer> and define this command via an

:autocmd BufRead /path/to/audit/log nnoremap ... 

If you plan more customizations or special syntax highlighting, better define a custom filetype and write a :help new-filetype and put the mapping in ~/.vim/ftplugin/myfiletype.vim.

Sign up to request clarification or add additional context in comments.

4 Comments

This works great. Doesn't really cover when I was in insert mode already and just pressed enter ... but guess its good enough for now.
Thanks! You could add :imaps, too, but the general recommendation is to avoid staying in insert mode for too long. And the more mappings you create, the higher the probability that it interferes.
Used imap <buffer> <expr> <CR> '<CR>' . strftime("%s ") works like a charm! This is just for this one special file that is acting as the interrupt log, so shouldn't affect other files.
actually a simpler solution for me would be just - imap <buffer> <CR> <Esc>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.