When VIM refers to a file im editing as a "buffer"...what exactly does it mean? Whenever I edit the file in shell or in the application, it refers to the copy of the file as a buffer. I was curious as to what exactly this meant, but couldn't find anything on it. Any help would be appreciated.
From :help windows-intro, as linked by icktoofay in a comment:
A buffer is the in-memory text of a file ... [which is] loaded into memory for editing. The original file remains unchanged until you write the buffer to the file.
That is, a buffer represents the actual loaded/working data itself.
You can think of it as similar to the Windows clipboard. You can use it to cut, copy and paste text snippets.
But you can have multiple "buffers" open at the same time. And each buffer can have a name.
See VI Tutorial: Manipulating Text:
A named buffer is another method to move or duplicate text... first position the cursor at the material you want to copy. Next make a copy of the desired text by using the yank command. This places the copied text into a temporary buffer...
Related
I've found questions that are similar, but don't really address what I'm trying to learn. I want to yank or delete text and append it to a new (or existing) buffer without changing buffers. I want to basically redirect the pasted text to its destination at the end of a separate buffer without leaving the original one, similar to what you might do with shell file redirection. I have a hard time believing vim/nvim can't do this, but haven't found an appropriate answer anywhere as of yet.
:'a, 'bw ~/path/to/file.txt
This will copy the text between the two marks 'a and 'b, and write it to a file in the filesystem. This is good, but the file can't be appended to... and it doesn't get opened in a buffer.
There is a :w >> {file} variant that lets you append to a file (:help :write_a).
As #Matt already commented, the usual way would involve switching buffers. Vimscript usage is closely aligned with (mostly Ex-) commands that the user would interactively use. With recent Vim versions, you can alternatively call the low-level appendbufline() function, though. This would bypass any autocmds, buffer setttings, etc. Depending on your use case, this can be desirable or not.
If the target buffer is already visible or can be kept visible as a side effect, temporarily switching to it is easy (mostly involving :sbuffer). My ingo-library plugin has a function ingo#buffer#visible#Execute() that also handles hidden buffers transparently.
I want to know the process for copying data from file 1 and paste the data into file 2 in vi editor using a buffer.
Can you please tell me the step by step process.
How to do that?
From vi manual:
6.5.6.3 Using Named Buffers
To repeatedly insert a group of lines in various places within a document, you can yank (or delete) the lines
into a named buffer. You specify named buffers by preceding a command
with double quotes (") and a name for the buffer. For example, to yank
four lines into the named buffer a, type "a4yy. You can use several
different buffers. For example, you might also delete text from one
location and add it to several others. To delete 12 lines into the
named buffer b, type "b12dd.
To insert the text, precede the p or P command with n, where n is the
named buffer. For example, to insert the lines saved in buffer b, type
"bP.
You can overwrite named buffers with new lines. The buffers are saved
until you exit vi.
When you use named buffers, you can safely delete and yank other text
without affecting the lines you have already saved in the named
buffers -- unless, of course, you purposely overwrite the named
buffer.
Just in case you don't really need the buffer contents...
then you can also just directly do something like this to overwrite file2:
:4,12w file2
or append to it with something like:
:5,20w >> file2
Of course other standard ex line addressing options are also available.
There has already been a post in stackoverflow for VI editor for copying all the text into the clipboard. (Copy all the lines to clipboard) I want to do the same thing with the less viewer. I tried to search online for the process called "yank" and I did not find anything for it.
How do I copy all lines in the less editor into the clip board.
And I cannot close less and reopen it in vi. It is because of the fact that I have managed to load this file into the editor and while I have loaded it, the file has already been moved in the back end. It is a long story. The easiest solution for me now is to copy the contents of the file into memory.
less doesn't have a clipboard, but you may be able to get it to output what's stored in its buffers to a new file. This will only work if the entire contents of the file are buffered:
Type g to go to the top of the file
Type | (that's a pipe character, not an L or I) to indicate that you want to output to a pipe
Type $ to indicate that you want the output content to go to the end of the file
Type dd of=/path/to/new/file and press Enter
The dd command will take the piped data and save it to the file passed to the of= argument.
as an workaround you can set terminal's font size to 1, then select with mouse and copy (works for big , but not huge files).
If the file is not too big and if it fits in your terminal number of lines configured, then do the following:
Terminal > Edit > Clear to start
cat <file_name>
Terminal > Edit > Select all
Terminal > Edit > Copy
I'm looking for a way to prevent the modification of a part of a vim buffer. I know it is possible to lock buffer to prevent all modifications inside, but I would like to do the same for just a few lines, or a paragraph.
Any idea if this is possible ?
Cheers,
V
Well, as I know, vim can not do that. The text in vim buffer is just a "string" without any property can be attached to them. So "readonly" can be only to whole buffer, but not specific characters, although emacs is able to add text properties to let some characters in buffer readonly.
On the other handle, if you really want to edit something and make it not affect the other buffer content. There should be an alternate way, although is not elegant.
1.create a temp buffer with "setlocal buftype=nofile", insert the text you want to edit into that buffer.
2.show that buffer on other window(need split firstly)
3.edit the temp buffer.
4.when you close that buffer, "merge" the result in the real buffer, and replace the text you want to edit.
So, you need to do that via some key binding and vim scripting, not an easy way.
If the "protected" region of text is readily defined (i.e. by its position in the file, or a regular expression) you could try writing a BufWritePre function that checks this region and throws an error if it has been modified. I resorted to this when I wanted to prevent saving a file with an invalid fold structure.
Presumably this would involve saving the original text in a variable when the file is loaded, and this may have performance implications.
Hope this helps.
There is a plugin called narrow region that edits selected text leaving intact the rest
I just ran :help registers in Vim and noticed that # 'contains the name of the alternate file'.
I have seen an example for renaming files that goes like this:
" Save the current file, foo.txt, as bar.txt
:w bar.txt
" Start editing bar.txt
:e#
So apparently in that case, the file you just saved out is the "alternate file."
Can someone give me a more general definition for the "alternate file" and what else you might use it for?
The alternate file is the file that was last edited in the current window. Actually when you use some command to open a new buffer, if the buffer that was displayed had a filename associated with it, that filename is recorded as alternate file name.
See :help alternate-file.
Very useful for...
Pasting in the name of a file I've just been looking at into the current file.
You can use <C-R># for this in insert mode or "#p in normal mode.
Not that useful for...
Jumping back and forth between two files. It does the job very well, but this is just something I don't generally need to do.
Even in the example given, I'd probably use:saveas bar.txt instead.
An Example:
Say if you're doing a bit of C programming and want to call some function. You can't remember the name of the function, so you place a mark on your current location mA and jump into several different files using tags or grep to find out where the function is declared and what it's actually called.
Ah - found it. You can copy the name and return to the mark yiw'A
Uh-oh - we also need to #include the file! Easy - just use the alternate file name register to paste the file name in... Gi#include"<C-R>#"
Be pleased that you've avoided the distraction of having to go back to the function's declaration and copy out the file name via :let #"=#% or something similar.
What I'd rather do when jumping between files:
When editing two files, it's probably easier to split them, so you can keep both on screen at the same time. If I'm editing 2 files I'll usually be comparing them in some way.
Usually I'm interested in 1-3 files (any more and I get confused). I'll often jump into or directly open many other files. Marking the interesting files, or traversing the jump list is usually the way to get around in this case.
If you're editing C/C++ where you're switching between a file and it's header, use a plugin! It will be much more convenient.
I use it in the buffer context to return to the last buffer that I was editing
vim foo bar
:n
:e#
will take you back to foo in that case
I 've always interpreted the "alternate file" as being the "previous file", so it is an handy way to jump back to the buffer you were editing.