I know I can open the last closed (:q) file in Vim by using :e#. This will open the file in the current tab. How would I perform the same task but open the file in a new Vim tab. Also I am interested in how to open the file in a new split instead of the current tab.
# is simply an Ex special character that will be replaced with the name of the alternate file. Do an :ls, and the alternate file will be marked with a # there also.
# can similarly be used with :tabnew and split. In the examples below I'll use :tabe in place of :tabnew as :tabe is a shorter alias for :tabnew (search for either in the help docs):
To open the alternate file in a new tab: :tabe#
To open the file in a new split: :split#; this can be abbreviated to :sp#, and :vsp# for a vertical split.
Using a buffer number from :ls, e.g. buffer number 5 you can also:
open the buffer in a split with :sp#5; alternately :sb5 if the switchbuf option contains the newtab specifier - see :help switchbuf
open the buffer in a vertical split with :vsp #5 (there is no :vsb)
open the buffer in a new tab with :tabe #5
You don't necessarily have to leave normal mode to open the alternate buffer in a new window:
CTRL-W ^ opens the alternate buffer in a horizontal split.
CTRL-W T opens the current buffer in a new tab (Shift-T, that is).
So, one solution to your title question is the following combo.
CTRL-W ^, CTRL-W T: opens the alternate buffer in a new tab.
Note that for the caret "^" in the first command you don't have to release the Control key and you don't have to press Shift, just hold down CTRL then strike W and 6 (where the caret is located on many English keyboard layouts).
Related
I opened up two windows in vim and was trying to switch between windows, but accidentally pressed Ctrl+w+Tab (instead of Ctrl + w + h). If the cursor is currently on #include <boost/multi_array.hpp>, vim would open a new horizontal new window and open the file /usr/include/boost/multi_array/extent_range.hpp, and leave the cursor on the line namespace multi_array{.
If I am in .bashrrc file and do the same thing on a export, it would open another window and open .bashrc again, with the cursor located in the place where export first appears.
I tried to search for what the use is for this shortcut, but cannot find any. I also checked my .vimrc but don't see any key mappings for this.
The thing that you need to know is that in a terminal, Tab and Ctrl+I are indistinguishable (both are ASCII character 9).
So we check the docs (:help CTRL-W) and see
CTRL-W CTRL-I same as "CTRL-W i"
and looking under "CTRL-W i" says:
CTRL-W i split window and jump to declaration of identifier under the cursor.
So there we are — it does a jump to definition (same as :tag or Ctrl+]), only in a split.
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'
How do I close a window or unsplit without deleting the buffer?
A window is a viewport on a buffer. In vim to manage windows it is CTRL+w the leading command, that you can follow with several options (in bold those that answer to your question):
CTRL+w, v: Opens a new vertical split
CTRL+w, s: Opens a new horizontal split
CTRL+w, c: Closes a window but keeps the buffer
CTRL+w, o: Closes other windows, keeps the active window only
CTRL+w, right arrow: Moves the cursor to the window on the right
CTRL+w, r: Moves the current window to the right
CTRL+w, =: Makes all splits equal size
Then, you need to switch the buffers in the windows:
:ls lists all opened buffers
:b5 switches to your 5th buffer
Finally, to open all buffers in a vertical split, use: :vertical sball.
Very useful when you open multiple files as buffers after a grep:
grep -rno --exclude-dir={dir1,dir2,dir3} "searchterm" *
vim $(!! -l)
For more info, see the doc: vimdoc.sourceforge.net
If you want to edit another buffer in the window, just use :edit / :buf. The current buffer will be replaced, and remains in the buffer list (i.e. it shows up in :ls).
If you want to close the windows split, use :close. The :quit command will work, too, but has the side effect of closing Vim when this is the last window.
In order to leave buffers that have changes, you need
:set hidden
If you know how Vim deals with buffers, this is a recommended option that many users have set.
Vim windows are closed using :q.
However, if you don't have another window open, it will exit from Vim. If you do have another window to switch to, only the current window is closed, and buffer remains open. You may need to set hidden.
To close a buffer, you would need to do :bdelete.
You can check if your buffer is open or not by using :buffers.
In vim gf opens the file whose name is under the cursor in the current window. <C-W>f does the same but opens it in a new window. However this new window is created with an horizontal split.
How can I do the same and get a vertical split?
I tried various alternatives that did work (like :vsplit +normal\ gf), but have a slight problem: if the file doesn't exist, a new window is created anyways. This does not happen with gf nor <C-W>f. I'd like to have this behaviour as well from the "open file under cursor on a vertical split" command. How can I do that?
Here is possible mapping:
:nnoremap <F8> :vertical wincmd f<CR>
With a file name under cursor, hit F8 and voila.
Here is a solution involving 5 key strokes:
Ctrl-wv
gf
That is Ctrl-wv followed by gf in the normal mode.
Here is an equivalent solution that involves 7 key strokes:
:vs
gf
That is :vs command in command-line mode followed by gf in normal mode. Assuming we are in the normal mode with the cursor on the filename already, the complete sequence of keystrokes are: Shift:vsEntergf.
When we enter Ctrl-wv in normal mode or :vs in the command-line mode, the current window is split into two with the same file in both and the cursor remains in the same position (i.e., on the filename of the file that we want to go to in a new vertical split). So if we press gf now, the current window is now updated with the file we want to go to.
The end result is two vertical split windows: one with the first file and another with the file we wanted to go to.
If we're playing Vim Golf, I think the winning solution is to do:
Ctrl-w then f
to open the relevant file in a horizontal split.
Then change it to a vertical split, send then active window to the left or right side of the screen by doing either
Ctrl-w then L (active window to the left)
or
Ctrl-w then R (active window to the right)
Note that the final keystroke for left / right must be uppercase. The others should be lowercase
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.