execute an alias with vim

176 views
Skip to first unread message

Mathieu Roux

unread,
Jul 10, 2019, 4:30:19 AM7/10/19
to vim...@googlegroups.com
Hello,

I use many text files to write many things on my laptop, and i use
aliases to access them.
For exemple, suppose that i have the alias "foo" to access ~/bar with
vim.

alias foo='vim ~/bar'

So it works when i write "foo" in my terminal.

But I also want to use this alias in vim.
When i am in vim, i do "C-w n" to open one new window.
And then i try :!foo

But it writes:

"/bin/bash: foo: command not found

le shell a retourné 127"

Can you help me?
Best regards,
Mathieu

tooth pik

unread,
Jul 10, 2019, 4:45:42 AM7/10/19
to vim...@googlegroups.com
On Wed, Jul 10, 2019 at 3:30 AM Mathieu Roux <mth...@gmail.com> wrote:
>
> Hello,
>
> I use many text files to write many things on my laptop, and i use
> aliases to access them.

why?

> For exemple, suppose that i have the alias "foo" to access ~/bar with
> vim.
>
> alias foo='vim ~/bar'
>
> So it works when i write "foo" in my terminal.

sure -- but it would also work if you created a 'foo' in ~/bin with a
#!/bin/bash shebang
and a 'vim ~/bar' -- why not use that? what is your fascination with aliases?

wherever you are defining your aliases (~/.bashrc?) is not being
executed by vim when
it needs a shell -- but really -- why not create a script on ~/bin?

> But I also want to use this alias in vim.
> When i am in vim, i do "C-w n" to open one new window.
> And then i try :!foo
>
> But it writes:
>
> "/bin/bash: foo: command not found
>
> le shell a retourné 127"
>
> Can you help me?
> Best regards,
> Mathieu
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/5918a9198df7dd2f6a0abf18a0666277cec85065.camel%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Tony Mechelynck

unread,
Jul 10, 2019, 4:46:22 AM7/10/19
to vim_use
When called from Vim to run an external :!command, the shell does not
source all the same startup files. I also have noticed that, for
instance, :!ll doesn't work in Vim (in a "normal" bash shell on my
system, ll is aliased to ls -l) so I have to use ls -l instead.

I think bash-from-Vim doesn't regard itself as an interactive shell
when used to run a :!command. If I type ":shell" (without the quotes)
in Vim, then ll gives me a long directory list.

Rather than call vim inside vim through the shell, why don't you do

:new ~/bar

? That would open the file ~/bar in a split-window in the same Vim.

Mathieu Roux

unread,
Jul 10, 2019, 5:25:35 AM7/10/19
to vim...@googlegroups.com
Thanks for your answer.

I love aliases since i am a child! i am absolutely fascinated by them!
i dream of them every night :-)

Hmmmm in fact aliases was the best way i found to do what i want. Just
a ligne in ~/.bashrc, so simple. And just to type "bar" in my terminal
to access the file.

So, I tried to create one file with shebang (in one directory in my
$PATH, ~/bin is not my path).

But when i do "c-w n" in vim, and then :!bar, the file is open in
another vim, not in my window i have just created!
> --

Tony Mechelynck

unread,
Jul 10, 2019, 6:29:14 AM7/10/19
to vim_use
On Wed, Jul 10, 2019 at 11:25 AM Mathieu Roux <mth...@gmail.com> wrote:
>
> Thanks for your answer.
>
> I love aliases since i am a child! i am absolutely fascinated by them!
> i dream of them every night :-)
>
> Hmmmm in fact aliases was the best way i found to do what i want. Just
> a ligne in ~/.bashrc, so simple. And just to type "bar" in my terminal
> to access the file.
>
> So, I tried to create one file with shebang (in one directory in my
> $PATH, ~/bin is not my path).
>
> But when i do "c-w n" in vim, and then :!bar, the file is open in
> another vim, not in my window i have just created!

That is because your alias invokes a new instance of Vim.

See :help clientserver to make a new shell talk to an existing instance of Vim.

Best regards,
Tony.

Kit

unread,
Jul 10, 2019, 8:24:13 AM7/10/19
to vim...@googlegroups.com
> I love aliases since i am a child! i am absolutely fascinated by them!
> i dream of them every night :-)

Try this alias:
alias .bashrc="vim $HOME/.bashrc; . $HOME/.bashrc"

> Hmmmm in fact aliases was the best way i found to do what i want. Just
> a ligne in ~/.bashrc, so simple. And just to type "bar" in my terminal
> to access the file.

Try this:
:new .
select and <Enter>

> So, I tried to create one file with shebang (in one directory in my
> $PATH, ~/bin is not my path).

add "$HOME/bin" into your PATH. This is the standard for your binaries.
--
Kit

Mathieu Roux

unread,
Jul 10, 2019, 8:38:48 AM7/10/19
to vim...@googlegroups.com
I really don't understand what you want with your alias .bashrc???


And when you write
:new .
select and <Enter>
What do you mean? select what?
> --

Kit

unread,
Jul 10, 2019, 8:50:29 AM7/10/19
to vim...@googlegroups.com
> I really don't understand what you want with your alias .bashrc???

