Question

So I am learning about vim/vi/ex and I thought I had a pretty good understanding of the buffer system. (theres a gen purpose buffer and you can also name buffers). I know that anytime you use yank, change or delete, the contents of the general purpose buffer are over written.

However, I was given a task to use a vi/ex command to delete the last 2 lines of the buffer and I am stumped. I can't seem to find the answer by searching the forums or the Internet in general so I thought I would ask the gurus of the interwebs for some help here.

Is there a way to directly modify the contents of a buffer, or is the answer simply to paste the buffer, delete the last 2 lines and then yank it again?

Any help is appreciated.

EDIT: I would like to edit this question to include the question:what is the difference between a buffer and a register?

I originally thought they were essentially the same thing, but have since learned otherwise.

My new understanding is that the things I were previously referring to as buffers are actually registers, which makes more sense according to my knowledge of computers.

Now, I believe the buffer is just the current copy of the text that you are modifying. Or more specifically, the area of main memory in which you are manipulating data. (So the answer to my question would actually just be :$-1,$d)

However, I have run into conflicting information on these forums that say the opposite, referring to "registers" as "Buffers".

If someone could clear that up I would be really appreciative.

EDIT: vim help got me all the answers I needed, can't delete the post though. It should be noted that there are a lot of answers to questions both on this site and many others that are voted up highly, but refer to registers as buffers incorrectly.

Was it helpful?

Solution

I started to write a lengthy answer but I ultimately decided to scrap it and give you the answer you deserve:

:help buffers
:help registers

OTHER TIPS

first of all, after reading your question, I am pretty sure what you were talking about "buffers" actually are "registers". The difference between buffer and register you can find in vim help. (In fact the two things are completely different concept)

I know that anytime you use yank, change or delete, the contents of the general purpose buffer are over written.

This is not true, you can append content to register, if you use A-Z register.

Is there a way to directly modify the contents of a buffer(register)...delete the last 2 lines and then yank it again?

Yes, there are ways. You can access and modify the content of a register by @x. You can change the content simply by let @a='newValue'.

with your example, if you want to remove last two lines:

:let @a=join(split(@a,'\n')[0:-3],"\n")."\n"

then you can do "ap to paste the modified content from register a.

There is built-in function setreg(), which can change the value of a register as well. check its help doc if you want to use it.

How to paste the buffer, delete the last 2 lines and then yank it again?

The secret is to use use the '' and `]marks.

p`]dky''

For more help see:

:h ''
:h `[
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top