I'm putting some code in my .vimrc to automatically update the "Last Update" field on my .cpp and .h files documentation.
I've tried it for my .py files and it worked. The problem is searching the line that starts with the characters * Last Update
. Here's what I have so far:
The comments in my cpp file
/**
* @file test.cpp
* @author John Doe
* @version
* @brief
* @date
* Created: 21 mai 2019
* Last Update:
*/
and my .vimrc
autocmd BufWritePre *.h exe "%s/^ \* Last Update:.*$/Last Update: "
\. strftime("%d %b %Y (%T)") . "/e"
This should update the comments to:
/**
* @file test.cpp
* @author John Doe
* @version
* @brief
* @date
* Created: 21 mai 2019
* Last Update: 21 mai 2019 (21:15:48)
*/
But I got no changes at all.
Update: I have the same code in my header files (.h)
Updated to work with .cpp
and .h
files
You need to add the following to your .vimrc
or other file that gets sourced:
autocmd FileType cpp,h autocmd BufWritePre <buffer> :%s/^ \* Last Update:.*$/\=printf(' * Last Update: ') . strftime("%d %b %Y (%T)")/e
autocmd FileType cpp,h
.cpp
or .h
filesautocmd BufwritePre <buffer>
:%s/^ \* Last Update:.*$/
\=printf(' * Last Update: ') . strftime("%d %b %Y (%T)")/e
When the {replacement} starts with \=
it is evaluated as an expression.
As a string is no expression printf
is needed to output the first part of the string. The two functions printf
and strftime
are then concatenated via .
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With