Create new file and open it in new buffer from within vim - vim

It's baffling how hard it is to find the magical command that does it.
I'm in vim. All I want to do is create a new file and immediately switch to a new buffer with it. How do I do it?

In the first page of the help (:help), see under 'basic editing' the chapter called 'editing.txt' and in that chapter, section 2 'Editing a file'. You can go straight there with :help edit-a-file. You will find the following options (among others):
:edit <your_new_file> will open a new buffer called 'your_new_file' (or open that file if it already exists). Or even shorter :e <your_new_file>.
Or:
:enew will create a new buffer for you to edit. (N.B. Then you can use :saveas <your_new_file> to save it).
I would recommend going over the all the basic editing pages (as well as doing vimtutor) if you're new to vim. The sheer number of help pages might seem overwhelming, but getting comfortable with the basics in vim should 'only' take a few weeks of practice. It's all up from there :)

Related

How to save buffer (preferably if changed) and then close the buffer but not VIM?

So, I just realized I could use marks with capital letters to go to different files. That's great! However, I'm trying to find a way to close the buffer and return to the previous one. Say I go to my header file to change or add the declaration of the function I'm writing, and then I'd like to save the file, but only if there's changes to it, to go back to working on the contents of the function. How can I do this?
There's :x, but it also quits VIM
There's :bd!, but it doesnt save the changes
There's :bw, but that's even worse (unfortunately that's w[ipeout], not w[rite]...)
There's ctrl+O, but it doesnt seem to work when I edit the file (also, it doesnt actually close the buffer)
There's :up followed by :bd, but that's two commands and VIM's about efficiency, so I'd prefer a single command if it exists
There's a few other options and variants, but none that do what I wanted, afaik
It feels like this should be simple enough to do with one command, preferably without macros/plugins/functions/snippets/etc; as close to vanilla as possible. I get the feeling I'm missing something obvious.
You could concatenate commands like so:
:w|bd
I'd like to save the file, but only if there's changes to it
:up[date]
to go back to working on the contents of the function
Press Ctrl^, or enter the command :e[dit] #
I'd prefer a single command if it exists
Set an option :set autowrite and then Vim will save the current buffer on pressing Ctrl^ automatically.

Pasting text to a new buffer

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.

Working with vi buffers

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.
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
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 `[

Making AutoComplPop search entire project (or open buffers)?

I started using AutoComplPop for automatic code completions. It works great on the single file I am editing, but if file1 is making a reference to a method defined in file2, it doesn't find it.
The docs don't specify if there is a way to make it search a whole project directory, or even just all open buffers, so I can't tell if this is simply not something the plugin does, or if I need to enable something.
I was testing it out on two Ruby files, if that's relevant. Thanks!
Looks like that the cause of the problem is that ACP set the complete option for its purposes to .,w,b,k (see line #125 in autocomplpop/plugin/acp.vim),
call l9#defineVariableDefault('g:acp_completeOption', '.,w,b,k')
while the default value that is used when pressing \<C-n> is .,w,b,u,t,i. And it appears that the very last letter i actually makes the difference: for some reason vim would not use word from an include file opened in a buffer to complete words in another buffer. So, b option is not enough, i must also be included. Adding the following line into my .vimrc helped
let g:acp_completeOption = '.,w,b,u,t,i'
At least it worked for C++ files, but I'm not sure it fixes the problem for the case of Ruby scripts.
Depending on what is on the left of the cursor, ACP (like all the alternatives) decides what completion mechanism to use.
But ACP only uses Vim's default completion mechanisms: if <C-x><C-o> and <C-n>/<C-p> don't provide what you are looking for, ACP won't help. Try them out first.
Oh cool, this plugin looks a lot like neocomplcache but maybe cleaner...looks a little old. Little concerning that there are so many open tickets on that project and no updates in two years.
Anyway, according to the documentation it doesn't...really...say. Very likely its one of the following things:
Your pwd. If the root directory for your source is some/path then that should also be your current working directory. Try typing :cd some/path to see if that makes a difference.
The runtime path rtp. See if adding the directory with your source files to &rtp does the trick.
The path. Same deal as the &rtp setting.
Very likely this plugin is just falling back on the built in ruby omni completion functions bundled with vim. Try help ft-ruby-omni.
I just had the same problem, and I actually found a solution for this.
Apparently you have to set in your .vimrc file the following:
let g:acp_behaviorKeywordCommand = "\<C-x>\<C-i>"
This will make acp look in every file included by your source for completions, as if you were actually typing <C-p>. However, it is slow, after trying it I decided to revert using <C-p> when there are no matches and default behaviour in the other cases.

In Vim, what is the "alternate file"?

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.

Resources