VIM: Insert Current Date Or Time

Advanced feature discussion, beta programs and unsupported "Labs" features.
6 posts Page 1 of 1
by nhdesign » Fri Sep 13, 2019 5:06 am
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()
by joemuller » Fri Sep 13, 2019 1:20 pm
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')
I'm a proud employee of Sonic.net! :-)
by nhdesign » Fri Sep 13, 2019 1:37 pm
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
by joemuller » Fri Sep 13, 2019 1:57 pm
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
I'm a proud employee of Sonic.net! :-)
by nhdesign » Fri Sep 13, 2019 2:07 pm
Wunderbar!

Thanks,

Have one on me!

_, . '__ .
'_(_0o),(__)o().
,o(__),_)o(_)O,(__)o
o(_,-o(_ )(),(__(_)oO)_
.O(__)o,__).(_ )o(_)Oo_)
.----| | | | | |_)0
/ .--| | | | | |,_)
| / | | | | | |o(_)
| | | | | | | |_/`)
| | | | | | | |O_)
| | | | | | | |
| \ | | | | | |
\ '--| | | | | |
'----| | | | | |
| | | | | |
\ \ \ / / /
`"""""""""""""""""`
by nhdesign » Fri Sep 13, 2019 2:07 pm
my ascii art no longer works.
6 posts Page 1 of 1

Who is online

In total there are 29 users online :: 0 registered, 0 hidden and 29 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 29 guests