Thursday, September 15, 2011

Re: Automatically insert something when open a file

On 09/15/11 08:25, Stanley Rice wrote:
> I have write some code that can change the modified data. I
> search for the first 10 line of the current file, and search
> for the key word "Modified: ", and then replace the content
> after the keyword with current time stamp each time I save the
> file. I thought it's amazing.
>
> However, I have to write the same line "Modified: " every time
> I edit a new file. what a boring and cumbersome job! So, is
> there any ways to insert the specific line to the newly edited
> file, thought writing some code in .vimrc?

While untested, I suspect it would look something like

autocmd BufWritePre myfile.txt
%s/Modified:\zs.*/\=strftime("%c")/e

which limits it only to the filespec you want (in this case
"myfile.txt" but you could do a glob like "my*template.txt"). It
then performs a substitute on the whole file anywhere
"Modified:..." is found and replaces the stuff after "Modified:"
with the current timestamp.

:help autocmd
:h BufWritePre
:h sub-replace-expression
:h strftime()

-tim

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: