Search code examples
vimxming

Update titlestring in Vim when I enter/leave the commandline


Correct setting: add |redraw at the end

augroup CmdLineStatus_to_Title
    autocmd!
    autocmd CmdlineEnter * set titlestring=CommandlineMode|redraw
    autocmd CmdlineEnter * set titlestring=%F\ %{v:servername}\ %{mode()}
augroup END

Original Post

I tried the following auto-command, and fail to achieve what I wanted. Ideally, I would like to append to the end of the titlestring an identifier when I move the cursor to the command line (by pressing : key).

" Global setting
setglobal titlestring=%F\ %{v:servername}\ %{mode()}

" The Auto command group
augroup CmdLineStatus_to_Title
    autocmd!
    autocmd CmdlineEnter * let &l:titlestring="CommandlineMode"
    " autocmd CmdlineLeave * let &l:titlestring=%F\ %{v:servername}\ %{mode()}
augroup END

There are multiple problems:

  1. The titlestring, and/or the title of the Gvim window does not update when I ENTER the command line. It refreshes when I LEAVE the command line mode.
    • Also, the &l: flag did not restrict the setting native to the buffer: I get the constant title string as "CommandlineMode" for new files opened in new buffer/tabs. (One particular file is opened through a nnoremap <> <Plug>VimwikiMakeDiaryNote mapping, with no command line operation involved.)
  2. Also, I cannot simply "append" a customized string towards the end of the titlestring. This should be a syntax problem
    • Also related: the line in augroup that has been commented out shall bring about the following error when I actually LEAVE the command line. Please also advise how to restore the titlestring settings when I leave the command line.

enter image description here

Inspiration and credits:

  • Naumann in this post, I can already tell the titlestring the current mode of Vim.

Machine specification: the reported trouble occurred on Gvim running through X11 on a WSL-Linux-shell on a Windows 10 machine. The %{mode()} "variable" does update when I switch back and forth between the Normal mode and the Insert mode. Chances are this could be a Xming problem? Will test by updating the Gvim.exe that is installed natively to the Windows 10 machine.


Solution

  • When you look at :help 'titlestring', you'll notice that this is a global option. As there is only a single window title for a Vim instance, local options don't make sense. You can update the contents based on the current window / buffer / whatever (using :autocmd), though.


    Vim tries to avoid unnecessary screen updates. To force an update at a certain point in time :redraw can be used. The following works for me:

    autocmd CmdlineEnter * set titlestring=CommandlineMode|redraw
    

    Regarding your syntax error: As you use the :let &option syntax, the right-hand side must be a Vim expression (here: interpreted as a String), so you must enclose it in (single or double) quotes. The escaping of spaces with backslashes is for the :set command. This is easy to confuse :-)