Vim Tips Wiki
Register
Advertisement
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Tip 736 Printable Monobook Previous Next

created 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.

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

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

Comments from tip 26

The following comments were originally from Change end-of-line format for dos-mac-unix (tip 26) which has now been merged to File format (tip 1585). I will be cleaning this tip soon. JohnBeckett 10:45, September 19, 2010 (UTC)

You can display the file format for the current buffer in the status line:

set statusline=%<%f%h%m%r%=%{&ff}\ %l,%c%V\ %P

All I care about is if the file format is not unix. If it's not, I want a big red warning. That way I'm not the jerk who checks in a file that causes every line to get modified by the diff patch.

So, I added this to my existing statusline:

%9*%{&ff=='unix'?'':&ff.'\ format'}%*

Here's what is does:

%9*
\- Change highlighting to user setting #9 (see :he hl-User1..9)
%{
\- Begin evaluating as expression until } is encountered
&ff=='unix'?'':&ff.'\ format'
\- This is a ternary that returns either an empty string, or 'XX format'
}
\- This marks the end of the expression
%*
\- Restores normal highlight

So, how do you use it? First I call:

:set statusline?

Which returns:

statusline=%<%f :: %{TagName()} %(%h%m%r %)%=%-15.15(%l,%c%V%)%P

But, remember that if you want to set a status line you must escape all white space. So that line would have to be entered as:

statusline=%<%f\ ::\ %{TagName()}\ %(%h%m%r\ %)%=%-15.15(%l,%c%V%)%P

So when I added my modification I have:

:set statusline=%<%f\ :%9*%{&ff=='unix'?'':&ff.'\ format'}%*:\ %{TagName()}\ %(%h%m%r\ %)%=%-15.15(%l,%c%V%)%P

But then, to make the user highlighting #9 big and red I started by viewing all the existing highlighting configurations (I'm too lazy to write my own) by calling:

:hi

The entry titled ErrorMsg looked good to me so I copied its settings which were:

term=standout cterm=bold ctermfg=7 ctermbg=1

I then called:

:hi User9 term=standout cterm=bold ctermfg=7 ctermbg=1

Now my status line is unchanged and uncluttered, unless I have opened a dos file. That's pretty cool.

I wrote this in REAL basic terms, because I really wish someone had explained it to me like this. I hope it's well received.


If you want to have the fileformat always show, but only stand out if it isn't unix, here's what you would put in your ~/.vimrc:

highlight User9 term=standout guibg=#ff0000 cterm=standout set statusline=%9*%{&ff=='unix'?:'['.&ff.']'}%*%{&ff=='unix'?'['.&ff.']':} set laststatus=2

If you would like a very long and possibly overly complete statusline (I include hex code for char under cursor, byte position in file, filetype, fileformat and file encoding in mine :) = geeks dream) you can use this:

highlight User9 term=standout guibg=#ff0000 cterm=standout set statusline=%<%f\ %h%m%r%=[char:%B][POS:%Oh]%y%9*%{&ff=='unix'?:'['.&ff.']'}%*%{&ff=='unix'?'['.&ff.']':}%{\"[\".(&fenc==\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\ &bomb)?\",BOM\":\"\").\"]\ \"}%k\ %-14.(L%l,C%c%V%) set laststatus=2


Advertisement