inserting a date on a file...

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

inserting a date on a file...

jose isaias cabrera-2

Greetings!

I would like to insert a date in a file everytime I open the file for
editing.  For example, in all of my code I keep track of the date
changes, since I rsync to a server and I would like to keep the dates of
the updates.  Is there a command that would do this?  Or is there an
easy way?  What I would love is to have a string on the text file, ie.

@DATE

which would turn into the date and time when open for editing, and then,
add another @DATE for the next editing session.

Ideas?

thanks,

jic


Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

Tim Chase-2
> I would like to insert a date in a file everytime I open the
> file for editing.  For example, in all of my code I keep track
> of the date changes, since I rsync to a server and I would
> like to keep the dates of the updates.  Is there a command
> that would do this?  Or is there an easy way?  What I would
> love is to have a string on the text file, ie.
>
> @DATE
>
> which would turn into the date and time when open for editing,
> and then, add another @DATE for the next editing session.

Well, the first thing that occurs to me would be to have an
autocmd that does the following:

        :g/@DATE/put =strftime('%c')

autocmd BufReadPost filename.txt g/@DATE/put =strftime('%c')

where "filename.txt" is something to match the text you want.
This will put the current timestamp below the @DATE line.

You can change the format as described in ":help strftime" and
you can put it before the @DATE line if you like, using "put!"
instead of "put".

This will expand *all* the "@DATE" items in the file.  You can
get just the first one with

autocmd BufReadPost filename.txt $/@DATE/put =strftime('%c')

(assuming you have 'wrapscan' set)  If the file doesn't have the
"@DATE" marker, it will give you an error, but not insert the date.

For more, check out the magic of auto-commands, "put", and strftime:
        :help autocmd
        :he strftime
        :he :put
        :he 'wrapscan'

Hope this gives you some ideas to run with.

-tim




Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

henrik-10
In reply to this post by jose isaias cabrera-2
you can do somehting like

:r !date

