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

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

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

Ottavio Caruso
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

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAEJNuHw_Z9Vop%3DRL5%2BdVsNryX%3Dqm7mP0FoaZqKnvNVw%2BBCBUZw%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

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

John Cordes

On Tue, Sep 10, 2019 at 7:32 AM 'Ottavio Caruso' via vim_use <[hidden email]> 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

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

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

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAGZBEdQa6j8PiCKOotDURQB4cFB2jLQuFviY4kNN0G3dxYe%2BcA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

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

Christian Brabandt
In reply to this post by Ottavio Caruso

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.

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20190910113343.GD30959%40256bit.org.
Reply | Threaded
Open this post in threaded view
|

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

Ottavio Caruso
On Tue, 10 Sep 2019 at 12:33, Christian Brabandt <[hidden email]> wrote:

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

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

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAEJNuHym1bcr%2BwtAVfH4yh_7CLANp8ZpchBuKa%2ByAtVmmP1iCA%40mail.gmail.com.