1

Related to this post, how to tabdo all the visible buffers without changing view of the current tab?

function! TabLcd()
    let current_tab = tabpagenr()
    tabdo <commands>
    execute 'tabnext' current_tab
endfunction

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

The code is written in Vimscript. I want to implement it in Lua.

Can anyone help me convert this?

6
  • 1
    At first glance your conversion looks ok. What is wrong about you conversion? What are the symptom of failure? What is it suppose to do and what it is actually doing or not doing? Commented Oct 9, 2023 at 11:11
  • \lcd command doesn't work but \cd works.
    – Mega Bang
    Commented Oct 10, 2023 at 13:28
  • What are the symptoms about \lcd failure? Commented Oct 10, 2023 at 13:30
  • Huh, it worked. I didn't have much belief in myself. I forgot to mention the feature. You can see the related post. My permanent local directory is ~/Documents/cp. If I want to change it to a temporary local directory. Then pressing \lcd executes this command tabdo lcd %:p:h in all opened tabs. Mnemonic lcd equal to Local Current Directory.
    – Mega Bang
    Commented Oct 10, 2023 at 13:42
  • 1
    Yep, mine worked fine too.
    – Mega Bang
    Commented Oct 10, 2023 at 13:55

2 Answers 2

0

My proposition would be:

function TabLcd()
    local current_tab = vim.fn.tabpagenr()
    vim.cmd("tabdo lcd %:p:h")
    vim.cmd("execute 'tabnext' " .. current_tab)
end

vim.api.nvim_set_keymap('n', '<leader>lcd', ':lua TabLcd()<CR>', { noremap = true, silent = true })
1
  • Yeah, my code worked fine. I have submitted the solution for a general format.
    – Mega Bang
    Commented Oct 10, 2023 at 13:57
0

Lua format

function TabLcd()
    local current_tab = vim.fn.tabpagenr()
    vim.cmd("tabdo <commands>")
    vim.cmd("execute 'tabnext' " .. current_tab)
end

vim.api.nvim_set_keymap('n', '<leader>lcd', ':lua TabLcd()<CR>', { noremap = true, silent = true })
  1. function TabLcd() defines a Lua function named TabLcd.

  2. local current_tab = vim.fn.tabpagenr(): It stores the current tab page number in the current_tab variable using a Neovim built-in function vim.fn.tabpagenr().

  3. vim.cmd("tabdo <commands>"): This line executes <commands> in all open tabs. You would replace with the specific commands you want to run in each tab. For example, if you want to run :command1 in each tab, you would use vim.cmd("tabdo command1").

  4. vim.cmd("execute 'tabnext' " .. current_tab): After executing the commands in all tabs, it switches back to the original tab also defined as current_tab.

  5. Set any keymap (in this case \lcd), And you're a go.

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.