I use this alias for edit ~/.bashrc and reload it.

> And when you write
> :new .
> select and <Enter>
> What do you mean? select what?

Select file in Vim. Simply open directory and select file. Enter will
open this file.
--
Kit

Mathieu Roux

unread,
Jul 10, 2019, 9:01:48 AM7/10/19
to vim...@googlegroups.com
I am not sure it helps me a lot.

Of course i can explore and select manually my file... but i prefer
something quicker!
> --

Andreas Perstinger

unread,
Jul 10, 2019, 10:29:18 AM7/10/19
to vim...@googlegroups.com
On 10.07.19 10:30, Mathieu Roux wrote:
> I use many text files to write many things on my laptop, and i use
> aliases to access them.
> For exemple, suppose that i have the alias "foo" to access ~/bar with
> vim.
>
> alias foo='vim ~/bar'
>
> So it works when i write "foo" in my terminal.

I suppose you use the aliases because your filenames are rather long
and/or the files are in a deeply nested directory (e.g.
/very/long/path/to/the/file/foo.txt)

If that's the case I would suggest to

a) create a directory in your $HOME and create symbolic links to all the
files you want to access with a short name in that directory.

b) in vim set the 'path' option to that directory

c) to open the files use :find instead of :new or :edit.

See also :help path and :help find

Bye, Andreas

Mathieu Roux

unread,
Jul 10, 2019, 12:43:41 PM7/10/19
to vim...@googlegroups.com
Very good solution! thank you very much!
> --

Grant Taylor

unread,
Jul 10, 2019, 12:56:06 PM7/10/19
to vim...@googlegroups.com
On 7/10/19 3:25 AM, Mathieu Roux wrote:
> I love aliases since i am a child! i am absolutely fascinated by them!
> i dream of them every night:-)

Aliases can be quite useful. But they generally only work /in/ /the/
/shell/. If you want to use (something like) an alias /outside/ /of/
/the/ /shell/ you quite likely need to use something other than an alias.

Scripts or other executables—that are in the $PATH—are quite good for this.

The 3rd alternative, function, will likely have the same limitations as
aliases.

As a general rule, if I want to use something outside of the interactive
shell, I use an executable. This applies equally as well to things like
running :!theCommand from within vim to running theCommand remotely via
ssh to creating desktop shortcuts that search the $PATH.



--
Grant. . . .
unix || die

Timothy Rice

unread,
Jul 10, 2019, 7:04:12 PM7/10/19
to 'Grant Taylor' via vim_use
> As a general rule, if I want to use something outside of the interactive
> shell, I use an executable. This applies equally as well to things like
> running :!theCommand from within vim to running theCommand remotely via ssh
> to creating desktop shortcuts that search the $PATH.

According to the Bash manual (man 1 bash), one can use shopt to set
expand_aliases to true. This should allow access to aliases in
non-interactive settings. (I have not tested it, though.)

A workaround I came across a long time ago was to keep aliases in a
separate file, which gets sourced by ~/.bashrc, but which can also be
sourced by a wrapper script like so:

```
#! /bin/bash
. ~/bashrc.d/aliases.sh

ALIAS=$1
ARGS=${@:2}

bash -c "${BASH_ALIASES[$ALIAS]} $ARGS"
```

So, if ~/bashrc.d/aliases.sh contains the line `alias foo="vim ~/bar"`, and
if that wrapper script is named `baz` somewhere in my $PATH, this should
work in non-interactive settings:

baz foo

It will source the aliases file, find the alias foo, and thus execute
`vim ~/bar`.

But if using shopt to set expand_aliases works, it is probably a superior
approach to my workaround.

~ Tim

Enno

unread,
Jul 10, 2019, 8:44:07 PM7/10/19
to vim_use
If you use bash, then define the aliases in `~/.bash_profile` and add

let $BASH_ENV = $HOME.'/.bash_profile'

to your vimrc. See also

http://stackoverflow.com/questions/4642822/commands-executed-from-vim-are-not-recognizing-bash-command-aliases/19819036#19819036

and

and http://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables/117470#117470


If you use zsh, then add them in `~/.zshenv` (instead of ~/.zshrc`) as well as the line 'setopt aliases'.

aro...@vex.net

unread,
Jul 11, 2019, 9:57:43 AM7/11/19
to vim...@googlegroups.com

Something to bear in mind with all this aliasing, linking, and
environmental trickery is the effect on subsequent maintainability.

Even if you ensure that it's properly documented and generally understood,
how many hours are going to be spent figuring out what's going on before
any real problem gets addressed? How much time and effort do you have to
save to justify it?

Grant Taylor

unread,
Jul 11, 2019, 2:12:56 PM7/11/19
to vim...@googlegroups.com
All very good points.

IMHO that's another reason for actual scripts that are in the $PATH.

Christian Brabandt

unread,
Jul 14, 2019, 9:28:08 AM7/14/19
to vim...@googlegroups.com
I believe, if you set the shellcmdflag to -ic Vim will run an
interactive shell (which in turn should read your alias file).

Mit freundlichen Grüßen
Christian
--
Wir sind doch alle nur Schachfiguren, sagte der König zum Bauern.
Reply all
Reply to author
Forward
0 new messages