How can I check if a terminal exist before I create one

I have this remap on my init.vim nnoremap <leader>t :bel 10sp term://zsh<CR>
it creates a terminal buffer at the bottom of the window. but often I end up creating many terminal buffers that become hard to manage. How can I check if a terminal is created and switch to if it already exists?

1 Like

dude this is an amazng mapping that i never thought of!!! thanks!
i dunno how to answer your question tho :stuck_out_tongue:

use a plugin like GitHub - akinsho/nvim-toggleterm.lua: A neovim lua plugin to help easily manage multiple terminal windows

1 Like

haha… you welcome. I am happy you find it useful.

Thank you, I will check it out

Here is a solution in the shape of a function to be put somewhere in your init.vim.

let s:command = 'term://bash'
let s:vimlcmd = 'bel 10sp'
let s:vimlcmd = 'edit'
fu! TerminalOnce()
  if exists('s:currterm')
    if bufexists(s:currterm)
      exec s:vimlcmd.' '.s:currterm
    else
      unlet s:currterm
      call TerminalOnce()
    endif
  else
    exec s:vimlcmd.' 's:command
    let s:currterm = bufname()
  endif
endf

nmap <leader>t :call TerminalOnce()<cr>

In this case I use the variable vimlcmd to chose how you wanna open the terminal. For me I rather use the edit option.

By the way, I’ve developed a plugin that already comes with a wild terminal like that, among other things. Future implementations will offer multiple terminals. Check the gifs in the readme.

2 Likes

this work like a charm… Thank you