Is there an option to apply a setting in all tabs and windows, e.g. apply :set number
to all open tabs?
3 Answers
Yes, there is! You can accomplish this with :tabdo
and :windo
. For your case specifically:
:tabdo windo set number
See :help :tabdo
and :help :windo
for more information.
-
7
:tabdo
is not sufficient.:help :tabdo
says that it operates in the current window of each tab page. To execute a command in all windows in all tabs you'll need:tabdo windo set number
.– garyjohnCommented Apr 12, 2017 at 0:13 -
:tabdo windo set number
gets the job done, but I don't really like it since it actually switches to each tab and window before running the command. After it finishes running, you'll end up on the last window of the last tab.
I think this is a cleaner way to set options in all windows without changing the current tab and window:
function! s:set_all(option, val, ...) abort
let val = eval(a:val)
for t in range(1, tabpagenr('$'))
for w in range(1, tabpagewinnr(t, '$'))
if gettabwinvar(t, w, '&buftype') !=# ''
continue
endif
call settabwinvar(t, w, '&'.a:option, val)
endfor
endfor
endfunction
command! -complete=option -nargs=+ SetAll call s:set_all(<f-args>)
Running :SetAll number 1
will enable the number column in all windows, except for the ones that have buftype
set, such as help windows.
This is the same as running: :tabdo windo let &number = 1
. So, the second argument needs to be an assignable expression. Instead of :set filetype=ini
, it would be :SetAll filetype "ini"
.
-
2I'll also add (beside changing the current tab/window) that
:tabdo windo
may trigger autocommands, and slow down the option settings. While Tommy A's solution cannot be used out-of-the-box, it's the one to prefer in plugins. Commented Apr 12, 2017 at 8:00
Another way to keep the current tab:https://vi.stackexchange.com/a/31903
I personally use buffers instead of tabs so I have:
" Toggle line numbers for all buffers/windows/tabs
function! ToggleLineNumbers()
let current_buffer = bufnr()
" tabdo windo bufdo set number!
" updated according to the comment from D. Ben Knoble:
" tabdo windo set number! " note by goodpen: if a buffer is opened in 2 windows, this may call `:set number!` twice?
bufdo set number!
execute 'buffer' current_buffer
endfunction
-
1You probably don't want
bufdo
insidetabdo windo
, since that would cause "set number!
in all buffers" to be run once by each window. Either ofbufdo set number!
(all buffers) ortabdo windo set number!
(all windows in all tabs) should be sufficient. Each leaves the last buffer (resp. tab and window) as the current buffer (resp. tab and window), which Tommy's answer addresses.– D. Ben Knoble ♦Commented Aug 9, 2022 at 18:23