vim: open file and enable autoread in one command - vim

G'day!
I have log file ~/log.txt and want to open it with vim and enable autoread option ( that makes vim reload file on change ) in single command.
I don't want to enable autoread manually with
:set autoread
I don't want to change .vimrc because I want autoread work only for log file.
Thank you!

You can tell Vim in your .vimrc file to add settings only to specific files.
autocmd BufNewFile,BufRead /home/foo/log.txt setlocal autoread
This autocommand will enable autoread automatically as soon as you open the log file.
Type :help :autocmd for an in-depth documentation of autocommands.

Related

vim filetype settings lost when resourcing vimrc

Following the instructions here I have an ftdetect file, ~/.vim/ftdetect/cheat.vim with this line:
au BufNewFile,BufRead *.cheat/* set filetype=cheat
This loads a simple config file at ~/.vim/ftplugin/cheat.vim:
set statusline=%t
set statusline+=\ %P
set statusline+=%#todo#
set nonumber
It loads fine, but when I source ~/.vimrc the settings for cheat.vim are lost.
The best long-term solution is to avoid having your vimrc overwrite filetype settings if executed directly by using local options and similar, but the simplest fix is often to re-edit the file. Type
:edit
And hit Enter.
This can be shortened to just :e in interactive use, and a mapping is easily created:
nnoremap <silent> <leader>e :edit<CR>
I suggest reading the help pages on vim’s startup, init files, source command, edit command, and the various ways to tune things local to a single buffer (e.g., setlocal, map-<buffer>, autocmd pattern <buffer>).

How can I apply vimrc conf file in .py

when I run
vim good.html
and
:verbose set et?
expandtab
Last set from ~/.vimrc
but when I run
vim good.py
and
:verbose set et?
expandtab
Last set from /usr/share/vim/vim74/ftplugin/python.vim
I want apply ~/.vimrc file in .py, not python.vim
Yesterday I all is fine but today suddenly path was changed
please someone teach me how can I change the path
Put this into your .vimrc:
autocmd VimEnter *.py set expandtab
or if you want to have the configuration of .vimrc to be executed after all plugins being loaded - in case they have changed some settings -, you can add this line:
autocmd VimEnter * source ~/.vimrc
Note: It could have a side-effect depending on the content of your .vimrc because the latter actually will be executed twice (at the begining and at the end of vim startup) so you need to consider that.
Concerning Plugins now, if they are logged in some specific folders like .vim or vim installation path they will be loaded automatically unless you removed them or run some specific commands to be ignored.
Vim has also the ability to detect the type of file which is being edited, and this occurs when the option filetype is activated and probably this is what happened to you.
So typing :filetype will confirm that. Maybe you can desactivate it for some specific files if you wish. It is up to you !
:help VimEnter
VimEnter
VimEnter After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.

What exactly happens when you change a file type in vim?

If I have an apache file in /etc/apache2/sites-available/www.example.com and I set its filetype like so
:set filetype=apache
What does that do? Does that change the file at all? Is it only reflected in the instance of vim? The session of vim? I can manually set the filetype, but then vim warns me that I am in read only mode (/etc/apache2 needs root access). If I open vim as root, I won't get the warning, but if I leave and open it again (as normal or root), the filetype is gone. How do I make this more permanent, at least when called from the same session file
set filetype changes the way vim handles the file, by invoking all the FileType autocommands. It does not persist. If you want to always open that file with filetype=apache, try adding this into your .vimrc:
au BufRead,BufNewFile /etc/apache2/sites-available/www.example.com set filetype=apache
You can read more about it in:
:help 'filetype'
:help filetypes
:help :autocmd
:help .vimrc
EDIT: as found in my /usr/share/vim/vim73/filetype.vim:
au BufNewFile,BufRead access.conf*,apache.conf*,apache2.conf*,httpd.conf*,srm.conf* call s:StarSetf('apache')
au BufNewFile,BufRead */etc/apache2/*.conf*,*/etc/apache2/conf.*/*,*/etc/apache2/mods-*/*,*/etc/apache2/sites-*/*,*/etc/httpd/conf.d/*.conf* call s:StarSetf('apache')
s:StarSetf will setfiletype to apache if the filetype doesn't match an ignored pattern. On my system, :echo g:ft_ignore_pat will show only archive file extensions as ignored. setfiletype does set filetype, but only once.
So, at least on my system, the pattern */etc/apache2/sites-*/* would catch your filename and make it an apache file.
The filetype basically lets Vim change settings for 'types of files'. The way it does this is by firing auto command for the FileType category when you change the filetype. This could potentially change your file if an auto command for FileType is applicable for your file (but generally plugin developers use it for r/o type changes that affect highlighting, and not the contents of the file).
If you are worried that setting the filetype is mucking with your file you can see what FileType autocommands exist by issuing the following command:
:au FileType
To setup your apache files to be apache filetypes you can put something like the following into your ~/.vimrc:
:au BufRead /etc/apache2/sites-available/* set ft=apache

vim set spell in any file

in ~/.vimrc I have
au BufNewFile,BufRead *.tex,*.md,*.markdown set spell
but when I have open a this filetype file and after I open other file, example :
:sp ~/.vimrc
this get spell highlight.
Use :setlocal spell (or :setl spell, for short) to enable spelling locally
in the current window.

Syntax highlighting changes after save in Vim

I have installed Markdown syntax plugin.
When I'm on a new buffer not associated with any file, and paste the source portion of the site http://rosylilly.github.com/markdown.html into it, the colors appear nicely as it should for the markdown syntax.
However, after saving the file, the colors change. Any idea how I can fix this?
Can you, before and after saving do
:verbose set filetype
:verbose set
Tip: use :redir > file.txt to capture the output so you won't have to copy/paste which can be difficult with gvim - for command output
You could also look at the autocommands when saving:
:verbose au BufWrite
:verbose au BufWritePre
:verbose au BufWritePost
This would serve to discover what plugin/script is causing the highlighting to go haywire.

Resources