Wikia

Vim Tips Wiki

Automatically set screen title

Talk0
1,612pages on
this wiki
Revision as of 10:05, April 28, 2014 by 114.32.241.146 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 1126 Printable Monobook Previous Next

created February 9, 2006 · complexity basic · author Michael Stilkerich · version 6.0


The screen's title can automatically be updated to the name of the currently opened file, or whatever you like.

Just add the following to your vimrc:

let &titlestring = hostname() . "[vim(" . expand("%:t") . ")]"
if &term == "screen"
  set t_ts=^[k
  set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
  set title
endif

To create ^[, which is escape, you need to enter CTRL+V <Esc>

This will make a title like hostname[vim(filename)], but that's only my personal preference. That way, you can easily keep track on which window in your screen you have which file opened.

CommentsEdit

Just found this on a website. Using:

autocmd BufEnter * let &titlestring = hostname() . "[vim(" . expand("%:t") . ")]"

instead of statically setting titlestring even updates the title when switching buffers/opening other files in the same vim session.


I use:

set titlestring=%t%(\ %M%)%(\ (%{expand(\"%:p:h\")})%)%(\ %a%)\ -\ %{v:servername}

By placing the expand expression inside a %{} pair, it is reevaluated automatically and doesn't require an autocommand around it.


I tried it, and after editing a mail within mutt, the screen title keeps saying 'Thanks for flying Vim' instead of being reset to 'mutt'. Any ideas to fix this?



I use this to avoid "Thanks for flying Vim"

        set t_ts=^[k
        set t_fs=^[\
        auto BufEnter * :set title | let &titlestring = 'v:' . expand('%')
        auto VimLeave * :set t_ts=^[k^[\

Oh this is really a problem with all apps that fork off Vim. You can hack around this in mutt using

source "~/.mutt/editorrc.$TERM"

in your muttrc and create a config for each terminal type, e.g. editorrc.screen:

set editor="sh -c 'vim $0 && echo -ne \"\\033kmutt\\033\\134\"' "

This will reset the title to mutt after leaving Vim. The problem with this is that you need an extra file for each terminal type you use, lots of the probably containing only set editor=vim

I don't know if there's a possibility to have mutt ignore silently if a sourced file does not exist in which case you could set editor to a default value and override it only for some special terminal types.

btw, since we left the Vim scope now anyway, here's the rest of my title configuring stuff

my .bashrc contains

case $TERM in
 xterm*|rxvt*)
 PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}[`basename ${PWD}`]\007"'
 ;;
 screen*)
 PROMPT_COMMAND='echo -ne "\033k\033\134\033k${HOSTNAME}[`basename ${PWD}`]\033\134"'
 ;;
 *)
 ;;
esac

and my .screenrc contains

shelltitle '] |bash'

This will have the shell display hostname[last component of directory] whenever no program is running and enable screens heuristic to display the program name whenever a program is running.

The fallback value bash is almost never displayed since it will be overriden by the shell prompt. For shells other than bash you should be able to configure this in a similar way. (there are examples in the screen manual). The characters in shelltitle before the pipe sign need to fit the end of your shell prompt, which is "] " in my case.


Around Wikia's network

Random Wiki