2

My init.vim code:

cd ~/Documents/cp
nnoremap <leader>lcd :tabdo lcd %:p:h<cr>

suppose, i have 4 tabs opened in nvim
via using nvim -p A.cpp B.cpp C.cpp D.cpp

A.cpp
B.cpp
C.cpp
D.cpp

When I need to temporarily change the 4 tab's current directory as the default one,
typing \lcd changes all the visible tabs to local current directory.

But tabdo has a side effect.
It helps to use a specific command in all opened tabs
while doing that, vim goes to the last tab (which i don't want)

So How can i tabdo all buffers without changing view of the current tab?
I didn't find any simple solution.

PS: I've checked some resources.
(1) Windo and restore current window
(2) How to apply a setting in all open tabs and windows?

but didn't find for tabdo command in 1st resource

tried the 2nd resource code, but it goes to the last tab and removes it & turns it into current buffer
if i have A.cpp opened as current tab, 2nd resource code will do this
A.cpp B.cpp C.cpp A.cpp (removes D.cpp)

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Mega Bang
  • 199
  • 11

1 Answers1

4

You can save and restore the current tab around the :tabdo operation. You can use tabpagenr() to get the position of the current tab and later you can pass that number to :tabnext to change back to this tab. This is better done in a function, since you can easily run multiple commands and also use a local variable to store the current tab number.

function! TabLcd()
    let current_tab = tabpagenr()
    tabdo lcd %:p:h
    execute 'tabnext' current_tab
endfunction

nnoremap <silent> <leader>lcd :call TabLcd()<cr>

Note that you need :execute for the :tabnext in order to pass the count with the tab number from a variable.

filbranden
  • 28,785
  • 3
  • 26
  • 71