to get from the unix-command line manually. How to do it automatically
while opening the file i do not know :(


-Henrik



On Wed, 2005-07-20 at 10:53 -0400, jose isaias cabrera wrote:

> Greetings!
>
> I would like to insert a date in a file everytime I open the file for
> editing.  For example, in all of my code I keep track of the date
> changes, since I rsync to a server and I would like to keep the dates of
> the updates.  Is there a command that would do this?  Or is there an
> easy way?  What I would love is to have a string on the text file, ie.
>
> @DATE
>
> which would turn into the date and time when open for editing, and then,
> add another @DATE for the next editing session.
>
> Ideas?
>
> thanks,
>
> jic
>

Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

A.J.Mechelynck
In reply to this post by jose isaias cabrera-2
----- Original Message -----
From: "jose isaias cabrera" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, July 20, 2005 4:53 PM
Subject: inserting a date on a file...


>
> Greetings!
>
> I would like to insert a date in a file everytime I open the file for
> editing.  For example, in all of my code I keep track of the date
> changes, since I rsync to a server and I would like to keep the dates of
> the updates.  Is there a command that would do this?  Or is there an
> easy way?  What I would love is to have a string on the text file, ie.
>
> @DATE
>
> which would turn into the date and time when open for editing, and then,
> add another @DATE for the next editing session.
>
> Ideas?
>
> thanks,
>
> jic

You could have a line with a fixed first part followed by the date, for
instance:

(in a text file)
    Last updated:    25 December 2004, 00:00 GMT

(in a C file)

/* Last change: 2005-07-17 17:08 +0200

You could write a small function which would be called by the BufWritePre
autocommand event, would look into the first (some number of) lines in the
file, and if the line was found it would replace its latter part by the
current date and time.

You would have to determine the format of the date header and the format of
the date itself.

See:
    :help autocmd.txt
    :help BufWritePre
    :help :while
    :help getline()
    :help strftime()

Here is a rough (untested) example:

    function SetDateStamp(lines, string, format)
        let l:lines = (a:lines < 1 ? 5 : a:lines)
        let l:string = (a:string == "" ? "Last updated:" : a:string)
        let l:format = (a:format == "" ? "%c" : a:format)
        let line = 1
        while line <= l:lines
            let text = getline(line)
            if text =~ l:string
                let col = stridx(text, l:string) + strlen(l:string) + 1
                call cursor(line, col)
                exe "normal i" . "\<Tab>" . strftime(l:format) .
"\<S-End>\<CR>\<Esc>"
                return 0
            endif
            let line = line + 1
        endwhile
        return 1
    endfunction
    autocmd BufWritePre * call SetDateStamp(10, "", "")

For more detailed handling, you could test the return value of the function
(0 if OK, 1 if line not found).


HTH,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

Gary Johnson
In reply to this post by jose isaias cabrera-2
On 2005-07-20, jose isaias cabrera <[hidden email]> wrote:

> Greetings!
>
> I would like to insert a date in a file everytime I open the file for
> editing.  For example, in all of my code I keep track of the date
> changes, since I rsync to a server and I would like to keep the dates of
> the updates.  Is there a command that would do this?  Or is there an
> easy way?  What I would love is to have a string on the text file, ie.
>
> @DATE
>
> which would turn into the date and time when open for editing, and then,
> add another @DATE for the next editing session.
>
> Ideas?

Here's what I use for my vim scripts, which all have a line in the
header of the form:

" Last Change: 2005-02-15 01:51:24

--------------------------------------------------------------------
augroup vim
    au!
    au BufWrite *.vim call s:VimUpdate()
augroup END

if !exists("*s:VimUpdate")
    function! s:VimUpdate()
        " Save cursor and edit window positions:
        "
        let restorepos = line(".") . "normal!" . virtcol(".") . "|"
        normal H
        let restoretop = line(".") . "normal!zt"

        " Update time of last modification:
        "     ISO 8601 date/time format:
        silent execute '1,$g/^\" Last Change:/s/:.*/:\t'
            \ . strftime("%Y-%m-%d %H:%M:%S") . '/'

        " Restore edit window and cursor positions:
        "
        execute restoretop
        execute restorepos
    endfunction
endif
--------------------------------------------------------------------

I put this in the file ~/.vim/after/ftplugin/vim.vim.

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

jose isaias cabrera-2
On 11:28 Wed 20 Jul     , Gary Johnson wrote:

> On 2005-07-20, jose isaias cabrera <[hidden email]> wrote:
> > Greetings!
> >
> > I would like to insert a date in a file everytime I open the file for
> > editing.  For example, in all of my code I keep track of the date
> > changes, since I rsync to a server and I would like to keep the dates of
> > the updates.  Is there a command that would do this?  Or is there an
> > easy way?  What I would love is to have a string on the text file, ie.
> >
> > @DATE
> >
> > which would turn into the date and time when open for editing, and then,
> > add another @DATE for the next editing session.
> >
> > Ideas?
>
> Here's what I use for my vim scripts, which all have a line in the
> header of the form:
>
> " Last Change: 2005-02-15 01:51:24
>
> --------------------------------------------------------------------
> augroup vim
>     au!
>     au BufWrite *.vim call s:VimUpdate()
> augroup END
>
> if !exists("*s:VimUpdate")
>     function! s:VimUpdate()
> " Save cursor and edit window positions:
> "
> let restorepos = line(".") . "normal!" . virtcol(".") . "|"
> normal H
> let restoretop = line(".") . "normal!zt"
>
> " Update time of last modification:
>         "     ISO 8601 date/time format:
> silent execute '1,$g/^\" Last Change:/s/:.*/:\t'
>    \ . strftime("%Y-%m-%d %H:%M:%S") . '/'
>
> " Restore edit window and cursor positions:
> "
> execute restoretop
> execute restorepos
>     endfunction
> endif
> --------------------------------------------------------------------
>
> I put this in the file ~/.vim/after/ftplugin/vim.vim.

Thanks Gary.

So, if I put this on the perl.vim and make the appropiate changes, it
would change all of the perl scripts that I edit?  One more question:
What if I want to keep the old "Last Change:" as history and insert a
new one above it, I know that I would have to perhaps add one more line
to the restoretop, correct?  Or even better, what if I wanted to just
insert that Last Change without replacing on, say, line 3, how would the
silent execute change?

thanks,

jic


Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

Gary Johnson
On 2005-07-20, jose isaias cabrera <[hidden email]> wrote:
> On 11:28 Wed 20 Jul     , Gary Johnson wrote:

> > Here's what I use for my vim scripts, which all have a line in the
> > header of the form:

[...]

> Thanks Gary.

You're welcome.

> So, if I put this on the perl.vim and make the appropiate changes, it
> would change all of the perl scripts that I edit?

Yes.

>                                                    One more question:
> What if I want to keep the old "Last Change:" as history and insert a
> new one above it, I know that I would have to perhaps add one more line
> to the restoretop, correct?

All the restoretop does is restore your window to the same position
in the buffer as it was before you wrote the buffer.  This, along
with restorepos, allows you to save your work and to time-stamp it
as it's saved without having your view of the buffer jerked to the
"Last Change:" line.  So you shouldn't need to change restoretop.

There are a number of ways to insert a new line above the first
"Last Change:" line.  I would probably replace the existing "silent
execute" command by a sequence of commands:

1.  Move the cursor to the first line of the file.
2.  Use the search() function to find the first "Last Change:" line.
3.  If such a line is found, use the :put!= command to insert a new
    "Last Change:" line.

I'll leave that as an exercise for the reader.

>                              Or even better, what if I wanted to just
> insert that Last Change without replacing on, say, line 3, how would the
> silent execute change?

        silent execute '3s/^/\" Last Change:\t'
            \ . strftime("%Y-%m-%d %H:%M:%S") . '\r/'

This gets rid of the global command and simply executes a substitute
command on line 3.  The "\r" at the end of the replacement string
adds a newline in front of the current line 3, making it line 4.

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: inserting a date on a file...

Lars Wilke
In reply to this post by jose isaias cabrera-2
* jose isaias cabrera wrote:
> I would like to insert a date in a file everytime I open the file for
> editing.  For example, in all of my code I keep track of the date

Just have a look at vim.org and search the scripts there for timestamp.
I bet you'll find a handfull of scripts which do that.

   --lars
Reply | Threaded
Open this post in threaded view
|

vim function magic for HTML / XML

John (Eljay) Love-Jensen
In reply to this post by Gary Johnson
Hi everyone,

I was trying to hack a little helper Vim function, let's call it "VisualTagger" and vnoremap, but alas...

My goal is to bind the function to, say, the <F8> key.

The desired function works in Visual Mode.

Processing steps of the desired function...
1. Visual Mode highlight desired selection.
2. press <F8> (or whatever key bound to our desired magic function)
*3. enter text at the prompt, such as:  b class="rainbow"<CR>
*4. the text "<b class="rainbow">" (without the outer quotes) would be inserted before the visual block.
*5. the text "</b>" (without quotes) would be inserted after the visual block.

The * indicate the VisualTagger function.

I'm having trouble figuring out how to access the Visual Mode block marks (or any marks, for that matter) from within a function.

And although I haven't gotten to this point yet, I figure I'll have problems figuring out how to prompt for arbitrary text to use in the function.  (I was starting simple, by trying to insert <b> at the beginning Visual Mode block mark, and </b> at  the end.)

I presume this has already been done, or maybe is part of Vim proper... my padawan Google-fu has failed me.  No need to reinvent the wheel.

Thanks,
--Eljay

PS:  I have looked through http://www.vim.org/scripts/script.php?script_id=72 from Benji Fisher.  Lots of good Vim voodoo, but I didn't find my Vim muse to inspire me, and overcome my function-writer's block.

Reply | Threaded
Open this post in threaded view
|

Re: vim function magic for HTML / XML

A.J.Mechelynck
----- Original Message -----
From: "Eljay Love-Jensen" <[hidden email]>
To: <[hidden email]>
Sent: Thursday, July 21, 2005 6:14 PM
Subject: vim function magic for HTML / XML


> Hi everyone,
>
> I was trying to hack a little helper Vim function, let's call it
> "VisualTagger" and vnoremap, but alas...
>
> My goal is to bind the function to, say, the <F8> key.
>
> The desired function works in Visual Mode.
>
> Processing steps of the desired function...
> 1. Visual Mode highlight desired selection.
> 2. press <F8> (or whatever key bound to our desired magic function)
> *3. enter text at the prompt, such as:  b class="rainbow"<CR>
> *4. the text "<b class="rainbow">" (without the outer quotes) would be
> inserted before the visual block.
> *5. the text "</b>" (without quotes) would be inserted after the visual
> block.
>
> The * indicate the VisualTagger function.
>
> I'm having trouble figuring out how to access the Visual Mode block marks
> (or any marks, for that matter) from within a function.
>
> And although I haven't gotten to this point yet, I figure I'll have
> problems figuring out how to prompt for arbitrary text to use in the
> function.  (I was starting simple, by trying to insert <b> at the
> beginning Visual Mode block mark, and </b> at  the end.)
>
> I presume this has already been done, or maybe is part of Vim proper... my
> padawan Google-fu has failed me.  No need to reinvent the wheel.
>
> Thanks,
> --Eljay
>
> PS:  I have looked through
> http://www.vim.org/scripts/script.php?script_id=72 from Benji Fisher.
> Lots of good Vim voodoo, but I didn't find my Vim muse to inspire me, and
> overcome my function-writer's block.

You may want to use the closetag plugin <
http://vim.sourceforge.net/scripts/script.php?script_id=13 >. With it, you
can do as follows (or variations on it):

1. Highlight the desired visual area, then hit <Esc>
2. Hit `< to go to the start of the former visual area
3. i (go to Insert mode) then <b class="rainbow">
4. <Esc> (go to Normal mode)
5. `> (go to the end of the latest Visual area)
6. Ctrl-_ (and Closetag adds the </b> closing tag). This assumes that
whatever is in between is proper HTML, without for instance a "paired" tag
opened-but-not-closed or closed-but-not-opened.

To go to the exact location of visual marks it's `< (backtick, less-than)
and `> (backtick, greater-than). From inside a function you could wrap that
in a ":normal" command, or use "normal g`>" if you don't want to change the
jumplist.

HTH,
Tony.


Reply | Threaded
Open this post in threaded view
|

RE: vim function magic for HTML / XML

David Fishburn
In reply to this post by John (Eljay) Love-Jensen
 

> -----Original Message-----
> From: Eljay Love-Jensen [mailto:[hidden email]]
> Sent: Thursday, July 21, 2005 12:14 PM
> To: [hidden email]
> Subject: vim function magic for HTML / XML
>
> Hi everyone,
>
> I was trying to hack a little helper Vim function, let's call
> it "VisualTagger" and vnoremap, but alas...
>
> My goal is to bind the function to, say, the <F8> key.
>
> The desired function works in Visual Mode.
>
> Processing steps of the desired function...
> 1. Visual Mode highlight desired selection.
> 2. press <F8> (or whatever key bound to our desired magic
> function) *3. enter text at the prompt, such as:  b
> class="rainbow"<CR> *4. the text "<b class="rainbow">"
> (without the outer quotes) would be inserted before the visual block.
> *5. the text "</b>" (without quotes) would be inserted after
> the visual block.
>
> The * indicate the VisualTagger function.
>
> I'm having trouble figuring out how to access the Visual Mode
> block marks (or any marks, for that matter) from within a function.
>
> And although I haven't gotten to this point yet, I figure
> I'll have problems figuring out how to prompt for arbitrary
> text to use in the function.  (I was starting simple, by
> trying to insert <b> at the beginning Visual Mode block mark,
> and </b> at  the end.)
>
> I presume this has already been done, or maybe is part of Vim
> proper... my padawan Google-fu has failed me.  No need to
> reinvent the wheel.

I don't know if this is exactly what you are looking for, but pretty close I
think.

Cut and paste this into your vimrc.
I used ,,, to trigger it, both in normal mode and visual mode.


" Tip #346: Tag Select/Wrapper:
" http://vim.sourceforge.net/tips/tip.php?tip_id=346
" Author: David Fishburn
" These mappings and TagSelection function will allow you to place
" an XML tag around either the current word, or the current selected
" text.
" If the visual select is on a single line, the tag is wrapped
" around the text <this>way</this>.  If the visual select extends
" over multiple lines, the tag is wrapped around the text
"     <this>
"     way
"     </this>
"    
" When you are prompted for the tag name, you can enter:
"     Tag name?  p class="classname" attri="bute"
" The select is wrapped with:
"     <p class="classname" attri="bute">
"     Your selection
"     </p>
" Notice the attributes have been stripped from the closing tag.
"
" Use nmap, not nnoremap, since we do want to use an existing mapping
nmap ,,, viw,,,
vnoremap ,,, <Esc>:call TagSelection()<CR>

function! TagSelection()
  let tag = input("Tag name (include attributes)? ")

  if strlen(tag) == 0
      return
  endif

  " Save b register
  let saveB = @b
  let curl  = line(".")
  let curc  = col(".")

  " If the visual selection is over multiple lines, then place the
  " data between the tags on newlines:
  "    <tag>
  "    data
  "    </tag>
  let newline = ''
  if getline("'>") != getline("'<")
      let newline = "\n"
      let curl  = line("'>")
  endif

  " Strip off all but the first word in the tag for the end tag
  let @b = newline . substitute( tag, '[ \t"]*\(\<\S*\>\).*', '<\/\1>\e', ""
)
  let curc = curc + strlen(@b)
  exec "normal `>a\<C-R>b"

  let @b = substitute( tag, '[ \t"]*\(\<.*\)', '<\1>\e', "" ) . newline
  let curc = curc + strlen(@b)
  exec "normal `<i\<C-R>b"

  " Restore b register
  let @b = saveB

  call cursor(curl, curc)
endfunction


HTH,
Dave

--
Do you use Vim to edit SQL?
<Plug>
Check the description of these plugins:
dbext.vim - Provides database access to 10 databases (Oracle, Sybase, SQL
Server, MySQL,...) (http://www.vim.org/scripts/script.php?script_id=356)
SQLUtilities - Formatting SQL, generate - columns lists, procedures for
databases (http://www.vim.org/scripts/script.php?script_id=492)
sql.vim - SQL indent file for Sybase ASA and Oracle
(http://www.vim.org/scripts/script.php?script_id=495)
Intellisense for Vim : SQL, C++, CSharp, Java, JSP, XML, HTML
(http://www.vim.org/scripts/script.php?script_id=747)
</Plug>


Reply | Threaded
Open this post in threaded view
|

Re: vim function magic for HTML / XML

James Vega-3
In reply to this post by John (Eljay) Love-Jensen
On Thu, Jul 21, 2005 at 11:14:11AM -0500, Eljay Love-Jensen wrote:
> The desired function works in Visual Mode.
>
> Processing steps of the desired function...
> 1. Visual Mode highlight desired selection.
> 2. press <F8> (or whatever key bound to our desired magic function)
> *3. enter text at the prompt, such as:  b class="rainbow"<CR>

let text=input("Tag text: ")

> *4. the text "<b class="rainbow">" (without the outer quotes) would be
> inserted before the visual block.

'<put! ='<' . text . '>'

> *5. the text "</b>" (without quotes) would be inserted after the
> visual block.

let idx = stridx(text, ' ')
if idx == -1
    let idx = len(text)
endif
'>put ='</' . strpart(text, 0, idx) . '>'

> The * indicate the VisualTagger function.
>
> I'm having trouble figuring out how to access the Visual Mode block
> marks (or any marks, for that matter) from within a function.

:he '<
>
> And although I haven't gotten to this point yet, I figure I'll have
> problems figuring out how to prompt for arbitrary text to use in the
> function.  (I was starting simple, by trying to insert <b> at the
> beginning Visual Mode block mark, and </b> at  the end.)

:he input()
:he :let

> I presume this has already been done, or maybe is part of Vim
> proper... my padawan Google-fu has failed me.  No need to reinvent the
> wheel.

Putting it all together, a simple version would be:

fun! VisualTagger() range
        let tag = input("Tag text: ")
        let idx = stridx(tag, ' ')
        " No spaces found, so it's just a simple tag
        if idx == -1
                let idx = strlen(tag)
        endif
        " Put tag before visually selected area
        '<put! ='<' . tag . '>'
        " Put closing tag after visually selected area
        '>put ='</' . strpart(tag, 0, idx) . '>'
endfun

A version that's a bit more flexible:

fun! VisualTagger() range
        let tag = input("Tag text: ")
        let idx = stridx(tag, ' ')
        " No spaces found, so it's just a simple tag
        if idx == -1
                let idx = strlen(tag)
        endif
        " Escape the double quotes so they don't mess up the execute
        " command
        let starttag = escape(tag, '"')
        let endtag = escape(strpart(tag, 0, idx), '"')
        " Put tag before starting text
        execute a:firstline . "put! ='<" . starttag . ">'"
        " Need to add 1 to a:lastline because the previous put added an
        " extra line to the document
        execute (a:lastline + 1) . "put ='</" . endtag .  ">'"
endfun

Possible improvements could be using the proper indentation level for
those lines, and wrapping the tags around text in a single line..

HTH,
James
--
GPG Key: 1024D/61326D40 2003-09-02 James Vega <[hidden email]>

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: vim function magic for HTML / XML

Mathias Michaelis
In reply to this post by David Fishburn
David

>> Processing steps of the desired function...
>> 1. Visual Mode highlight desired selection.
>> 2. press <F8> (or whatever key bound to our desired magic
>>    function) *3. enter text at the prompt, such as:
>>    b class="rainbow"<CR>
>> 4. the text "<b class="rainbow">" (without the outer quotes)
>>    would be inserted before the visual block.
>> 5. the text "</b>" (without quotes) would be inserted after
>>    the visual block.
>>
> I don't know if this is exactly what you are looking for, but
> pretty close I think: [...]
>
>     " Tip #346: Tag Select/Wrapper:
>     " http://vim.sourceforge.net/tips/tip.php?tip_id=346
>     " Author: David Fishburn
>     [...]
>
Great! I put the additional line

      exec "normal '<V'>j="

after the line

      exec "normal `<i\<C-R>b"

within your TagSelection() function in order to keep the inserted
code well intended.

Thanks a lot!

Mathias