Vim Tips Wiki
(revising/correcting code and adding to/rewording the explanation of it)
(adding references and adding to command)
Line 5: Line 5:
 
autocmd BufWritePre *
 
autocmd BufWritePre *
 
\ if &modified |
 
\ if &modified |
\ exe "normal :g#\\c\\(^V169\\|©\\) \\?Copyright#s#\\(".strftime("%Y")."\\)\\@!\\([0-9]\\{4\\}\\)\\(-[0-9]\\{4\\}\\)\\?#\\2-".strftime("%Y")."^M`." |
+
\ exe "normal :g#\\c\\(^V169\\|©\\) \\?Copyright#s#\\(".strftime("%Y")."\\)\\@!\\([0-9]\\{4\\}\\)\\(-".strftime("%Y")."\\)\\@!\\(-[0-9]\\{4\\}\\)\\?#\\2-".strftime("%Y")."^M`." |
 
\ endif
 
\ endif
 
</pre>
 
</pre>
Line 15: Line 15:
 
*<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 &copy; Copyright 2007).
 
*<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 &copy; Copyright 2007).
 
*<tt><nowiki>\\([0-9]\\{4\\}\\)</nowiki></tt> - matches a 4-digit year and allows a backreference to it for use in the replace text.
 
*<tt><nowiki>\\([0-9]\\{4\\}\\)</nowiki></tt> - matches a 4-digit year and allows a backreference to it for use in the replace text.
  +
*<tt><nowiki>\\(-".strftime("%Y")."\\)\\@!</nowiki></tt> - don't match on up-to-date notices.
 
*<tt><nowiki>\\(-[0-9]\\{4\\}\\)\\?</nowiki></tt> - optionally match an ending year, e.g. the "-2006" in "&copy; 2000-2006".
 
*<tt><nowiki>\\(-[0-9]\\{4\\}\\)\\?</nowiki></tt> - optionally match an ending year, e.g. the "-2006" in "&copy; 2000-2006".
 
*<tt><nowiki>\\2-".strftime("%Y")</nowiki></tt> - replace the found text with the second backreference (first year in the copyright), a hyphen, and the current year.
 
*<tt><nowiki>\\2-".strftime("%Y")</nowiki></tt> - replace the found text with the second backreference (first year in the copyright), a hyphen, and the current year.
 
*<tt><nowiki>^M</nowiki></tt> - complete execution of command by simulating an [ENTER] keypress. Make sure to replace this with an actual &lt;CTRL-M&gt; character.
 
*<tt><nowiki>^M</nowiki></tt> - complete execution of command by simulating an [ENTER] keypress. Make sure to replace this with an actual &lt;CTRL-M&gt; character.
 
*<tt><nowiki>`.</nowiki></tt> - return the cursor to the last modified position. This makes it so the cursor will not jump to the copyright notice every time you write the file - only when notice changes. Marks could be used instead to always jump back to the exact cursor position even when the notice changes.
 
*<tt><nowiki>`.</nowiki></tt> - return the cursor to the last modified position. This makes it so the cursor will not jump to the copyright notice every time you write the file - only when notice changes. Marks could be used instead to always jump back to the exact cursor position even when the notice changes.
  +
  +
==References==
  +
*{{help|strftime()}}
  +
*{{help|/\@!}}
  +
*{{help|`.}}
   
 
==Comments==
 
==Comments==

Revision as of 15:50, 19 October 2007

Especially when editing source code, there is often a copyright notice embedded in the file. Insert the following in a vimrc file to automatically update this copyright notice in ALL FILES when writing them (note that ^V must be replaced by an actual CTRL-V character, or CTRL-Q for the default Windows mapping, and ^M must be replaced by an actual CTRL-M character):

" Automatically update copyright notice with current year
autocmd BufWritePre * 
      \ if &modified |
      \   exe "normal :g#\\c\\(^V169\\|&copy;\\) \\?Copyright#s#\\(".strftime("%Y")."\\)\\@!\\([0-9]\\{4\\}\\)\\(-".strftime("%Y")."\\)\\@!\\(-[0-9]\\{4\\}\\)\\?#\\2-".strftime("%Y")."^M`." |
      \ endif

This replaces yyyy or yyyy-yyyy with yyyy-<current year> on any line in the file containing "© copyright" or "&copy; copyright" with or without the space in a case-insensitive manner. It only does this if the file has been modified.

An explaination of the command string follows:

  • g#\\c\\(^V169\\|&copy;\\) \\?Copyright#s# - search and replace only on a line matching case-insensitive copyright notice patten. '#' is used rather than the customary '/' to avoid confusion with all the '\' characters. The ^V169 is to insert a © symbol. Make sure to replace the ^V with an actual <CTRL-V> character. Rewrite this section to include your own copyright line format if it differs.
  • \\(".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 year and allows a backreference to it for use in the replace text.
  • \\(-".strftime("%Y")."\\)\\@! - don't match on up-to-date notices.
  • \\(-[0-9]\\{4\\}\\)\\? - optionally match an ending year, e.g. the "-2006" in "© 2000-2006".
  • \\2-".strftime("%Y") - replace the found text with the second backreference (first year in the copyright), a hyphen, and the current year.
  • ^M - complete execution of command by simulating an [ENTER] keypress. Make sure to replace this with an actual <CTRL-M> character.
  • `. - return the cursor to the last modified position. This makes it so the cursor will not jump to the copyright notice every time you write the file - only when notice changes. Marks could be used instead to always jump back to the exact cursor position even when the notice changes.

References

Comments