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:
- 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 annoremap <> <Plug>VimwikiMakeDiaryNotemapping, with no command line operation involved.)
- Also, the
- 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
augroupthat has been commented out shall bring about the following error when I actually LEAVE the command line. Please also advise how to restore thetitlestringsettings when I leave the command line.
- Also related: the line in
Inspiration and credits:
- Naumann in this post, I can already tell the
titlestringthe 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.

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
:redrawcan be used. The following works for me:Regarding your syntax error: As you use the
:let &optionsyntax, 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:setcommand. This is easy to confuse :-)