Non-native fileformat for your statusline
From Vim Tips Wiki
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
- Change end-of-line format for dos-mac-unix
- Show fileencoding and bomb in the status line
- :help 'statusline'
- :help hl-User1..9
- :help user-functions
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
