Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Automatically Update Copyright Notice in Files
(section)
Back to page
Edit
Edit source
View history
Talk (0)
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
Anti-spam check. Do
not
fill this in!
==More Complex Notices== The above works great for simple copyrights with just a range of years, but if you need more precise/correct ones (that skip years something was not worked on, e.g. "copyright 2004, 2006-2008") it will fail miserably. Here's what to replace the guts with, instead of the g#s## command above: <pre> \ exe '%s:'. \ '\cCOPYRIGHT\s*\%((c)\|©\|&copy;\)\?\s*'. \ '\%([0-9]\{4}\(-[0-9]\{4\}\)\?,\s*\)*\zs'. \ '\('. \ '\%('.strftime("%Y").'\)\@![0-9]\{4\}'. \ '\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?'. \ '\&'. \ '\%([0-9]\{4\}-\)\?'. \ '\%('.(strftime("%Y")-1).'\)\@!'. \ '\%([0-9]\)\{4\}'. \ '\)'. \ '\ze\%(\%([0-9]\{4\}\)\@!.\)*$:'. \ '&, '.strftime("%Y").':e' | \ exe '%s:'. \ '\cCOPYRIGHT\s*\%((c)\|©\|&copy;\)\?\s*'. \ '\%([0-9]\{4}\%(-[0-9]\{4\}\)\?,\s*\)*\zs'. \ '\%('.strftime("%Y").'\)\@!\([0-9]\{4\}\)'. \ '\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?'. \ '\ze\%(\%([0-9]\{4\}\)\@!.\)*$:'. \ '\1-'.strftime("%Y").':e' | </pre> This makes two passes, one to update years that it needs a comma for, the next to update years it can use a hyphen for. ===First Pass (needs a comma)=== Summary: Replace all lines with a copyright notice, that do NOT end in the previous or current year, with a comma and the current year. *'''<code>%s:</code>''' - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity. *'''<code>\cCOPYRIGHT\s*\%((c)\|©\|&copy;\)\?\s*</code>''' - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method. *'''<code>\%({below}\)*</code>''' - match any number of year ranges followed by commas, but DO NOT use them for backreferences. :*'''<code>[0-9]\{4}\(-[0-9]\{4\}\)\?,\s*</code>''' - match a year or year range, followed by a comma and whitespace. *'''<code>\zs</code>''' - Place the "start of match" at this point. This means that any matched text previous to this point (i.e. the copyright flag, and any year-ranges followed by commas) will NOT be replaced with the replacement text. *'''<code>\({below}\&{below}\)</code>''' - The parentheses group two "concats" separated by the \&. All concats must match at the same place for the pattern to match. When it matches, it matches the final concat. :*'''<code>\%('.strftime("%Y").'\)\@![0-9]\{4\}</code>''' - match any year that is not the current year. :*'''<code>\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?</code>''' - optionally match any year range ending ("-{year}") to complete the first concat :*'''<code>\%([0-9]\{4\}-\)\?</code>''' - optionally match a year range beginning ("-{year}") :*'''<code>\%('.(strftime("%Y")-1).'\)\@!\%([0-9]\)\{4\}</code>''' - match any year except for the previous year (note, current year already known NOT to match) to complete the final concat *'''<code>\ze</code>''' - place the end of the match at this point, so that any text following this point is NOT affected by the replacement. *'''<code>\%(\%([0-9]\{4\}\)\@!.\)*$</code>''' - match any text that does not contain a year, until the end of the line. This ensures that we captured only the very last year in the line with our final concat, above. *'''<code>:&, '.strftime("%Y")</code>''' - replace with the entire match (i.e. the last concat above) followed by a comma and the current year. Recall that the match start and end were defined carefully so that only the desired text is replaced. *'''<code>:e</code>''' - suppress error when no match is found so you don't see error messages when your copyright is up-to-date, and to allow the mapping to continue on to the second pass... ===Second Pass (can use a hyphen)=== Summary: Replace all remaining lines with a copyright notice, that do NOT end in the current year (i.e. they end in the previous year), with a hyphen and the current year. *'''<code>%s:</code>''' - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity. *'''<code>\cCOPYRIGHT\s*\%((c)\|©\|&copy;\)\?\s*</code>''' - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method. *'''<code>\%({below}\)*</code>''' - match any number of year ranges followed by commas, but DO NOT use them for backreferences. :*'''<code>[0-9]\{4}\(-[0-9]\{4\}\)\?,\s*</code>''' - match a year or year range, followed by a comma and whitespace. *'''<code>\zs</code>''' - Place the "start of match" at this point. This means that any matched text previous to this point (i.e. the copyright flag, and any year-ranges followed by commas) will NOT be replaced with the replacement text. *'''<code>\%('.strftime("%Y").'\)\@!\([0-9]\{4\}\)</code>''' - match any year except for the current year, and place it in the first backreference (note the use of \%(\) in every previous grouping) *'''<code>\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?</code>''' - optionally match a year range ending that does NOT include the current year. *'''<code>\ze</code>''' - place the end of the match at this point, so that any text following this point is NOT affected by the replacement. *'''<code>\%(\%([0-9]\{4\}\)\@!.\)*$</code>''' - match any text that does not contain a year, until the end of the line. This ensures that we captured only the very last year in the line. *'''<code>\1-'.strftime("%Y")</code>''' - replace with the first backreference (i.e. the first year in the final year range of the line), followed by a hyphen and the current year. *'''<code>:e</code>''' - suppress error when no match is found so you don't see error messages whenever your copyright notice is up-to-date. ===References=== *{{help|/branch}} *{{help|/\zs}} *{{help|/\ze}} *{{help|/\%(\)}} *{{help|/\@!}}
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab