Indent code using vim autocommand - vim

I want use the indent linux utility to indent the current file on saving. I tried
autocmd BufWritePre *.[ch] :!indent -kr -nut %
in my .vimrc file (~/.vim).
Manually I tried that command
:!indent -kr -nut %
it works only after prompting me to load the file, as below,
See ":help W12" for more info.
[O]K, (L)oad File:

You don't want BufWritePre because that modifies your file before saving it. Instead, try doing BufWritePost:
autocmd BufWritePost *.[ch] !indent -kr -nut %
This will still ask you to hit enter, but it doesn't prompt to load the file when I test it. If you don't want to have to hit enter after saving, you can change this to:
autocmd BufWritePost *.[ch] exec "!indent -kr -nut %" | redraw
Also, note how I removed the : from your command. This is because autocmd looks for an ex command, so the : is unneeded.

Related

Why vim returns E488: Trailling characters to my function?

I am trying to execute a yapf command to format my python file when I save it, so I created a function to call this command:
function Format_python_file()
silent :!yapf --style="{based_on_style: pep8, indent_width: 4}" -i %
silent :e %
endfunction
autocmd BufWritePost *.py call Format_python_file() <afile>
The problem is with your autocmd line, you have a trailing <afile> there.
In fact, the message I see is quite explicit about that:
Error detected while processing BufWritePost Autocommands for "*.py":
E488: Trailing characters: <afile>
You should just drop the <afile>, the function itself already works on the current buffer, doesn't need any argument or other reference to the current file.
Also note that it's a good practice to put your autocmds inside an augroup which gets cleared first. That way, if you reload your source file (vimrc or otherwise), it won't create duplicated autocmds.
The cleaner way to set up this autocmd would be:
augroup python_yapf
autocmd!
autocmd BufWritePost *.py call Format_python_file()
augroup END

Vim how to autosave and get autocmds to run

I have the following in my .vimrc to autoformat my python code on save.
autocmd BufWritePost *.py silent !autopep8 --in-place --aggressive %
However I am using a plugin vim-auto-save that automatically saves my code for me. However the BufWritePost event doesn't seem to get called. I have to manually run :w for my autocmd to work.
Vim-auto-save runs a
silent! wa
command, which should save all files, and that should trigger the BufWritePost event.
Did you enable vim-auto-cmd on startup?
let g:auto_save = 1 " enable AutoSave on Vim startup

Vim plugin : Rainbow parentheses using tab

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

Vim: Saving on FocusLost and executing auto command

I've setup my vim editor (I use MacVim) to save files automatically when the focus is lost:
autocmd FocusLost * silent! wall
I also automatically strip trailing whitespace from python files using this auto command:
autocmd BufWritePre *.py :%s/\s\+$//e
This auto command works perfectly when I save the file manually (either by typing :w or by pressing ⌘s) but it is not executed (i.e. the whitespace is not stripped) when I switch to another application and the buffer is automatically written.
How can I modify these auto commands to make them work together?
You need to change your FocusLost autocommand to:
autocmd FocusLost * nested silent! wall
See :h autocmd-nested for details.
I can't test this in a graphic Vim, but you can try some options:
Join some events in the same autocommand autocmd BufWritePre,FocusLost *.py ...
Execute an autocommand from an event, something like:
autocmd BufWritePre *.py :execute "%s/\s\+$//e" | doautocmd FocusLost %

how to use pythontidy in vim

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

Resources