2

I have been using

sudo find / -iname '*foo*' 2>/dev/null | vim

to get a list of directories/files containing foo, and then I inspect this list, edit it, and write a bunch of new commands inside of vim.

After writing my commands inside of vim, I copy what I have written and quit vim then I paste it into the new command line.

Vim is my C-x C-e command line editor.

Can I make it so that :wq from inside of this vim session automatically fills the command line with the buffer?

As it would had I started vim with C-x C-e instead of vim.

How can I start a C-x C-e vim session with piped input?

4
  • 1
    To be clear, you want to open vim, and then in an empty buffer type echo something in that empty buffer and then you want to execute that buffer content? Then basically superuser.com/questions/266934/… ?
    – KamilCuk
    Dec 27, 2020 at 15:32
  • @KamilCuk I want to write the buffer to the command line, not :! execute the command from within vim. Dec 27, 2020 at 15:37
  • I want to write the buffer to the command line So like save to temporary file and then output that file? Along .... | ( tmp=$(mktemp); trap 'rm "$tmp"' EXIT; cat > "$tmp"; vim "$tmp"; cat "$tmp"; ) ?
    – KamilCuk
    Dec 27, 2020 at 15:53
  • @KamilCuk you know if you press C-x C-e while you are at the command line, it opens up your configured text editor, so that you can edit the command line in your preferred environment. I want to start that C-x C-e session with buffer contents as provided by previous command's pipeline Dec 27, 2020 at 16:13

1 Answer 1

3

You could use vipe (from moreutils) as:

sudo find / -iname '*foo*' 2>/dev/null | vipe | "$SHELL"

Where vipe lets you edit the piped input and then feed it to your preferred shell.

Or:

sudo find / -iname '*foo*' 2>/dev/null | vipe | xclip -sel c

To save the edited pipe into the CLIPBOARD X11 selection.

vipe will launch the editor stored in $VISUAL, or $EDITOR if $VISUAL is not set or vi if neither is set.

If you want to push the result of that editing directly onto your shell's history, with zsh, that could be:

print -rs -- "$(sudo find / -iname '*foo*' 2>/dev/null | vipe)"

(and recall that with the Up key).

To put it directly into the shell's editing buffer, still with zsh, replace print -rs with print -rz.

Or if you don't have vipe:

(){vim $1 && print -rz -- "$(<$1)"} =(sudo find / -iname '*foo*' 2> /dev/null)

You could make a function for it like:

vif sudovif() {
  () {
    vim $1 &&
      print -rz -- "$(<$1)"
  } =(${0%vif} find "$@")
}
vif / -iname '*foo*'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.