cpow / neovim-for-newbs

a simple lua neovim configuration for newbs
548 stars 103 forks source link

What terminal to integrate with NeoVim #1

Closed mloskot closed 8 months ago

mloskot commented 11 months ago

Here I dare a request aka suggestion to your excellent series which is helping me immensely to learn NeoVim 😊


I have tried to configure the toggleterm following the typecraft'y way of structuring the configuration .lua files and here is what I came up with. It seems working. I even managed to integrate lazygit :-) But, I'm not confident this part is sound:

config = function()
  require("toggleterm").setup({
...

Here is my nvim/lua/plugins/toggleterm.lua:

function _G.set_terminal_keymaps()
  local opts = { buffer = 0 }
  vim.keymap.set('t', '<esc>', [[<C-\><C-n>]], opts)
  vim.keymap.set('t', 'jj', [[<C-\><C-n>]], opts)
  --vim.keymap.set('t', '<C-h>', [[<Cmd>wincmd h<CR>]], opts)
  --vim.keymap.set('t', '<C-j>', [[<Cmd>wincmd j<CR>]], opts)
  --vim.keymap.set('t', '<C-k>', [[<Cmd>wincmd k<CR>]], opts)
  --vim.keymap.set('t', '<C-l>', [[<Cmd>wincmd l<CR>]], opts)
  vim.keymap.set('t', '<C-w>', [[<C-\><C-n><C-w>]], opts)
end

function _G.toggleterm_lazygit()
  local Terminal = require('toggleterm.terminal').Terminal

  local lazygit  = Terminal:new({
    cmd = "lazygit",
    direction = "float",
    float_opts = {
      border = "single",
      -- Enable full screen: https://github.com/akinsho/toggleterm.nvim/issues/505
      width = vim.o.columns,
      height = vim.o.lines,
    },
    -- function to run on opening the terminal
    on_open = function(term)
      vim.cmd("startinsert!")
      vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
    end,
    -- function to run on closing the terminal
    on_close = function(term)
      vim.cmd("startinsert!")
    end,
  })

  lazygit:toggle()
end

return {
  'akinsho/toggleterm.nvim',
  version = "*",
  opts = { -- TODO: Can this be used along with the config = function?
  },
  config = function()
    require('toggleterm').setup({  -- TODO: Why/When there is need for explicit call to setup inside Lazy config function?
      open_mapping = [[<c-\>]], 
      direction = "float",
      hide_numbers = true,
      insert_mappings = true,
      terminal_mappings = true,
      start_in_insert = true,
      close_on_exit = true,
    })

    -- if you only want these mappings for toggle term use term://*toggleterm#* instead
    vim.cmd('autocmd! TermOpen term://* lua set_terminal_keymaps()')

    vim.api.nvim_set_keymap("n", "<leader>lg", "<cmd>lua toggleterm_lazygit()<CR>", { noremap = true, silent = true })
  end
}

-- NOTE: https://github.com/LazyVim/LazyVim/discussions/193
javiersanzgarcia commented 10 months ago

Hi @mloskot,

You don't need to use an integrated terminal inside Neovim, with next command you can open directly, vertically and horizontally:

image

keymap("n", "<Leader>tv", ":botright vnew <Bar> :terminal<cr>") keymap("n", "<Leader>th", ":botright new <Bar> :terminal<cr>")

For another hand, if you prefer dedicate a special management shell tool, I strongly recommend TMUX, and you can find info about how to configure it and use it in the channel of @cpow

Hope this helps to you,

mloskot commented 10 months ago

Thank you @javiersanzgarcia !

mloskot commented 8 months ago

Since my question has been answered, I'm closing this :)