I'm in a Linux class right now and the question was brought up as to if there was a command to see what's in your buffer list inside vi. I searched online to see if I could find anything, but found nothing, so I went ahead and started to attempt creating a script myself, however I cannot seem to get the commands right.

Here's what I have so far:

while [ : ]

echo "What buffer position would you like to see?"

read choice

case $choice in
1) '"1p' ;;
2) '"2p' ;;
3) '"3p' ;;
4) '"4p' ;;
5) '"5p' ;;
6) '"6p' ;;
7) '"7p' ;;
8) '"8p' ;;
9) '"9p' ;;
0) '"0p' ;;

esac ; read -n 1 ;


I've tried swapping the command for something like this too:

1) vi -c \"1p ;;

Any ideas on how I might be able to pull a vi command out into command line like that? Thank you! (Side note, this is NOT homework or required. I just want to see if it can be done. I asked my professor to help me with it, and even he was stumped)

  • 1
    Er, :ls lists buffers. Were you perhaps talking about registers? In that case, its :reg [r]
    – D. Ben Knoble
    Jan 25 '18 at 16:50
  • @DavidBenKnoble Judging by the code included, I gather that the OP is trying to write a shell script that, from outside of Vim outputs the contents of Vim's registers.
    – Rich
    Jan 25 '18 at 17:29

First thing's first: I infer from your code (which uses "1p etc.) that you actually mean "register" when you write "buffer". In Vim terminology, a "buffer" is Vim's in-memory representation of a file (which may or may not actually exist on disk).

Secondly, I gather from your code that what you are trying to achieve is the construction of a bash script that will allow you to query the contents of Vim's registers from outside of Vim.

That said, here is a command that will print the contents of Vim's numbered register "0 (the yank register) from your shell:

vim -es -c'normal "0P' -c%p -cq! -

I leave it as an exercise to you to figure out how to incorporate that into your bash script: it's outside the scope of this site.

Broken down:

  • vim: Run Vim,
  • -es: in silent mode,
  • -c: passing in the commands...
  • normal "0P: use the normal mode command "0P to paste the contents of the "0 register at the start of the buffer,
  • -c%p: print the contents of the buffer (i.e. output it to stdout i.e. the shell)
  • -cq!: quit, discarding the contents of the buffer (which you altered with the "0P command),

See :help vim-arguments, this earlier question, and this one for more details on how this works.

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.