Aliases don't take effect when I use Vim to execute external commands, for example:
:! ll
I want it to support aliases like normal shells do.
On
Contrary to what romainl writes in his answer, Vim's own :help :! suggests the following:
On Unix the command normally runs in a non-interactive shell. If you want an interactive shell to be used (to use aliases) set 'shellcmdflag' to "-ic".
That would be :set shellcmdflag=-ic.
The caveat is that your shell will not terminate after running your command but prompt you for input instead. You'll have to fg to return to Vim. I would guess that's why romainl discarded it as a solution.
FWIW, I wrote a tiny alias a while ago which will return me to Vim when I type fg regardless of whether I'm in the shell through Vim's :shell or :terminal commands or Ctrl-Z:
alias fg="[[ -v VIM ]] && exit || fg"
This will work for :! with 'shellcmdflag' set to "-ic" as well.
There is no such thing as a "normal shell". You have "interactive shells" and "non-interactive shells", which differ, among other things, by what
*rcfile they source at startup and by whether aliases are expanded or not. And there are other types, even.By default Vim uses a non-interactive shell for
:!and$ man bashas the following to say about aliases in non-interactive shells:Now, you could try to play with
shoptor try to convince Vim to use an interactive shell with:help 'shellcmdflag'but these don't sound like solutions to me.The only proper solution, IMO, is to turn your aliases into actual shell scripts, somewhere in your
$PATH, where they will be accessible to all kinds of shells.--- EDIT ---
FWIW, I don't like the suggestion made in
:help :!because…As illustrated in the other answer, it creates other problems which in turn requires solutions and so on.
The "shell" in which
:!commands are executed is a very dumb one that is not made at all for interactive stuff (no colors, etc.).I much prefer to suspend Vim and end up in a proper shell than to execute lots of commands in the bastardized
:!.And there's even
:help :terminalnowadays if that's your thing.I find it a lot cleaner, portable, and scalable to create proper shell scripts in general.
That said, I still have a bunch of aliases, like everyone, but none of them is of any use in a
:!context so there is no point a) adding-itoshellcmdflagor b) turning them into scripts::!is just not a good enough environment for running interactive commands.