vim - buffers, tabs, splits and windows
Working with multiple files
- https://mkaz.blog/working-with-vim/buffers/#:~:text=Using%20%3Abd%20will%20close%20the,will%20also%20close%20the%20window.
- https://vim.fandom.com/wiki/Quick_tips_for_using_tab_pages
- https://vim.fandom.com/wiki/Using_tab_pages
- http://vimdoc.sourceforge.net/htmldoc/tabpage.html
Buffers
How to view all open buffers?
:buffers
:ls
This will show a numbered list with all open buffers. Tip: the numbers on buffers do not change during a session.
Jumping between buffers
(There might be an issue)
To go to a specific buffer (file), we can use one of the following:
:buffer <filename>
(to go to the buffer with the number 5):buffer5
(to go to the buffer with the number 5):b5
(to go to the buffer with the number 5):bnext
:bprevious
:bfirst
or:bf
:blast
or:bl
How to close a single buffer?
:q
will quit the current window.- When there is only one window,
:q
will close vim. - Use
:q
only when there are multiple splits.
Using it’s number:
- To close the buffer with number 2, do this
:2bd
- To close the buffer with number 13, do this
:13bd
Get the name of the current file
When working in a file/buffer, how to paste the name of the file/buffer in it?
"%p
This is because, the name of the file is available in the register %
.
Register %
contains the name of the current file, and register *
contains the name of the alternate file. These registers allow the name of the current or alternate files to be displayed or inserted. The name, or full path, or directory containing the file can be used.
For example, in directory /abc
the command vim def/my.txt
would edit file /abc/def/my.txt
. The following commands could be entered to display the information shown.
command | description | |
---|---|---|
:echo @% | def/my.txt | directory/name of file (relative to the current working directory of /abc) |
:echo expand(’%:t') | my.txt | name of file (’tail') |
:echo expand(’%:p') | /abc/def/my.txt | full path |
:echo expand(’%:p:h') | /abc/def | directory containing file (‘head’) |
:echo expand(’%:p:h:t') | def | First get the full path with :p (/abc/def/my.txt), then get the head of that with :h (/abc/def), then get the tail of that with :t (def) |
:echo expand(’%:r') | def/my | name of file less one extension (‘root’) |
:echo expand(’%:e') | txt | name of file’s extension (’extension') |
The following commands insert lines consisting of the full path of the current and alternate files into the buffer:
:put =expand('%:p')
:put =expand('*:p')
Update multiple java files at once
Option 1 (Do not use this):
If you want to open all files matching the pattern in subfolders - :args **/*.java
To replace four white spaces in java files with a tab use this - :bufdo exexute "%s/ /(press tab)/g" | update
1. :bufdo execute "%s/(press tab){/{/g" | update
1. :bufdo execute "global/^{/normal -gJ" | update
1. :bufdo execute "%s/){/) {/g" | update
Option 2: Use Rg and cfdo as explained in the fzf document.
Tabs
Creating And Closing Tabs
command | description |
---|---|
:tabnew | To open a new tab with an empty buffer |
:tabe <file> | to edit file in a new tab |
:tabc | to close a tab and all the buffers in it |
Show a numbered list with all open tabs
To go to a specific tab, do :5gt
(to go to the tab with the number 5)
:tabs List the tab pages and the windows they contain.
Shows a ">" for the current window.
Shows a "+" for modified buffers.
For example:
Tab page 1 ~
+ tabpage.txt ~
ex_docmd.c ~
Tab page 2 ~
> main.c ~
Other commands for tabs
command | description |
---|---|
:tabs | List the tab pages and the windows they contain. |
g<Tab> | Go to the last accessed tab page. |
:gt | Go to the next tab |
: gT | Go to the previous tab |
: nnn gt | Go to a numbered tab. For example, 3gt goes to tab 3 |
:tabn, :tabp | (or gt, gT to switch) |
Reordering Tab Pages
command | description |
---|---|
:tabm2 | moves the current tab to appear after tab 2. To move this tab to the first position, use :tabm0. To move this tab to the last position, just use :tabm |
:tabm[ove] +[N] | Move the current tab page N places to the right (with +) or to the left (with -). |
:tabm[ove] -[N] | Move the current tab page N places to the right (with +) or to the left (with -). |
:tabmove - | move the tab page to the left |
:tabmove -1 | as above |
:tabmove + | move the tab page to the right |
:tabmove +1 | as above |
Open question - how to move buffers from one tab to another or into split windows when wanted? It may not be such a great idea to work on multiple windows in a given tab, unless there is a need for it. Always prefer to work only on one buffer in a tab because that will give maximum visibility into the open buffer. If there is a need to work on another file/buffer at the same time, put it in a separate tab. For now, just go to the tab that you want to open split windows in and use :vsp to split it and open the file that you want to view as a split window.
How to open buffers in tabs from netrw: One way is to change netrw settings using vimrc. The other way is, open it in a window and move it into a tab using Cntrl W T
How to move an existing window (split) and put it in a new tab? Ctrl W followed by T
Splits and Multiple windows
You can also use the “windows command mode” with navigation keys to change a window’s position: Check out :help window-moving for more information
command | description |
---|---|
Ctrl w | gives you the “windows command mode”, allowing the following modifiers |
Ctrl w + R | To rotate windows up/left |
Ctrl w + r | To rotate windows down/right |
Ctrl w + L | Move the current window to the “far right” |
Ctrl w + H | Move the current window to the “far left” |
Ctrl w + J | Move the current window to the “very bottom” |
Ctrl w + K | Move the current window to the “very top” |
Ctrl+W +/- | increase/decrease height (ex. 20<C-w>+) |
Ctrl+W >/< | increase/decrease width (ex. 30<C-w><) |
Ctrl+W _ | set height (ex. 50<C-w>_) |
Ctrl+W (pipe) | set width (ex. 50<C-w>) |
Ctrl+W = | equalize width and height of all windows |
^Ws or :split↵ | split window in two |
^Wn or :new↵ | create new empty window |
^Wo or :on↵ | make current window one on screen |
^Wj ^Wk | move to window below, above |
^Ww ^W^W | move to window below, above (wrap) |
:e filename | Edit another file |
:split filename | Split window and load another file |
CTRL-W v | Vertical split current window |
CTRL-W s | Horizontal split current window |
CTRL-W Arrow Up | Move cursor up a window |
CTRL-W CTRL-W | Move cursor to another window (cycle) |
CTRL-W_ | Maximize current window |
CTRL-W= | Make all equal size |
10 CTRL-W+ | Increase window size by 10 lines |
:vsplit file | Vertical split |
:sview file | Same as split, but readonly |
:hide | Close current window |
:only | Keep only this window open |
:ls | Show current buffers |
:b 2 | Open buffer #2 in this window |
:bd[n] | Close current buffer. If [n] is given close buffer #n. #n can be gathered with :ls. |
See also: :help CTRL-W
These mappings will make it a little easier.
- (pressing + is too difficult. = is + without having to press shift.)
- (pressing - is easy enough.)
nnoremap <Leader>= :vertical resize +5<CR>
nnoremap <Leader>- :vertical resize -5<CR>