How do I automatically execute an external command in vim/neovom? - vim

What should I put in my init.vim to run the following command every time I save a Python file?
:!black %
Obviously this should only run if file type is .py

This can be achieved using an autocmd:
autocmd BufWritePost *.py !black %
It will run the your command after a file with the extension .py has been written to file.
For more information see :h autocmd.

Related

Define vim command to open current filename with another extension

I'm trying a simple way to compile pdfs in LaTeX and open them with zathura from a single autocmd inside vim, so far I tried:
command Latex !pdflatex %:t
command Za !zathura #%.pdf
autocmd FileType tex map <leader>pdf :w<CR>:Latex<CR>:Za <CR>
Obviously, #%.pdf is wrong, but I've had no luck in finding how to append an extension to the file name. I'm newbie in vim scripting so please point out any other errors.
Since no one bother to answer, I came up with a shell solution for it, a script called zatex:
tex=".tex"
pdf=".pdf"
cd $1;
texfile="$2$tex";
pdflatex $texfile;
pdffile="$2$pdf";
setsid zathura $pdffile;
And inside my .vimrc:
command Zatex !zatex %:p:h %:p:h:t
autocmd FileType tex map <leader>pdf :w<CR>:Zatex<CR>
Of course, after the first compiling it might be handy to have another mapping just to compile the pdf, since zathura is already open:
command Latex !pdflatex %:t
autocmd FileType tex map <leader>tex :w<CR>:Latex<CR>
I believe %:r is what you are looking for, without the # character. It will return the file name, without extension.
Also, have a look at this question.
EDIT: I just tested here (not with !zathura, but with !echo) and the following line should work for you (just append the correct extension after the file name expand):
command Za !zathura %:r.pdf

Indent code using vim autocommand

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.

Prevent Vim's autocmd from running on write just once

I have the following line in my .vimrc to automatically beautify js files after I save them:
autocmd BufWritePost *.js :call JsBeautify()
I want this 99% of the time, but sometimes I just want to write without having this function called. Is there an easy way to do that?
I guess you're looking for :noa.
Usage:
:noa w
Excerpt from help:
:noautocmd :noa
To disable autocommands for just one command use the ":noautocmd" command
modifier. This will set 'eventignore' to "all" for the duration of the
following command. Example:
:noautocmd w fname.gz
This will write the file without triggering the autocommands defined by
the
gzip plugin.

Golang: running `go build` on save using vim (macvim) and vim-go

I am recently moving from sublime 3 go to mvim (vim on the mac os) and am trying to get my Golang development environment to be as similar on vim to my sublime implementation as possible.
Within my sublime setup, it runs go build anytime I save the a Go file. This provides me instant feedback on if I have unused vars or other info go build provides.
I'm attempting to move to vim, and am wondering if I can have this functionality implemented there as well. I am using vim-go but have not found a setting to implement this.
In short I want to run :GoBuild upon the saving of a Go file while using vim / vim-go. Is this Possible and how do I do so?
yes, use vim autocommand to run :GoBuild:
You can specify commands to be executed automatically when reading or
writing a file, when entering or leaving a buffer or window, and when
exiting Vim. The usual place to put autocommands is in your .vimrc or
.exrc file.
Run the following command:
:autocmd BufWritePre *.go :GoBuild
Now each time you save your Go file with :w it will run :GoBuild too.
The event type is BufWritePre, which means the event will be checked just before you write *.go file.
BufWritePre starting to write the whole buffer to a file
BufWritePost after writing the whole buffer to a file
and:
When your .vimrc file is sourced twice, the autocommands will appear
twice. To avoid this, put this command in your .vimrc file, before
defining autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a
variable to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
like this (put this at the end of your vim startup file):
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: autocmd BufWritePost *.go :GoBuild
:endif
ref:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
http://learnvimscriptthehardway.stevelosh.com/chapters/12.html
Create ~/.vim/after/ftplugin/go.vim with:
autocmd BufWritePre *.go :GoBuild

How can I have vim execute a shell command upon every :w?

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!

Resources