[Help needed] customizing nvim terminal title

Can anyone please help me customize the nvim terminal title?
I couldn’t customize the tab number (1 of 4)

If i open 4 tabs (A.cpp, B.cpp, C.cpp, D.cpp) via nvim, I want my nvim terminal title to look like this
A.cpp - nvim (1 of 4)

my code:

set title

if has('title') && (has('gui_running') || &title)
    set titlestring=
    set titlestring+=%f 	" filename
    set titlestring+=%h%m%r%w 	" Flag
    set titlestring+=\ -\ %{v:progname}
  " set titlestring+=\ -\ %{substitute(getcwd(),\ $HOME,\ '~',\ '')}

This should do about what you’re looking for:

function _G.current_tab()
  local curr_buf = vim.fn.bufnr()
  local total = 0
  local curr_tab

  for i = 1, vim.fn.tabpagenr('$') do
    total = total + 1
    for _, bufnr in ipairs(vim.fn.tabpagebuflist(i)) do
      if bufnr == curr_buf then
        curr_tab = i

  return string.format('(%d of %d)', curr_tab, total)

vim.opt.titlestring = [[%f %h%m%r%w - %{v:progname} %{luaeval('current_tab()')}]]

Note that this uses nvim 0.5 features (vim.opt, vim.fn, etc.). Also not necessarily the best way to do it, but it does work from some quick testing.

Actually, I way overcomplicated that :sweat_smile:

Can be done a lot simpler:
:lua vim.opt.titlestring = [[%f %h%m%r%w %{v:progname} (%{tabpagenr()} of %{tabpagenr('$')})]]

Could also do this with :set title but I like Lua and it’s actually kinda cleaner this way.

1 Like

@smolck where should i put it?
do i have to make init.lua or something?
i am using init.vim

You should be able to drop the : at the start and just put lua <rest of cmd> in your init.vim. Or use multiple set titlestring+= calls to do the same but w/out any Lua.

1 Like