History
Article Edit this page Discussion

Non-native fileformat for your statusline

From Vim Tips Wiki

(Redirected from VimTip736)
Jump to: navigation, search

Tip 736 Previous Next Created: May 30, 2004 Complexity: intermediate Author: Grant Bowman Version: 5.7


I like knowing when a file I open is detected as having a non-native file format.

The way to provide this is a function called from within your statusline. Add the following lines to your vimrc and modify as you prefer. I run unix, but this can be slightly altered for other platforms too. If you are on the mac or dos platforms, simply substitute unix for your platform name in the fuction.

function ShowFileFormatFlag(var)
  if ( a:var == 'dos' )
    return '[dos]'
  elseif ( a:var == 'mac' )
    return '[mac]'
  else
    return ''
  endif
endfunction
hi User1 term=bold cterm=bold ctermfg=red ctermbg=darkblue

I call it and color the output of this function red with a blue background. Add the following string to your :set statusline= line in your vimrc.

%1*%{ShowFileFormatFlag(&fileformat)}%*

The %* returns the highlighting to normal, whatever happens to be set at the time. This is a function that is called each time the statusline is drawn. It passes in the value of the variable fileformat, used locally in the function above via the a:var variable.

[edit] References

A wiki page that speaks to Vim's auto-detection of fileformat is located at http://www.vi-improved.org/wiki/index.php/FileFormat

[edit] Comments

I use a more neutral function since I move back and forth between Linux and Win32. I keep the same vimrc, and this function adapts to the system I'm working on.

function! FileFormatCorrect()
 return
 \(&ff == 'unix' && !has('unix')) ||
 \(&ff == 'dos' && (!has('win32') && !has('win95'))) ||
 \(&ff == 'mac' && !has('mac'))
 \ ? ','.&ff : ''
endfunction

Since the status line works best when functions are quicker, the following is a little better on slower terminals. It defines a quicker function, but the definition depends on the file system.

if has('unix')
  function! FileFormatCorrect()
    return (&ff == 'unix')? ','.&ff : ''
  endfunction
elseif has('mac')
  function! FileFormatCorrect()
    return (&ff == 'mac')? ','.&ff : ''
  endfunction
elseif has('win16') || has('win32') || has('dos16') || has('dos32')
  function! FileFormatCorrect()
    return (&ff == 'dos')? ','.&ff : ''
  endfunction
else
  function! FileFormatCorrect()
    return ','.&ff
  endfunction
endif

How about:

function ShowFileFormatFlag(var)
 return '['.a:var.']'
endfunction
set rulerformat=%43(%l,%L\ %c\ %t\ %{ShowFileFormatFlag(&fileformat)}\%)

Note that the function itself is unnecessary:

let g:main_ff = substitute(&ffs, ',.*', '', '')
set stl=...%{&ff==g:main_ff?'':'['.&ff.']'}...

where "..." is whatever else you want in your statusline. For example, to simulate 'ruler':

set stl=%<%f\ %h%m%r%{&ff==g:main_ff?'':'['.&ff.']'}%=%-14.(%l,%c%V%)\ %P

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Non-native fileformat for your statusline"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation