vim command to jump to a tab (tabpage) containing the filename - vim

Is there any command in vim with which I can jump to an already open tab containing the file which is open in the tab.
And that too with possibility of completion among all the open buffers.
For example you have following files open in different tabs.
readme.txt
pom.xml
Then the command jt will autocoplete the buffer names and jump to the tab containing the buffer.

You can use the command :sb <buffer>
You'll have to :set swb=usetab, in order to use existing open window in another tab. Otherwise it will just split the current window.

Related

how to open background buffers in new tab without influence current window?

I open a bunch of files by vim *.html *.css *.js, and I want to know how to open those targeted files in background buffers without mess up current window?
I tried to use vsplit, then "buf <buffer-number-I-want-to-open>, then C-w T. But I found it's way to tedious.
So is there a clear way to do that? thanks!
When you launch vim by vim *.html *.css *.js, the argument would be loaded into your vim argument list.
you can get the list it by args:.
And you can use :next, :previous, first, last, to traverse them.
if you want to open them all as tab?
you can use argdo: argdo tab split. this will dispatch a tab split command to each element in the arglist.
see:
:help :args
:help :argdo
:help :tab
you can :tabnew then use :next or buffer + <num> to open background buffers. but it will introduce a NO NAME buffer.
a neater way is to use CtrlP's buffer view, then press <Ctrl-t> and you will have it.
--
plugin Ctrl-P can perfectly achieve this, if you want to save your time, you can stick with it.
For example, suppose you have 3 buffers, where you are in buffer 1 and 2 more buffers are in background.
:ls
1 %a index.html
2 style.css
3 h script.js
Assume that you want to open buffer 3 script.js
open CtrlP,
switch to buf view, and choose the buffer your want to open
then press <C-t>, you would be able to open the buffers in new tab
Cheers!

VIM: How to open another file from same location as an already open file

Let's say I am in my home directory. I open a file that is present in some deep nested directory structure:
vim /some/really/long/path/file.txt
Now, within vim, I want to start a vertical split with another file from that same long location (but I don't want to change the current directory to that path, I still want to be in the home directory).
A layman (like me) will do something like:
:vsp /some/really/long/path/file2.txt
But I want to find out from all you VIM geniuses out there, is there an easier way to do this?
On the same note, if I had multiple files already open residing in different paths, can VIM automatically assign some sort of internal variables to all the locations? And then when I want to start a new VSP or SP with one of the files from one of those locations, I simply use those internal variables?
Try this:
:vs %:p:h/otherfile
as %:p:h gives you the path of the current file.
See :help %:p for more info.
Edit another file in the same directory:
:vs %<Tab><C-w><C-w><C-w>file2<Tab>
With a mapping:
nnoremap <key> :vs <C-R>=expand('%:p:h')<CR>
If you like more navigation than typing the commands, a convenient option could be the use of the embedded Explore command, which opens a window with the list files in the directory of the file in current buffer. Then you can navigate with the cursors and press v when over the file you want to open in a vertical split.
A convenient way is to map the Explore command, like:
" open embedded file system navigator
map <leader>\ :Explore<cr>
Press <leader>\ and navigate to the file, then press v.
With some versions of vim, the Explore window stays open after pressing v -- in that case, if you want to avoid extra burden to close the Explore window, you can do first the vertical split with :vsp, invoke :Expore and open the desired file by pressing Enter.

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.

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'

Diff two tabs in Vim

Scenario: I have opened Vim and pasted some text. I open a second tab with :tabe and paste some other text in there.
Goal: I would like a third tab with a output equivalent to writing both texts to files and opening them with vimdiff.
The closest I can find is "diff the current buffer against a file", but not diffing two open but unsaved buffers.
I suggest opening the second file in the same tab instead of a new one.
Here's what I usually do:
:edit file1
:diffthis
:vnew
:edit file2
:diffthis
The :vnew command splits the current view vertically so you can open the second file there. The :diffthis (or short: :difft) command is then applied to each view.
I would suggest trying :diffthis or :diffsplit
When you have two files opened in vertical splitt, run
:windo diffthis
The content of all tabs are inside the buffers.
Look at the buffers:
:buffers
Find the right number for the content which should be diffed with your current tab content.
Open the buffer inside your current tab (f.e. buffer number 4)
:sb 4
Or do for vertical view:
:vertical sb 4
Then you can simple diff the content with
:windo diffthis
If you finished diff analysis you can input:
:windo diffoff

Resources