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

帅比萌擦擦* 提交于 2021-02-08 06:38:34

问题


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:


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'



来源:https://stackoverflow.com/questions/47475460/vim-ansiesc-how-to-have-them-on-by-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!