vim :AnsiEsc - how to have them on by default?

Viewed 1832

I have log files (let say with the extension *.smt2) with colored log entries, where colors are defined by ansi escape codes. I installed the vim plugin based on AnsiEsc.vim.

Now when I open my vim editor I need to type :AnsiEsc. How can I add :AnsiEsc to my .vimrc to be in my default view?

1 Answers

Your autocmd will only work for the first file that is passed as a command-line argument, as the :AnsiEsc command only applies to the current buffer. It will miss files opened via :edit or :split. Better use the BufRead event:

autocmd BufRead * AnsiEsc

Also, I wonder whether you only use Vim to view log files (that would be a pity)? The :AnsiEsc command may interfere with editing other kind of files.

The usual solution for that is defining a custom filetype, e.g. log. You can then enable the plugin for those files via the following script in ~/.vim/ftplugin/log.vim:

" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
    finish
endif
AnsiEsc

(You also need to have :filetype plugin on in your .vimrc.)

To open a log file, you have to specify the filetype: vim "+setf log" logfile, or later inside Vim via :setf log. Of course, if the logfile names follow a pattern or have a certain common string inside, you can be fancy and write a filetype detection; see :help new-filetype. Or you write a shell alias, e.g. alias vimlog='vim +setf\ log'

Related