Find file in vim, switch to next file without saving current file - vim

Vim newbie here.
I use :find command to open a file and make some changes.
Suppose I want to open another file without saving the current changes, then how should I do it.
What is the best way to list all open files and quickly open them again for editing.
Thanks in advance.

:e! filename
will allow you to open a named file in a new buffer (note the exclamation mark overrides the warning re. an unsaved file).
CTRL-W + cursor
will move between visible buffers and does not enforce the saving of your file as you navigate.
:buffers
will list your open buffers and allow you to navigate to each one (albeit with some cryptic flags indicating which buffers are visible/hidden/amended etc.)
:help buffers
will tell you about all the buffer-related features.
:help :wincmd
will tell you all about windows navigations
See also the VIM buffer FAQ

Related

Vim: How to open a file from current directory list without using arrow keys

Suppose I am editing a file using vim, and I go back to the file's current directory using :Ex, and I have a list of all the files I could open, I know arrow keys + Enter works, but is there a way to use : something to open a specific file? I tried :e filename but this goes directly back to the root of vim instead of the current directory.
Thanks.
The following is not a :-command, but it does the job and it can be in the muscle memory already:
You can move around the directory listing just like in a standard buffer. So you can /filename<Enter> to get to your file and <Enter> to open it. But typing whole filename can be rather cumbersome, so let's improve:
If there is something specific in the filename-baz, it will be enough to /baz<Enter><Enter>. And yet better, if you run vim with set incsearch and set hlsearch as many do, you'll see the search space narrow down to your filename, so you can easily get the prefix-search behavior of file commanders. Or even better, thanks to the coloring.
In case you can see the filename on the screen, then with EasyMotion, you can <Leader><Leader>w, then the usually two letters to get there and <Enter><Enter>.
tried :e filename but this goes directly back to the root of vim instead of the current directory.
This may happen because you are running vim from a different directory.
Suppose I run vim from my home directory, you will have to run :e /path/to/filename and :tabe /path/to/filename where the filepath is relative to the home directory.
you can open another file while vim is open with :tabe filename and to switch to the other file you type :tabn or :tabp for next and previous accordingly.
Maybe this link can help you

How to open VIM [no name] file from terminal

I am using linux.
In terminal I can type
vim sample
A vim window for the file 'sample' opens
Here any change can be saved with :w
But I want to open a new vim file having no name and save it with the name sampleName using
:w sampleName
But I am unable to do so.
Typing only vim in terminal gives me a window with about and copyright information
I am not using gvim but vim
You should be able to just run vim with no arguments. It will open vim by itself. To save it to a file, execute the command: :w <new file name>
Issue the Command, :enew and Also Learn about How Vim Manages File Editing
I would recommend reading up on file and buffer usage in Vim in the help file, usr_22.txt.
As architrex has indicated, by default, if one does not have a file(s) listed in the argument list when starting Vim, a new buffer is created. See :help starting.
If you started vim with one file in the file argument list like you described, vim sample, a common way to work with an empty buffer is to issue the :enew command (typically after you would have written changes to the file named, sample, :w).
We can see the new buffer here:
Once one is done modifying the new buffer, you can issue the write command, :w sampleName, with the expected result of writing the file.
Vim's use of buffers is intuitive and you will become more skillful as you use it.
When I started using Vim, I leveraged using NETRW which is a seeded file navigation plugin with Vim for file creation (and placing those new files in buffers).
What follows is one way to leverage NETRW to do this.
If you have already started Vim, I would type :Exp or :e . which are ex commands which will start the file explorer which is a feature of the seeded file navigation plugin, NETRW.
One could start Vim:
Start the file explorer for NETRW:
I would then use the file explorer to navigate to the desired file to create a new file you wish to edit (k is up, j is down, enter key means select).
Next, I would type,%, once I have navigated to the desired location.
You will be notified to "Enter filename". Just enter "sampleName" and press enter.
Go into edit mode (e.g. i) and start typing.
NETRW is a robust file navigation tool. Creating files in the file locations desired is an essential skill to have to utilize Vim well.
You could also read the help files concerning NETRW (e.g. the ex command, :help netrw).

vim open buffer in existing tab

How can you tell vim to use an existing tab page for a file if it's already open?
I thought this option is supposed to do that
set switchbuf=usetab
As a minimal example I had only the above line in my .vimrc and moved all plugins (no .vim directory) but when I do for example vim .vimrc and then :tabe .vimrc I get two tab pages with the same file. Is there a way to prevent that?
You should read :help 'switchbuf' more carefully:
This option controls the behavior when switching between buffers.
So… that option has no effect on non-buffer-switching commands like :tabedit.
Also, :help :tabedit says:
Open a new tab page with an empty window, after the current tab page.
So… you can't really expect that command to not open a new tab page, do you?
If you want to edit a new file in place, use :e filename.
If you want to edit a file in a horizontal window, use :sp filename.
If you want to edit a file in a vertical window, use :vs filename.
If you want to edit a file in a new tab page, use :tabe filename.
If you want to switch to another buffer, use :b.
If you want to switch to another buffer and benefit from the switchbuf option, use :sb.

GVim - How to handle multiple files

Sorry to ask such a novice question but I am looking for a way to handle multiple files. I dont want to type huge file paths to open every file using :tabnew and :e commands
Fuzzy Finder is a handy plugin to quickly find and open files.
Basically you have to only type a few letters like test and you'll get a pop-up menu to open in your current path :
footest.c
bartest.h
footest.h
...
It is a bit slow when used on NFS but it is useful if you don't want to type long path and file names.
Alternatively if you don't want to use any plugin, by default gvim/vim includes a file browser called netrw.
To start it, just type :e . you'll get the content of your current directory, you can then navigate through the directory structure quite easily. (There is even commands to delete, rename, etc like a standard file explorer)
:help netrwfor more information.
A couple of tips that you might be interested in:
You can configure Vim so that the
current directory "follows" the
directory of the file you are
currently editing. That way you can
edit another file from the same
directory without having to type the
full path. This can be achieved by
putting either set autochdir or
autocmd BufEnter * lcd %:p:h in
your .vimrc
You can use wildcards with tab
completion. e.g. to edit
a_file_with_a_long_name.txt you could
do :e a*long and then press
Tab followed by
Return.
Usually, vim supports buffers for that. Use :badd to add buffer, :bdelete to remove it and :ls (or :buffers) to list all opened buffers. I believe, GVim supports these features too.
For example, if you wanna edit all .rb files in your app/controllers/pages dir (in the case of Rails project), you type vim app/controllers/pages/*.rb in your terminal and then edit the first file (buffer) in the vim window. When you've done with all changes, save changes as usual with :w (note: do not use q! option - this will close all your buffers you've opened) and then use :bn<tab> (or fully, :bnext) or :bprevious to switch to the next file (buffer). When you run :bnext on the last buffer, you'll be dropped to the first one.
You can open a directory in Vim, search for the file o directory you are looking for with '/' and type [enter] to open it.

How to make Vim's file browser open a file in a new buffer?

Sometimes I start Vim by pointing it at a directory, but I'm not certain yet which file I'll need to change, so I end up looking through several files.
Is there a way to make the file browser open a selected file into a new buffer so that the file browser is still available without having to reopen it with :e path/to/directory ?
I'm not sure if you can have it open in a new buffer, but you can have it open in a new split using o or v for a horizontal or vertical split respectively.
You could also preview the file using p which runs :pedit <fname> where <fname> is the file under the cursor. This opens a new split window but doesn't change the cursor focus or position. You can close this window with :pclose or simply :pc.
See :help netrw-browse-maps for more information.
This is not strictly speaking what you are asking for, but I think it's equivalent:
If you open a file from the file browser you can use CTRL-^ (and/or CTRL-6 ?) to return to the browser. This results in the file open in one buffer and the file browser open at the directory you started in.
Hope it helps...

Resources