Page 1 of 1

VIM: Insert Current Date Or Time

Posted: Fri Sep 13, 2019 5:06 am
by nhdesign
Good Morning,

This works great on my work computers.
Please install what ever is missing at Sonic so I can use it here.

https://vim.fandom.com/wiki/Insert_current_date_or_time

function! LastModified()
if &modified
let save_cursor = getpos(".")
let n = min([20, line("$")])
keepjumps exe '1,' . n . 's#^\(.\{,10}Last Modified \).*#\1' .
\ strftime('%Y-%m-%d-%H:%M') . '#e'
call histdel('search', -1)
call setpos('.', save_cursor)
endif
endfun
autocmd BufWritePre * call LastModified()

Re: VIM: Insert Current Date Or Time

Posted: Fri Sep 13, 2019 1:20 pm
by joemuller
Do you get an error from vim on shell.sonic.net? I was able to run the following command per the wiki article you linked:

Code: Select all

:pu=strftime('%c')

Re: VIM: Insert Current Date Or Time

Posted: Fri Sep 13, 2019 1:37 pm
by nhdesign
I pasted your line into a vim session and it worked fine.

I don't get any errors on the screen, no file dumps in my directory.

When it writes the file it has not found and or updated the "Last Modified" with the new time value.

I am using it when updating status reports I write in VIM. Works great and I never have to remember to update my subject line. But like I said, it only works at my job, not at sonic. FWIW at work I am on Linux version 2.6.32-504.el6.x86_64 (mockbuild@x86-023.build.eng.bos.redhat.com) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP T
ue Sep 16 01:56:35 EDT 2014

Thanks,
Vic

Re: VIM: Insert Current Date Or Time

Posted: Fri Sep 13, 2019 1:57 pm
by joemuller
Hi Vic,

I tested out your function on the shell server using my personal account - you should be able to paste the following block into your .vimrc file:

Code: Select all

function! LastModified()
if &modified
let save_cursor = getpos(".")
let n = min([20, line("$")])
keepjumps exe '1,' . n . 's#^\(.\{,10}Last Modified \).*#\1' .
\ strftime('%Y-%m-%d-%H:%M') . '#e'
call histdel('search', -1)
call setpos('.', save_cursor)
endif
endfun
autocmd BufWritePre * call LastModified()
There's also a good example over in this Reddit thread, though I haven't tested it.

-- Joe

Re: VIM: Insert Current Date Or Time

Posted: Fri Sep 13, 2019 2:07 pm
by nhdesign
Wunderbar!

Thanks,

Have one on me!

_, . '__ .
'_(_0o),(__)o().
,o(__),_)o(_)O,(__)o
o(_,-o(_ )(),(__(_)oO)_
.O(__)o,__).(_ )o(_)Oo_)
.----| | | | | |_)0
/ .--| | | | | |,_)
| / | | | | | |o(_)
| | | | | | | |_/`)
| | | | | | | |O_)
| | | | | | | |
| \ | | | | | |
\ '--| | | | | |
'----| | | | | |
| | | | | |
\ \ \ / / /
`"""""""""""""""""`

Re: VIM: Insert Current Date Or Time

Posted: Fri Sep 13, 2019 2:07 pm
by nhdesign
my ascii art no longer works.