3

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)

1
  • Complete aside: forcibly setting the directory in your vimrc (init.vim) is something I really recommend against.
    – D. Ben Knoble
    Commented Jul 24, 2021 at 18:24

1 Answer 1

5

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.

1
  • 1
    it works.. thank you very much :)
    – Mega Bang
    Commented Jul 25, 2021 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.