How can I make shell aliases available when shelling out from Vim?

1,668

Solution 1

Old question, but the cleanest solution for vim in zsh was to add the alias to ~/.zshenv, the file that zsh loads for all shells, login, interactive, or otherwise. This avoids starting vim or zsh with flags and any possible problems with that.

There's a nice explanation of ~/.zshenv vs ~/.zshrc here: http://tanguy.ortolo.eu/blog/article25/shrc

Basically, zsh always sources ~/.zshenv. Interactive shells source ~/.zshrc, and login shells source ~/.zprofile and ~/.zlogin. Thus, an interactive login shell sources ~/.zshenv ~/.zprofile ~/.zlogin ~/.zlogin, and a noninteractive, nonlogin shell like the one vim uses to run commands only sources ~/.zshenv.

Solution 2

Looks like this works for zsh:

  • Ensure that $ZDOTDIR= the directory where .zshrc is located. Eg, export ZDOTDIR=$HOME
  • In .vimrc, set shell=zsh\ -i or set shellcmdflag+=i for the same effect.

The -i is because, when started in interactive mode, zshell loads $ZDOTDIR/.zshrc. See man zsh and search for $ZDOTDIR for details.

Solution 3

I believe when you're in vim and you use the :!some_command it's using whatever shell is defined by the environment variable $SHELL.

This is configurable, so you could change by overriding the $SHELL behavior in your $HOME/.vimrc file to use zsh instead.

:set shell
shell=/bin/bash
:set shell=zsh\ -i

Or in your .vimrc using 1 of these 2 lines

 set shell=/bin/bash\ -i
 set shell=/bin/zsh\ -i

Vim help

See :help shell from within vim for more info.

:!{cmd}                 Execute {cmd} with the shell.  See also the 'shell'
                        and 'shelltype' option.
                        Any '!' in {cmd} is replaced with the previous
                        external command (see also 'cpoptions').  But not when
                        there is a backslash before the '!', then that
                        backslash is removed.  Example: ":!ls" followed by
                        ":!echo ! \! \\!" executes "echo ls ! \!".
                        After the command has been executed, the timestamp of
                        the current file is checked timestamp.
Share:
1,668

Related videos on Youtube

jimminybob
Author by

jimminybob

Updated on September 18, 2022

Comments

  • jimminybob
    jimminybob about 1 year

    I'm trying to use the following code to retrieve the SharePoint URL of a record:

    RetrieveAbsoluteAndSiteCollectionUrlRequest retrieveRequest = new RetrieveAbsoluteAndSiteCollectionUrlRequest
            {
                Target = new EntityReference(SharePointDocumentLocation.EntityLogicalName, _spDocLocId)
            };
            RetrieveAbsoluteAndSiteCollectionUrlResponse retrieveResponse = (RetrieveAbsoluteAndSiteCollectionUrlResponse)_service.Execute(retrieveRequest);
    
            return retrieveResponse.AbsoluteUrl.ToString();
    

    But it says that SharePointDocumentLocation does not exist and has asked for a reference to it. I can't find any reference for this and am not sure how to get it working. Can anyone help?

    Thanks

    • S edwards
      S edwards almost 10 years
      include your .zshrc into .profile (which seems to be use by vim
  • jimminybob
    jimminybob over 11 years
    Its the result of a fetch on the sharepointdocumentlocation table that has the regardingobjectid as the record ID. I've seen other examples of this code with the same references and nearly the same code as mine but i cannot see where this SharePointDocumentLocation is being defined
  • jimminybob
    jimminybob over 11 years
    When i set it up i specified the base URL of the sharepoint site, so i'm guessing that'll make it automatic then? All i want to do is get the complete sharepoint URL of the current record, but i can't find an example that does it in a way that works for me
  • Chris Snyder
    Chris Snyder over 11 years
    From what I can tell, the "automatic" approach requires you to "walk up the chain" to determine the full path. For a Contact named "John Doe" who works for an Account named "Account1" the automatic integration builds out a folder structure like this: \Accounts\Account1\Contacts\John Doe\
  • jimminybob
    jimminybob over 11 years
    I see, so would it be easiest then to go to the database from my plugin to work up the chain and construct the URL?
  • Nathan Long
    Nathan Long almost 10 years
    No, as I said: "If I do :! echo $0 to see what shell Vim is using, it outputs '/bin/zsh'"
  • slm
    slm almost 10 years
    @NathanLong - no I think that's misleading you, see my updates.
  • phemmer
    phemmer almost 10 years
    @slm vim pulls the value from $SHELL. So your shell is /bin/bash. If my shell is /bin/zsh, thats what it'll use.
  • Nathan Long
    Nathan Long almost 10 years
    Hmmm, this creates a weird issue for me: copying to the system clipboard like "+y now suspends Vim! superuser.com/questions/712245/…
  • Jakob Bennemann
    Jakob Bennemann over 8 years
    This is not a link-only answer, but it would be best if you inlined the explanation of the difference from the link; that way everyone can benefit from the explanation here and we are safe from linkrot.
  • Kevin Lee
    Kevin Lee over 8 years
    Updated my answer with the main points from the link.
  • Caleb
    Caleb over 8 years
    This worked exponentially better for me than the other answers. When I added for vim to start an interactive shell, it suspended immediately. When I use the .zshenv file (which I previously didn't know existed), I got my aliases back!
  • Caleb
    Caleb over 8 years
    My vim becomes suspended as soon as I open it.
  • vilsbole
    vilsbole over 7 years
    using ubuntu 14.04 this doesn't work for me