Want the title of the open program on my tab using neovim

So I was using my newly setup neovim to write an asteroids clone in python, and I had several open tabs and realized something : All of these tabs I have open to reference other code doesn’t have the title on the tab.

So, I asked chat Jippidy how to alter my init.lua file so that the title of the program appears at the top of the tab when neovim is open.

After several attempts and changes I can confidently say : What I wrote does not work!

Now, below I have included my code.

Here are some things I discovered.

  1. For whatever reason the input is reaching the output but the machine isn’t sure what to do with it.

  2. This is compatible with iterm2, what I am using, as writing echo -ne ‘\033]0;TestTitle\007’ in my terminal makes TestTitle appear in the terminals tab.

  3. I COULD make iterm2 display neovim + arguments which would solve this problem , but that’s how Losers think.

  4. My current code outputs this message in the bottom of the neovim terminal rather than changing the Terminal Title at the top of the tab: Output: -ne !]0;init.lua^G

  local filename = vim.fn.expand '%:t' -- Get the name of the current file
  if filename == '' then
    filename = '[No Name]'
  end
  -- Terminal escape sequence to set the terminal title
  --local title_string = string.format("\27]0;%s\7", filename)
  local title_string = string.format('\033]0;%s\007', filename)
  -- Execute the sequence to set the title
  vim.fn.jobstart({ 'sh', '-c', 'echo -ne "' .. title_string .. '"' }, {
    on_stdout = function(_, data)
      if data then
        vim.notify('Output: ' .. table.concat(data, '\n'), vim.log.levels.INFO)
      end
    end,
    on_stderr = function(_, data)
      if data then
        vim.notify('Error: ' .. table.concat(data, '\n'), vim.log.levels.ERROR)
      else
        vim.notify('Error occurred, but no details', vim.log.levels.ERROR)
      end
    end,
    on_exit = function(_, exit_code)
      vim.notify('Job exited with code ' .. exit_code, vim.log.levels.INFO)
    end,
  })
end

-- Autocommands to trigger title setting
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWinEnter', 'TabEnter' }, {
  pattern = '*',
  callback = function()
    set_terminal_title()
  end,
})```

What were your attempts?
Have you checked :h title and :h titlestring?