Reputation: 25
I just learned that I can run perl scripts on my vim text window by using
:%!/path/to/script
Is there a way to set filter1 = /path/to/script
so I only need to type :%!filter1
?
Upvotes: 2
Views: 281
Reputation: 196789
You can also place your script somewhere in your $PATH
.
The scriptname
command is now available to vim (and other programs) for use on the command line.
:!scriptname<CR>
Obviously, if you use that command often, you should create a mapping in your ~/.vimrc
.
edit
On Mac OS X, all terminal emulators use a login shell by default. It means that, for day-to-day terminal usage, the right place for defining aliases, altering your $PATH
, setting shell variables or exporting environment variables is actually ~/.bash_login
or ~/.profile
. In this context, ~/.bashrc
is totally useless.
But MacVim doesn't use a login shell by default. Actually, it doesn't even use an interactive shell. The immediate consequence is that, by default, MacVim is able to see environment variables (export FOO='bar'
) but it can't see our aliases (alias foo='bar'
) or shell variables (BAZ='foo'
), whether they are set in ~/.bash_login
, ~/.profile
or ~/.bashrc
.
Thankfully, we only need to change an option in Vim to make it use an interactive shell and thus see the aliases defined in ~/.bashrc
:
:set shellcmdflag=-ic
But we end up with two configuration files: ~/.profile
and ~/.bashrc
. Aliases defined in the first are available in all terminal sessions but not in Vim and those defined in the second are available in Vim but invisible in a regular terminal session.
One solution could be to add the -l
flag to the option above to make MacVim run an interactive login shell (and thus make use of what we have in our ~/.profile
).
Another solution would be to source ~/.profile
in ~/.bashrc
as I did a couple of years ago.
But, as @ZyX correctly suggested in his comment, the whole thing could become messy and probably have performance issues.
In my opinion, the best course of action, on Mac OS X, is thus probably to leave shellcmdflag
at its default value, put whatever aliases/variables/functions you need in your day-to-day terminal usage in ~/.profile
(not ~/.bashrc
as it is not executed by default) and simply place your custom scripts in your $PATH
.
Upvotes: 2
Reputation: 29875
You can use cabbrev
to create a command-line abbreviation like this:
:cabbrev filter1 /path/to/script
If you the type filter1
followed by a space it will expand to to /path/to/script
.
And you can also put the %!
into the abbreviation if you only use it in this combination.
Obviously, if you use this filter a lot it might be easier to create a keybinding for this:
map ,f :%!/path/to/script<cr>
This will map ,f
to executing your filter...
Upvotes: 5