Thursday, March 3, 2011

Re: Command to insert dated comment

On Thu, 3 Mar 2011, Benjamin R. Haskell wrote:

> On Thu, 3 Mar 2011, Sapfeer wrote:
>
>> Dear all,
>> I want to create a command that will insert comment mark, for
>> instance '#', space, date and time on the next line after current,
>> but AFAICS there is no way to put insert commands like o in command.
>> The most obvious way - use mapping, but it is preferable to use
>> command feature
>
> You don't want to use the o command, because that leaves you in insert
> mode. You're looking for the append() function:
>
> command TimedComment :call append(line('.'), '# '.strftime('%Y-%m-%d %H:%M:%S'))
>
> Another common idiom, if the comment you want to insert gets complex,
> is to create a function, then call the function from a command.
>
> E.g. Convert2HTML() is a function that converts a specified range to
> HTML in a new buffer:
>
> command -range=% TOhtml :call Convert2HTML(<line1>, <line2>)
>

Forgot to add... I much prefer doing this via abbreviations:

E.g. in languages that support C-style comments:

==> ~/.vim/plugin/eatchar.vim <==
" similar to one from the vim docs
fun! Eatchar(...)
let c = nr2char(getchar(0))
return c =~ (a:0 ? a:1 : '\s') ? '' : c
endfun
=================================

==> ~/.vim/after/ftplugin/php.vim <==
iabb /t /* TODO - bhaskell - <C-R>=strftime('%Y-%m-%d')<CR> - */<left><left><C-R>=Eatchar()<CR>
=====================================

Then, when writing PHP code, I can type /t<space> to insert a dated TODO
comment (ending up in insert mode with the cursor at the position
indicated by '^'):

/* TODO - bhaskell - 2011-03-03 - ^*/

--
Best,
Ben

--
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: