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?

Mega Bang
  • 199
  • 11

2 Answers2

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 })

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
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.

Mega Bang
  • 199
  • 11