Execute command and insert its standard output at the beginning of non-empty line?

29 views
Skip to first unread message

Ottavio Caruso

unread,
Sep 10, 2019, 6:33:00 AM9/10/19
to v...@vim.org
Hi all,

I have this bash alias:
alias my-date='date +"%A %d %B %Y"'

and this line in vimrc:
set shellcmdflag=-ic

I want to insert (prepend) a timestamp at the beginning of a non-empty
line, without line breaks, but if I type:

:r !my-date

this will print the timestamp on the next line, that is the line below
the cursor.

Is there a way to achieve what I want?

Thanks

--
Ottavio Caruso

John Cordes

unread,
Sep 10, 2019, 7:27:44 AM9/10/19
to vim...@googlegroups.com
--
 
I recently set up a map like this:
map <leader>H :exec 'norm i' . system("echo -n 'Chronicle Herald, '") . system("date +'%A, %B %d, %Y'")<cr>

 Perhaps you could experiment with that idea?

--
John Cordes

Christian Brabandt

unread,
Sep 10, 2019, 7:33:58 AM9/10/19
to v...@vim.org

On Di, 10 Sep 2019, 'Ottavio Caruso' via vim_use wrote:

> Hi all,
>
> I have this bash alias:
> alias my-date='date +"%A %d %B %Y"'
>
> and this line in vimrc:
> set shellcmdflag=-ic
>
> I want to insert (prepend) a timestamp at the beginning of a non-empty
> line, without line breaks, but if I type:
>
> :r !my-date

:r reads the output of your command and puts it below the current line.

> this will print the timestamp on the next line, that is the line below
> the cursor.
>
> Is there a way to achieve what I want?

use strftime() together with Ctrl-R in insert mode. See the example in
the faq: https://vimhelp.org/vim_faq.txt.html#faq-21.4

Best,
Christian
--
Lerne Klagen ohne zu Leiden.

Ottavio Caruso

unread,
Sep 10, 2019, 1:26:49 PM9/10/19
to Christian Brabandt, v...@vim.org
Many thanks Christian, this is exactly what I was looking for.

I put this in my .vimrc:

iabbrev mydate <C-R>=strftime("%A %d %B %Y")

and it works like a charm.



--
Ottavio Caruso
Reply all
Reply to author
Forward
0 new messages