4

I'd like to add the short hostname to the statusline in vim. I've looked around but short of installing the Powerline plugin, it doesn't seem posible. Below is my .vimrc. How can I add thehostname variable to it?

let hostname=system('hostname -s')
set laststatus=2
set statusline+=%F\ %P\ %c:%l

UPDATE: to remove the '^@' which appears after the hostname on the vim status line (I suspect is a newline charcter from the hostname call) I use this instead:

let hostname=system('echo -n $LOGNAME@$(/bin/hostname -s)')
2

statusline can be modified to include variable names.

For example, I'm using the statline plugin, so my statuslineis:

statusline=[%{StatlineBufCount()}:%n] %<%1*[%f]%*%2*%h%w%m%r%* %y[%{&ff}%{g:statline_encoding_separator}%{strlen(&fenc)?&fenc:g:statline_no_encoding_string}] %5*%{&paste?(g:statline_show_paste_string):''}%*%5*%{&list?(g:statline_show_list_string):''}%*%=%-14( L%l/%L:C%c%V %)%P %4*%{exists('g:sfe_availableScms')?SfeStatus():''}%* %3*%{exists('g:loaded_syntastic_plugin')?SyntasticStatuslineFlag():''}%*%3*%{StatlineTabWarning()}%*%3*%{StatlineTrailingSpaceWarning()}%*

All of which is concatenated by the plugin itself.

In your case, you can use:

let hostname=system('hostname -s')
set statusline+=%F\ %P\ %c:%l\ %{hostname}
2
  • Thanks! I tried everything but the curly braces – Server Fault Jan 14 '16 at 18:30
  • Do you know how to change the time with which the statusline gets updated? It would e useful for things that won't change at all or change very rarely. – Durga Swaroop Jul 19 '16 at 11:54
4

vim has a hostname() function (see :h hostname()), so you can do

:set statusline=%{hostname()}
2
  • Do you know how to change the time with which the statusline gets updated? It would e useful for things that won't change at all or change very rarely. – Durga Swaroop Jul 19 '16 at 11:54
  • This approach is more reliable. – jdhao Apr 17 '19 at 13:12
1

The default status line can be more or less emulated this way:

:set statusline=%f\ %h%w%m%r%=%-14.(%l,%c%V%)\ %P

where

%f - buffer name (path to a file, or something)
%h - help flag
%w - preview flag
%m - modified flag
%r - readonly flag
%= - separater between the left (buffer name) and the right (ruler) parts
%-14.(...%) - minimum field width == 14
%l - line
%c - column
%V - virtual column
%P - percentage

One way to add hostname would be to use hostname() function:

:set statusline=%#CursorColumn#\ %{hostname()}\ %*\ %f\ %h%w%m%r%=%-14.(%l,%c%V%)\ %P

where

%#CursorColumn# - sets highlight group
%{...} - embeds arbitrary expression
%* - resets highlight group

You might want to run the following script to see the list of available groups and what they look like:

:so $VIMRUNTIME/syntax/hitest.vim

Or you can simply enclose the hostname in brackets:

:set statusline=[%{hostname()}]\ %f\ %h%w%m%r%=%-14.(%l,%c%V%)\ %P

See my other answer if you want to get closer to the way status line is drawn by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.