Vim Tips Wiki
(Insert subpage argument (where new tip discussed))
(Remove html character entities)
Line 25: Line 25:
   
 
An explanation of the command string follows:
 
An explanation of the command string follows:
*'''<tt><nowiki>g#</nowiki></tt>''' - search and replace only on a line matching copyright notice patten. '#' is used rather than the customary '/' to avoid confusion with all the '\' characters.
+
*'''<tt>g#</tt>''' - search and replace only on a line matching copyright notice patten. '#' is used rather than the customary '/' to avoid confusion with all the '\' characters.
:*'''<tt><nowiki>\\cCOPYRIGHT </nowiki></tt>''' - match on text COPYRIGHT followed by a space (case-insensitive)
+
:*'''<tt>\\cCOPYRIGHT </tt>''' - match on text COPYRIGHT followed by a space (case-insensitive)
:*'''<tt><nowiki>\\(".strftime("%Y")."\\)\\@!</nowiki></tt>''' - only match when the entire match does not consist of the current year (i.e. don't update a notice containing only the current year, like "Copyright 2007").
+
:*'''<tt>\\(".strftime("%Y")."\\)\\@!</tt>''' - only match when the entire match does not consist of the current year (i.e. don't update a notice containing only the current year, like "Copyright 2007").
:*'''<tt><nowiki>[0-9]\\{4\\}</nowiki></tt>''' - matches a 4-digit number for the first year in the notice
+
:*'''<tt>[0-9]\\{4\\}</tt>''' - matches a 4-digit number for the first year in the notice
:*'''<tt><nowiki>\\(-".strftime("%Y")."\\)\\@!</nowiki></tt>''' - don't match on up-to-date notices
+
:*'''<tt>\\(-".strftime("%Y")."\\)\\@!</tt>''' - don't match on up-to-date notices
*'''<tt><nowiki>#s#</nowiki></tt>''' - search and replace the outdated year
+
*'''<tt>#s#</tt>''' - search and replace the outdated year
:*'''<tt><nowiki>\\([0-9]\\{4\\}\\)</nowiki></tt>''' - match a 4-digit year and give it a backreference
+
:*'''<tt>\\([0-9]\\{4\\}\\)</tt>''' - match a 4-digit year and give it a backreference
:*'''<tt><nowiki>\\(-[0-9]\\{4\\}\\)\\?</nowiki></tt>''' - optionally match an ending year, e.g. the "-2006" in "copyright 2000-2006".
+
:*'''<tt>\\(-[0-9]\\{4\\}\\)\\?</tt>''' - optionally match an ending year, e.g. the "-2006" in "copyright 2000-2006".
:*'''<tt><nowiki>#\\1-".strftime("%Y")</nowiki></tt>''' - replace the found text with the first year in the copyright, a hyphen, and the current year
+
:*'''<tt>#\\1-".strftime("%Y")</tt>''' - replace the found text with the first year in the copyright, a hyphen, and the current year
   
 
This script will "jump" to the copyright notice whenever it gets updated. If this is not desired, you can put in a command before the g#s# to create a mark and a command after it to jump back to the mark.
 
This script will "jump" to the copyright notice whenever it gets updated. If this is not desired, you can put in a command before the g#s# to create a mark and a command after it to jump back to the mark.
Line 50: Line 50:
 
<pre>
 
<pre>
 
\ exe '%s:'.
 
\ exe '%s:'.
\ '\cCOPYRIGHT\s*\%((c)\|&amp;copy;\|&copy;\)\?\s*'.
+
\ '\cCOPYRIGHT\s*\%((c)\|&copy;\|&copy;\)\?\s*'.
 
\ '\%([0-9]\{4}\(-[0-9]\{4\}\)\?,\s*\)*\zs'.
 
\ '\%([0-9]\{4}\(-[0-9]\{4\}\)\?,\s*\)*\zs'.
 
\ '\('.
 
\ '\('.
Line 63: Line 63:
 
\ '&, '.strftime("%Y").':e' |
 
\ '&, '.strftime("%Y").':e' |
 
\ exe '%s:'.
 
\ exe '%s:'.
\ '\cCOPYRIGHT\s*\%((c)\|&amp;copy;\|&copy;\)\?\s*'.
+
\ '\cCOPYRIGHT\s*\%((c)\|&copy;\|&copy;\)\?\s*'.
 
\ '\%([0-9]\{4}\%(-[0-9]\{4\}\)\?,\s*\)*\zs'.
 
\ '\%([0-9]\{4}\%(-[0-9]\{4\}\)\?,\s*\)*\zs'.
 
\ '\%('.strftime("%Y").'\)\@!\([0-9]\{4\}\)'.
 
\ '\%('.strftime("%Y").'\)\@!\([0-9]\{4\}\)'.
Line 75: Line 75:
 
===First Pass (needs a comma)===
 
===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.
 
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.
*'''<tt><nowiki>%s:</nowiki></tt>''' - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity.
+
*'''<tt>%s:</tt>''' - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity.
*'''<tt><nowiki>\cCOPYRIGHT\s*\%((c)\|&amp;copy;\|&copy;\)\?\s*</nowiki></tt>''' - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method.
+
*'''<tt>\cCOPYRIGHT\s*\%((c)\|&copy;\|&copy;\)\?\s*</tt>''' - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method.
*'''<tt><nowiki>\%({below}\)*</nowiki></tt>''' - match any number of year ranges followed by commas, but DO NOT use them for backreferences.
+
*'''<tt>\%({below}\)*</tt>''' - match any number of year ranges followed by commas, but DO NOT use them for backreferences.
:*'''<tt><nowiki>[0-9]\{4}\(-[0-9]\{4\}\)\?,\s*</nowiki></tt>''' - match a year or year range, followed by a comma and whitespace.
+
:*'''<tt>[0-9]\{4}\(-[0-9]\{4\}\)\?,\s*</tt>''' - match a year or year range, followed by a comma and whitespace.
*'''<tt><nowiki>\zs</nowiki></tt>''' - 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.
+
*'''<tt>\zs</tt>''' - 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.
*'''<tt><nowiki>\({below}\&{below}\)</nowiki></tt>''' - 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.
+
*'''<tt>\({below}\&{below}\)</tt>''' - 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.
:*'''<tt><nowiki>\%('.strftime("%Y").'\)\@![0-9]\{4\}</nowiki></tt>''' - match any year that is not the current year.
+
:*'''<tt>\%('.strftime("%Y").'\)\@![0-9]\{4\}</tt>''' - match any year that is not the current year.
:*'''<tt><nowiki>\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?</nowiki></tt>''' - optionally match any year range ending ("-{year}") to complete the first concat
+
:*'''<tt>\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?</tt>''' - optionally match any year range ending ("-{year}") to complete the first concat
:*'''<tt><nowiki>\%([0-9]\{4\}-\)\?</nowiki></tt>''' - optionally match a year range beginning ("-{year}")
+
:*'''<tt>\%([0-9]\{4\}-\)\?</tt>''' - optionally match a year range beginning ("-{year}")
:*'''<tt><nowiki>\%('.(strftime("%Y")-1).'\)\@!\%([0-9]\)\{4\}</nowiki></tt>''' - match any year except for the previous year (note, current year already known NOT to match) to complete the final concat
+
:*'''<tt>\%('.(strftime("%Y")-1).'\)\@!\%([0-9]\)\{4\}</tt>''' - match any year except for the previous year (note, current year already known NOT to match) to complete the final concat
*'''<tt><nowiki>\ze</nowiki></tt>''' - place the end of the match at this point, so that any text following this point is NOT affected by the replacement.
+
*'''<tt>\ze</tt>''' - place the end of the match at this point, so that any text following this point is NOT affected by the replacement.
*'''<tt><nowiki>\%(\%([0-9]\{4\}\)\@!.\)*$</nowiki></tt>''' - 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.
+
*'''<tt>\%(\%([0-9]\{4\}\)\@!.\)*$</tt>''' - 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.
*'''<tt><nowiki>:&, '.strftime("%Y")</nowiki></tt>''' - 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.
+
*'''<tt>:&, '.strftime("%Y")</tt>''' - 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.
*'''<tt><nowiki>:e</nowiki></tt>''' - 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...
+
*'''<tt>:e</tt>''' - 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)===
 
===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.
 
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.
*'''<tt><nowiki>%s:</nowiki></tt>''' - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity.
+
*'''<tt>%s:</tt>''' - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity.
*'''<tt><nowiki>\cCOPYRIGHT\s*\%((c)\|&amp;copy;\|&copy;\)\?\s*</nowiki></tt>''' - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method.
+
*'''<tt>\cCOPYRIGHT\s*\%((c)\|&copy;\|&copy;\)\?\s*</tt>''' - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method.
*'''<tt><nowiki>\%({below}\)*</nowiki></tt>''' - match any number of year ranges followed by commas, but DO NOT use them for backreferences.
+
*'''<tt>\%({below}\)*</tt>''' - match any number of year ranges followed by commas, but DO NOT use them for backreferences.
:*'''<tt><nowiki>[0-9]\{4}\(-[0-9]\{4\}\)\?,\s*</nowiki></tt>''' - match a year or year range, followed by a comma and whitespace.
+
:*'''<tt>[0-9]\{4}\(-[0-9]\{4\}\)\?,\s*</tt>''' - match a year or year range, followed by a comma and whitespace.
*'''<tt><nowiki>\zs</nowiki></tt>''' - 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.
+
*'''<tt>\zs</tt>''' - 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.
*'''<tt><nowiki>\%('.strftime("%Y").'\)\@!\([0-9]\{4\}\)</nowiki></tt>''' - match any year except for the current year, and place it in the first backreference (note the use of \%(\) in every previous grouping)
+
*'''<tt>\%('.strftime("%Y").'\)\@!\([0-9]\{4\}\)</tt>''' - match any year except for the current year, and place it in the first backreference (note the use of \%(\) in every previous grouping)
*'''<tt><nowiki>\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?</nowiki></tt>''' - optionally match a year range ending that does NOT include the current year.
+
*'''<tt>\%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\?</tt>''' - optionally match a year range ending that does NOT include the current year.
*'''<tt><nowiki>\ze</nowiki></tt>''' - place the end of the match at this point, so that any text following this point is NOT affected by the replacement.
+
*'''<tt>\ze</tt>''' - place the end of the match at this point, so that any text following this point is NOT affected by the replacement.
*'''<tt><nowiki>\%(\%([0-9]\{4\}\)\@!.\)*$</nowiki></tt>''' - 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.
+
*'''<tt>\%(\%([0-9]\{4\}\)\@!.\)*$</tt>''' - 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.
*'''<tt><nowiki>\1-'.strftime("%Y")</nowiki></tt>''' - 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.
+
*'''<tt>\1-'.strftime("%Y")</tt>''' - 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.
*'''<tt><nowiki>:e</nowiki></tt>''' - suppress error when no match is found so you don't see error messages whenever your copyright notice is up-to-date.
+
*'''<tt>:e</tt>''' - suppress error when no match is found so you don't see error messages whenever your copyright notice is up-to-date.
   
 
===References===
 
===References===

Revision as of 00:25, 30 September 2008

Tip 1521 Printable Monobook Previous Next

created October 9, 2007 · complexity advanced · author Fritzophrenic · version 7.0


Especially when editing source code, there is often a copyright notice embedded in the file. Insert one of the following in a vimrc file to automatically update this copyright notice in ALL FILES when writing them.

Simple Copyright Notices

" Automatically update copyright notice with current year
autocmd BufWritePre *
  \ if &modified |
  \   exe "g#\\cCOPYRIGHT \\(".strftime("%Y")."\\)\\@![0-9]\\{4\\}\\(-".strftime("%Y")."\\)\\@!#s#\\([0-9]\\{4\\}\\)\\(-[0-9]\\{4\\}\\)\\?#\\1-".strftime("%Y") |
  \ endif

This replaces yyyy or yyyy-yyyy with yyyy-<current year> on any line in the file containing an outdated "COPYRIGHT yyyy" notice (case-insensitive). It only does this if the file has been modified.

An explanation of the command string follows:

  • g# - search and replace only on a line matching copyright notice patten. '#' is used rather than the customary '/' to avoid confusion with all the '\' characters.
  • \\cCOPYRIGHT - match on text COPYRIGHT followed by a space (case-insensitive)
  • \\(".strftime("%Y")."\\)\\@! - only match when the entire match does not consist of the current year (i.e. don't update a notice containing only the current year, like "Copyright 2007").
  • [0-9]\\{4\\} - matches a 4-digit number for the first year in the notice
  • \\(-".strftime("%Y")."\\)\\@! - don't match on up-to-date notices
  • #s# - search and replace the outdated year
  • \\([0-9]\\{4\\}\\) - match a 4-digit year and give it a backreference
  • \\(-[0-9]\\{4\\}\\)\\? - optionally match an ending year, e.g. the "-2006" in "copyright 2000-2006".
  • #\\1-".strftime("%Y") - replace the found text with the first year in the copyright, a hyphen, and the current year

This script will "jump" to the copyright notice whenever it gets updated. If this is not desired, you can put in a command before the g#s# to create a mark and a command after it to jump back to the mark.

References

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:

          \   exe '%s:'.
          \       '\cCOPYRIGHT\s*\%((c)\|©\|©\)\?\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)\|©\|©\)\?\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' |

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.

  • %s: - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity.
  • \cCOPYRIGHT\s*\%((c)\|©\|©\)\?\s* - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method.
  • \%({below}\)* - match any number of year ranges followed by commas, but DO NOT use them for backreferences.
  • [0-9]\{4}\(-[0-9]\{4\}\)\?,\s* - match a year or year range, followed by a comma and whitespace.
  • \zs - 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.
  • \({below}\&{below}\) - 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.
  • \%('.strftime("%Y").'\)\@![0-9]\{4\} - match any year that is not the current year.
  • \%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\? - optionally match any year range ending ("-{year}") to complete the first concat
  • \%([0-9]\{4\}-\)\? - optionally match a year range beginning ("-{year}")
  • \%('.(strftime("%Y")-1).'\)\@!\%([0-9]\)\{4\} - match any year except for the previous year (note, current year already known NOT to match) to complete the final concat
  • \ze - place the end of the match at this point, so that any text following this point is NOT affected by the replacement.
  • \%(\%([0-9]\{4\}\)\@!.\)*$ - 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.
  • :&, '.strftime("%Y") - 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.
  • :e - 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.

  • %s: - start a "replace in all lines" search, using ':' rather than the customary '/' for clarity.
  • \cCOPYRIGHT\s*\%((c)\|©\|©\)\?\s* - find lines containing the copyright flag (copyright {optional symbol}) as in the simple method.
  • \%({below}\)* - match any number of year ranges followed by commas, but DO NOT use them for backreferences.
  • [0-9]\{4}\(-[0-9]\{4\}\)\?,\s* - match a year or year range, followed by a comma and whitespace.
  • \zs - 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.
  • \%('.strftime("%Y").'\)\@!\([0-9]\{4\}\) - match any year except for the current year, and place it in the first backreference (note the use of \%(\) in every previous grouping)
  • \%(-'.strftime("%Y").'\)\@!\%(-[0-9]\{4\}\)\? - optionally match a year range ending that does NOT include the current year.
  • \ze - place the end of the match at this point, so that any text following this point is NOT affected by the replacement.
  • \%(\%([0-9]\{4\}\)\@!.\)*$ - 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.
  • \1-'.strftime("%Y") - 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.
  • :e - suppress error when no match is found so you don't see error messages whenever your copyright notice is up-to-date.

References

Take-Away Lessons

  • Use the 'g' command or \zs and \ze atoms to carefully limit your 's' commands to certain lines, and to simplify your replacement text for 's' commands.
  • Use \%(\) for grouping whenever you don't need the group for a back-reference to simplify things and provide some speedup.
  • Zero-width matches such as \@! are very powerful - familiarize yourself with them.
  • Complicated regular expressions can be made far less complex by splitting them into sections and working on each section independently.
  • Familiarize yourself with the definition of a pattern (:help /pattern) and its constituent components (like branches - :help /branch). Using these components fully can simplify your patterns or even make possible things that are not possible otherwise.

See also

Comments