Thursday, July 30, 2015

Re: Flexible auto-insertion of date at end of file.

On 2015-07-31 00:14, Erik Christiansen wrote:
> On 30.07.15 07:52, Tim Chase wrote:
> > So combining those above with your enhancement request, I came up
> > with the following function which seems to do what you want:
> >
> > function! Append_Date(fmt)
> > $put=strftime(a:fmt)
> > startinsert!
> > endfunction
>
> Many thanks Tim. That does it exactly, is half the length of what I
> laboriously waded through swamps to cobble together, and doesn't
> have lines which don't work as expected.
>
> I'm sure all my helpgrepping and archive diving was good for my
> soul, but the syntax which works is in a dialect I don't grok.
> Looks like I'll have to try to find out where it fits into the
> normal/ex/whatever universe. One day.

Functions are just a sequence of ex commands. Because of that, the
leading ":" can be omitted, but if present, will just be ignored.

So the first ex command is the :put command (":help :put") with a
specified line/location ("$" is the last line of the file:
":help :$"). The ":put" command expects a register, and as detailed
in the help, if you use the expression register ("="), you can
provide an expression to evaluate...in this case, the results of the
strftime() function.

The second ex command is "startinsert!" which, according to ":help
:startinsert" goes into insert-mode after the function has finished
evaluating. By using the "!", it acts like typing "A" in Normal-mode
to append to the end of the line.

Put those two pieces together, and presto! :-)

-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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: