vim open buffer in existing tab - linux

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.

Related

How to stop Vim from splitting window horizontally when doing an :Ex

When I use :Ex to open a new file, Vim sometimes decides to split the window horizontally. I don't like that behavior. I prefer what Vim usually does, which is to open the file in its own... window, I guess it's called in Vim? Anyway, when it doesn't split the window horizontally. How can I control what behavior is trigger by an :Ex?
Well :h is your friend. :h :Ex shows you the following:
:Explore will open the local-directory browser on the current file's
directory (or on directory [dir] if specified). The window will be split only if the file has been modified and |'hidden'| is
not set, otherwise the browsing window will take over that window.
Normally the splitting is taken horizontally. Also see:
|netrw-:Rexplore|
So vim will split the window, if you haven't saved your changes. This is to ensure, that you don't lose anything. I am not aware of any possibility to turn it off. You could of course redefine :Ex to just discard or save changes.

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

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

Show vim tab line even if only one file is open

I would like for vim to show the tab header (tab line) even if only one file is open.
For example, I open a file using vim. Then, I use :tabnew to open another file in a vim tab. Vim then displays a nice tab header at the top of the file. I would like this tab header to always be displayed (i.e. even if only one file is open in vim).
Also, not really a fan of the airline plugins, so not really looking for those kinds of answers.
Yes sorry about that. I was doing :help tabline and didn't see anything. You just need to put set showtabline=2 in your vimrc

Vim :drop for :buffer - jump to window if buffer is already open with tab autocomplete

How to jump to tab if trying to open already opened file in Vim has introduced me to the :drop command, which is analogous to :edit but focuses on an existing window if there is a buffer open there with the same path instead of taking up the current window.
Is there a command analogous to :drop such that when I try to tab complete, instead of listing files in the current directory like :drop, searches in my open buffers like :buffer does?
You can try :sbuffer. It works like :buffer except that it tries to jump to the first window displaying the desired buffer, defaulting to opening it in a new window otherwise. Its behavior is governed by the switchbuf option.
:help :sbuffer
:help 'switchbuf'

Vim's :open does not tab complete, what does?

Instead of tab completed file or directory names, I see ^I
:open ./lib/^I^I^I
Is there another command that would offer tab completion, or perhaps another solution?
I'm considering replacing the following mapping with something that would use buffers, but want to keep tab completion.
map <C-O> :tabnew ./
That might be because :open is not the command you're looking for. Try :edit or :e for short. Also try :help :open and :help :e to see, what the commands do. :e has tab completion.
Use :enew if you want to create a new empty buffer.
Use :edit filename if you want to edit a specific file in place of the current one.
Use :new if you want to create a new empty buffer in a new horizontal split window.
Use :split filename if you want to edit a specific file in a new horizontal split window.
Use :vnew if you want to create a new empty buffer in a new vertical split window.
Use :vsplit filename if you want to edit a specific file in a new vertical split window.
Use :tabnew if you want to create a new empty tab.
Use :tabedit filename if you want to edit a specific file in a new tab.
Use :help :command if you are unsure about its usage.
:set wildcharm=<tab> for tab completion.

Resources