7

Is there a way to call Vim in such a way that it opens a buffer with some content specified on the terminal, i.e. not saved in a file?

For example, I would like to run vim and have it open a buffer with foo, bar, baz and qux written on it.

Is there an option that does this?

11

Did you try something like this?

$ echo "foo, bar, baz, and qux" | vim -

Or like that?

$ vim -c "put='foo, bar, baz, and qux'"
  • Aww, sweet! I Tried vim < echo "test" but with no success. I did not know about vim -! And +/-c can be useful too. Thank you. – Gonçalo Ribeiro Aug 29 '15 at 20:58
3

This solution is totally inspired from romainl's one (many thanks to him) but it allows to open multiple buffers with multiple strings.

Add the following lines to your .zshrc (or .bashrc or any relevant file where you define your bash functions)

function mvim {
    CMD="vim "
    for string in "$@"
    do
        CMD=$CMD"-c \"enew | put='"$string"'\" "
    done
    CMD=$CMD"-c \"bufdo 1d\""

    eval $CMD
}

After sourcing your .zshrc file you'll be able to call the following function:

mvim "foo bar" "baz" "and quz"

Which will open a vim instance with 3 buffers, each one containing one string.

If no argument is provided a regular vim instance will be started. If one argument is an empty string, an empty buffer will be created.

EDIT I realized that for a reason that I don't understand an empty line is created at the beginning of each buffer so I added the line

CMD=$CMD"-c \"bufdo 1d\""

To get rid of this empty line in all buffers.

  • 1
    That is an interesting script. Might be useful. Thank you :) – Gonçalo Ribeiro Aug 30 '15 at 21:15
3

I've recently looked for a way, how to put the clipboard content into a new buffer in vim. I think, it is worth to share it here:

vim -c "put! *"

UPDATE:
This will put the text from register * before ! the current line.

The * and + registers refers to clipboard in windows.

On linux + has the content of the clipboard and * contains the selected text.

  • In the vimrc that could even be done automatically if argc is 0. – dash-tom-bang Nov 19 '16 at 21:21
  • Maybe you could refer :help quotestar and :help quoteplus in your answer to help those who don't know those registers understand why this command works. Also on GNU/Linux there is a difference between * and +, while on Windows they are equivalent. – Gonçalo Ribeiro Nov 19 '16 at 23:57

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.