Is it possible to make vim write something on the first line (or on few first lines) automatically everytime when I create file with a specific extension?
For example (and this is only an example) if I create .txt file I would like vim to write "Hello" on the first line.
Is it possible? If it is, how?
Yes, you can achieve this with autocommands. The general structure of an autocommand is
au event filetype command
In your specific case, you want
au BufNewFile *.txt normal iHello
Explanation:
au "Define a new autocommand
BufNewFile "When you create a new file
*.txt "With the extension '.txt'
normal iHello "Execute the command 'normal iHello', which is like typing 'iHello' manually
The docs give an idea of how to do this in :h skeleton. Essentially, you just need a BufNewFile autocommand in your .vimrc.
The docs assume you'll be reading your initial content from a file. So, for your example, assuming skeleton.txt contains the text "Hello":
autocmd BufNewFile *.txt 0r ~/vim/skeleton.txt
Alternatively, if your first line is relatively simple, you can always hardcode it by just entering insert mode and adding the text you need.
au BufNewFile *.txt normal iHello
This answer from vi.stackexchange provides a couple examples that call functions and are a bit more complex.
In my case, to bash files, I have this on my ~/.vimrc
augroup sh
au BufNewFile *.sh 0r ~/.vim/skel/template.sh
"au BufWritePost *.sh,*.pl,*.py,*.cgi :silent !chmod a+x <afile>
augroup end
If I create a new file "BufNewFile" vim opens at the line zero "0" by reading "r" the content of the file template.sh, on that file I can put wherever I want.
If I uncoment the seccond line removing the " at beginning I have also a change on file permission automatically
Related
When trying to automatically open the corresponding .cpp or .h file using autocommand I encounter no colorscheme on the corresponding file that is opened.
I'm not too familiar with vimscript but I believe Vim is opening the file thinking it is of file type ".txt" and therefore using a default colorscheme.
Two autocommand lines in ~/.vimrc:
au BufRead,BufNewFile *.cpp exe "bel vsplit" fnameescape(expand("%:r").".h")
au BufRead,BufNewFile *.h exe "vsplit" fnameescape(expand("%:r").".cpp")
Any help would be appreciated.
Your answer is a workaround (though you should use :setlocal instead of :set to avoid that the syntax leaks out to new buffers that are opened from that one), but it doesn't attack the root cause, which you'll find explained at :help autocmd-nested:
By default, autocommands do not nest. If you use ":e" or ":w" in an autocommand, Vim does not execute the BufRead and BufWrite autocommands for those commands. If you do want this, use the "nested" flag for those commands in which you want nesting.
Syntax highlighting (you say colorscheme in your title, but that's actually just the color and font attributes that are then used by syntax highlighting) is based on :autocmd events (same goes for filetype plugins, so any C++-related settings you also wouldn't find in the split file, assuming you have :filetype plugin on in your ~/.vimrc). Without the nested attribute, the split file will be opened, but none of the usual actions will be run on them. Though nesting in general can be problematic, this is one of those cases where it is needed.
au BufRead,BufNewFile *.cpp nested exe "bel vsplit" fnameescape(expand("%:r").".h")
au BufRead,BufNewFile *.h nested exe "vsplit" fnameescape(expand("%:r").".cpp")
Unfortunately, this introduces another problem: The one autocmd will trigger the other one, and vice versa (up to a limit). You need to guard the actions so that a split is only done if the file isn't open yet. (This also improves on the usability in a general way, when you open a file with the other already open.) :help bufwinnr() checks whether the target buffer is already visible in a window:
au BufRead,BufNewFile *.cpp nested if bufwinnr("^" . expand("%:r").".h$") == -1 | exe "bel vsplit" fnameescape(expand("%:r").".h") | endif
au BufRead,BufNewFile *.h nested if bufwinnr("^" . expand("%:r").".cpp$") == -1 | exe "vsplit" fnameescape(expand("%:r").".cpp") | endif
If anyone cares to look at this in the future the solution was that Vim was loading the second file as syntax=none. So adding | set syntax=cpp at the end of each auto command fixed it.
Basically, I have a pre-processor for my code which reads all my .foo files and converts them to corresponding compiled .bar versions, though without ever modifying any of the originals. Each time I write changes to my source files, I would like vim to !./run-my-preprocessor-script.js after it has finished saving my file. How can this be done? I only want this to happen when I'm editing files with names ending in .foo.
Thanks
You can use Vim’s autocmd feature (see :help :autocmd). It has lots of hooks for “events”; the one you want should be BufWritePost. E.g., in your ~/.vimrc:
autocmd BufWritePost *.foo !./run-my-preprocessor-script.js
You can use autocmd to hook into the BufWritePost event like so:
autocmd BufWritePost * execute '!ls'
This means that every time the BufWritePost (after the buffer has been written) event is triggered on any file, you'll execute the command !ls. It's easy to specialize this to work only on *.foo files that are saved:
autocmd BufWritePost *.foo execute '!ls'
Now, the command !ls will only be triggered when a file with a filename matching the expression *.foo is written.
Obviously, you'll need to change ls to whatever you do to execute your preprocessor script, but the process is the same. Put that modified autocmd into your .vimrc file and it should work!
I am trying to get vim to recognize a filetype.vim file consisting of the following:
if exists("did_load_filetypes")
finish
endif
augroup autodetect
au! BufRead,BufNewFile *.ish setfiletype perl
augroup END
This file is in the directory ~/programs/vim
My .vimrc file contains the following line:
set runtimepath=~/programs/vim,$VIMRUNTIME
I checked that this line is being executed by typing ":set runtimepath?" The result is "runtimepath=~/programs/vim,/usr/share/vim/vim74".
But when I open a file such as ish.ish, the vim filetype variable is set to 'on'. When I open x.pl, the vim filetype variable is set to 'perl'.
I can fix the problem by copying (or moving) filetype.vim to the ~/.vim directory (without changing runtimepath). Why doesn't vim recognize filetype.vim in ~/programs/vim?
The 'runtimepath' option is meant to tell Vim where to look for "standard" *.vim files such as colorschemes and plugins. Try :echo &rtp in a clean Vim session to see what it should look like and read :help 'runtimepath'. By setting this option to a meaningless value you effectively make Vim unable to find those files and thus work correctly.
To add a specific directory to 'runtimepath', use the following syntax:
set runtimepath+=/path/to/directory
But what's the reason you'd want to use a non-standard directory for standard scripts? What's wrong with ~/.vim?
The most obvious way to tell Vim about a new filetype is to add these lines to your ~/.vimrc:
augroup autodetect
autocmd!
autocmd BufRead,BufNewFile *.ish set filetype=perl
augroup END
The cleanest way is to put this line in ~/.vim/ftdetect/ish.vim:
autocmd BufRead,BufNewFile *.ish set filetype=perl
I'm using vim 7.3 and Rainbow Parentheses plugin. When opening multiple tabs with vim -p file1 file2 or with vim -S session.vim, or even with tabnew file or any other method, my parenthesis are colored in only one file.
I just put this into my .vimrc : au VimEnter * RainbowParenthesesToggle
as said here. I tried to use :RainbowParenthesesToggle on the other tabs once opened but it only toggles in the parenthesis-activated tab.
What should I do to make things work in all tabs ?
I made it work by adding the same instructions as here in my .vimrc, thanks to FDinoff. I replaced the last instruction to make it work using tab, as I intended first.
function! Config_Rainbow()
call rainbow_parentheses#load(0)
call rainbow_parentheses#load(1)
call rainbow_parentheses#load(2)
endfunction
function! Load_Rainbow()
call rainbow_parentheses#activate()
endfunction
augroup TastetheRainbow
autocmd!
autocmd Syntax * call Config_Rainbow()
autocmd VimEnter,BufRead,BufWinEnter,BufNewFile * call Load_Rainbow()
augroup END
The VimEnter flag on the autocommand tells vim to perform the command specified (in this case RainbowParenthesesToggle only when starting the editor, which is in your case when you open the first file.
If you want to extend the functionality to everytime you load a buffer you should do something like:
autocmd BufRead,BufNewFile * RainbowParenthesesToggle
what I am using now is ,
autocmd BufWritePost *.py !python PythonTidy.py % %
It really call the tidy programe and change the file, but the vim does not reload the new file.
And I do not want to install another plugin for it.
=======================
note: I found it's dangerous about this feature, PythonTidy will will output a empty file if the command faild, it means if you have syntax error, you will lose your file unless press "u" to get it,but you can't save before you fix syntax error.
I call :!PythonTidy % % manually after pylint complete now.
Use BufWritePre instead of BufWritePost and combine Vim range filtering with PythonTidy’s stdin/stdout mode.
autocmd FileType python autocmd BufWritePre <buffer> let s:saveview = winsaveview() | exe '%!python PythonTidy.py' | call winrestview(s:saveview) | unlet s:saveview
(The use of autocmd FileType python autocmd BufWritePre <buffer> makes this a bit more accurate than matching on a glob pattern: it means “any time a Python file is detected, install this autocmd for that buffer” – so it works independently of file name.)
Unfortunately this cannot preserve your cursor position if you undo the filtering. (You are filtering a whole-file range; when undoing a filter operation, the cursor jumps to the first line in the range; so you end up at the top of the file.) I was hoping to find a way to create a no-op undo state, before, so you could hit u twice and get back to the right place, but I can’t make that work as yet. Maybe someone else knows how.
hi the following fixed the cursor postion problem
function! PythonTidySaver()
let oldpos=getpos('.')
%!PythonTidy
call setpos('.',oldpos)
endfunction
autocmd! bufwritepost *.py call PythonTidySaver()
Based on :help :e:
*:e* *:edit*
:e[dit] [++opt] [+cmd] Edit the current file. This is useful to re-edit the
current file, when it has been changed outside of Vim.
This fails when changes have been made to the current
buffer and 'autowriteall' isn't set or the file can't
be written.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
So you'd need to use :e after updating the file externally. However, :! doesn't let you use | normally (see :help :!), so you need to wrap it:
autocmd BufWritePost *.py execute "!python PythonTidy.py % %" | e
(:autocmd doesn't interpret | normally either, which is why you don't need to escape it yet again.)