Today, when studying the autocmd command of vim, we can see the final result of ": help auto". The help document provides an example: the current date and time are automatically inserted when a *. html file is written. As follows:
Autocmd BufWritePre, FileWritePre *. html ks | call LastMod () |'s
Fun LastMod ()
If line ("$")> 20
Let l = 20
Else
Let l = line ("$ ")
Endif
Exe "1,". l. "g/Last modified:/s/Last modified:. */Last modified :".
\ Strftime ("% Y % B % d"
)
Endfun
The following code is explained:
To work with this code, you need to include this line of "Last modified: <date" in the first 20 rows of the file.
Time> ". Vim replaces <date time> (including any content after the row) with the current date and time.
Explanation:
Ks saves the current location to the 's' flag
Call LastMod () to call the LastMod () function to complete the work
'S cursor back to the old position
The LastMode () function first checks whether the file contains less than 20 lines, and then uses the ": g" command to find
Modified. Execute the ": s" command on these lines to replace the existing time with the current time.
The ": execute" command enables the ": g" and ": s" commands to use expressions. Use the strftime () function to retrieve dates
. It can use other parameters to obtain date strings of different formats.
I was inspired to see that my vim configuration file _ vimrc has been frequently changed over the past few days. After each modification, I have to manually modify the last modification time of the file header, why not use this code to free yourself? Copy the above code to my _ vimrc and change the first sentence:
Autocmd BufWritePre, FileWritePre _ vimrc ks | call LastMod () |'s
Change the last line of the file header modification time to the following:
"Last modified: <date time>
Save, restart vim, open _ vimrc, run: w <CR>, and this line will automatically become:
"Last modified: 2007 August
Well, there is a date, and there is still a short time. Naturally, I want to go to the LastMod () function, and I was surprised to find that:
Exe "1,". l. "g/Last modified:/s/Last modified:. */Last modified :".
\ Strftime ("% Y % B % d ")
Changed
Exe "1,". l. "g/Last modified: 2007 may September 06
\ Strftime ("% Y % B % d ")
After a bit of thinking, I understand that this function also replaces the Last modified: In this sentence when saving the automatic insertion time in the first 20 rows.
The simplest solution is to get this function 20 rows later. However, if there are only a few configuration items in the configuration file, I have to add several blank lines before the function. There is a big problem with this example function. So I decided to modify it without any restrictions.
First, let's analyze the key sentence in the function:
Exe "1,". l. "g/Last modified:/s/Last modified:. */Last modified :".
\ Strftime ("% Y % B % d ")
After the above if statement, the variable L has changed to the number of rows in the _ vimrc file (if the number of rows is less than 20) or 20. Then, from the first line to the first line, use the g command to find a line that matches the regular expression "/Last modified, run the s command to associate it with the regular expression "/Last modified :. */Replace the matched content with "Last modified: current date". The strftime function returns the current date.
The number of rows is exactly within the range of 1st to L, and this sentence exactly matches "/Last modified :. */". Therefore, except for the line we want to replace, this line (exe ...) also replaced.
If you want to think about it, you must use another expression of "g/Last modified:/" to avoid matching yourself. The next two "Last modified" will also need to be changed, try again. Finally, you can:
Exe "1,". l. "g/[L] ast modified:/s/[L] ast modified:. */Last modified: [do not use spaces here]".
\ Strftime ("[Add a space here] % Y % B % d ")
Later, I found that the g command was redundant (I arbitrarily used it, but it was actually useful. For details, see the comments on the first floor). In addition to the time, it eventually became like this:
Exe "1,". l. "s/[L] astModified:. */LastModified :".
\ Strftime ("% Y % B % d % X ")
In this way, this function does not have the row restrictions, so the above if statement should also be changed, and the function will eventually become like this (the function name has also been changed by the way ):
Un LastModified ()
Let l = line ("$ ")
Exe "1,". l. "s/[L] astModified:. */LastModified :".
\ Strftime ("% Y % B % d % X ")
Endfun
Is it much simpler and more scalable than the original one, and loose coupling.
The regular expression goes smoothly, and the autocmd In the first sentence is also changed:
Autocmd BufWritePre, FileWritePre [. _] vimrc ks | call LastModified () |'s
In this way, whether it is. vimrc or _ vimrc, the modification date and time can be automatically inserted during storage.
The final result is as follows: (comment on the 9th floor is the final result)
Autocmd BufWritePre, FileWritePre [. _] vimrc ks | call LastModified () |'s
Fun LastModified ()
Let l = line ("$ ")
Exe "1,". l ."G/[L] ast modified :/S/[L] astModified:. */LastModified :".
\ Strftime ("% Y % B % d % X ")
Endfun
PS: In case, (one in 10 thousand, of course, the probability is very small) LastModified appears in other places of the file, and that line also happens with the regular expression/[L] astModified :. */match, you can proceed with caution. Pai_^