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

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.

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

How can I explain this behaviour in Vim?

Vim is so awesome. For example, you have a file, called 'test0.html', stored in a folder.
In the same folder, you store the folder 'test' with the files test1.html, and test2.html.
Then you put in test0.html the following content:
include('test/test1.html');
include('test/test2.html');
In vim, you put the cursor on the filenames. You open the files under the corsor with the keys gf. Thats why Vim is so awesome.
I would like to open in a new tab. That's possible with the keys gF.
But what if you want to stay in the same file, but open the file in a background tab, like Chrome does?
So I'm mapping the key.
noremap gf <c-w>gF<c-PageDown>
So, when my cursor is on test1.html, it open with the key gf in a background tab. Wonderful, now I'm a satisfied man.
Then I want to open test2.html under cursor.
Vim jumps to the tab of test1.html, instead stay on test0.html
When I tried to debug this weird behaviour, by only mapping gf to gF, and then do manual CTRL+pagedown, I get the expected behaviour.
My guess is that Vim is much faster with executing the command before he opens the new tab (gF), and so I get to the last tab from the first tab.
Am I correct in my guess explaination?
<c-PageDown> or more commonly used gT will got to the previous tab. <c-w>gF on the other hand will open the file under the cursor in a new tab. That tab will be last tab. So doing a gT will not always make you go back to the previous tab.
You can change your mapping to go back to the previous tab like so:
nnoremap gf :execute "normal! \<lt>c-w>gF" . tabpagenr() . "gt"<cr>
However I would personally suggest you avoid using tabs in such a manner and use buffers instead.
noremap gf :tabe<cfile><CR><c-PageUp>
This is even better. When the file doesn't exist, Vim will create a new one.

Move section to new file in vim

I have a section in file1 which should be moved to file2. Normally I'd do this by visually-selecting the block, deleting it, :wq from file1, open file2, then paste.
Is there an easy way to move a block of text from file to file2 without closing vim?
Alternatively:
write selection to new file:'<,'>w file2
then reselect and delete with gvd
The first step is covered in vimtutor Lesson 5.3: SELECTING TEXT TO WRITE.
You could open your new file in a split using :sp newfile and then delete the block as normal from your first file. Then, change to the other split with ctrl w and w. Then use p to put the deleted content into the other file.
You could open the file in a new buffer.
just open the file via :e file2 and paste the text. To move quickly between the buffers use either :e # or :b #
see :help buffers for more information
Since no one mentioned that: you can use tabs instead.
Select your block.
Delete it with d
Create a new buffer in new tab with :tabnew newfile.name
Paste it and save it with p and :w
You can go back with gT or close current tab with :q
I personally newer use buffers -- only tabs. Read more about them in :help tabpage

Vim split unless open

When using split windows in VIM, sometimes I create new split of a file already opened in another split (typically with plugins that open a unit test for given file in a new split).
Is there a way how to remap split command so that it checks whether the file is already opened before splitting, and if it is, give a focus on it?
You can't remap the existing split command, as far as I know, but you can achieve the same same effect by writing a new function Split and then using a command-mode abbreviation (cabbrev).
Here's a function/mapping that should do what you want.
function! MySplit( fname )
let bufnum=bufnr(expand(a:fname))
let winnum=bufwinnr(bufnum)
if winnum != -1
" Jump to existing split
exe winnum . "wincmd w"
else
" Make new split as usual
exe "split " . a:fname
endif
endfunction
command! -nargs=1 Split :call MySplit("<args>")
cabbrev split Split
Note that this will only "check" for existing splits in the current tab, and hidden buffers are ignored. (However, it shouldn't be too difficult to add more cases to enhance this functionality.)
An alternative would be using :drop {file}.
Edit the first {file} in a window.
- If the file is already open in a window change to that
window.
- If the file is not open in a window edit the file in the
current window. If the current buffer can't be abandoned,
the window is split first.
Also using :sb to switch buffers might also be of use. See vim: move to buffer?
For more information:
:h :drop
:h :sb
:h 'swb'
That's one of the features of searchInRuntime.
:GSplit and :GVSplit can be renamed, they support filename completion, and they will ask which file to open when several match the pattern in &path.

Resources