Getting zsh environment in vim to match command line

I am having trouble getting aliases to work in Vim when I try to run shell commands.

I am using a fork of Thoughtbot dotfiles and running vim in iTerm2.

Stack Overflow seems to indicate that the best way to get my .aliases and .aliases.local to source is to set set shellcmdflag='-ic', but when I do that, I get an error about Zsh not being able to open the input file ‘-ic’, and it still doesn’t seem to source my aliases. Using set shell=zsh\ -i also doesn’t seem to result in the shell called from ! or :sh loading my aliases.

I’ve been trying to read the Zsh documentation to see if there’s a command I could run that would allow me to just manually force zsh to load my aliases on load, and I’m coming up short. Are Thoughtboters just running commands from a tmux pane rather than using !command at a vim command line?

I’ve had similar problems in the past, and doing: sudo mv /etc/{zshenv,zprofile} fixed them. Some background is here. Try that out and see if you have any luck?

This is done automatically as part of our laptop script

1 Like

@derekprior, thanks for your advice. Unfortunately, I’ve already done that (I used Laptop to get my base configuration set up). The file OSX creates as zshenv is now zprofile, and only contains a clause about the path helper.

I can get aliases loaded if I source the .aliases file from zshenv, but that means that in a normal interactive shell aliases get sourced twice (which I’m sure is a tiny performance hit, but I can live with that). It also means that zsh functions from thoughtbot/dotfiles such as g and rake don’t work.

Correction, the file is now named zshrc as Laptop renames it.

When I get stuck on tricky zsh issues, I head to #zsh on freenode. Maybe try there?

@benorenstein, thanks. Got an answer there about using fpath that I think is an acceptable workaround, but I’m pretty certain there’s something amiss in my general setup that is causing this if other laptop/dotfiles users are not having this issue.

It’s our differences that make us unique. :smile:

2 Likes

I don’t use laptop or dotfiles directly but do pull a lot of my setup from there. I don’t often run shell commands from vim (as you speculate, I use a tmux split for that), but realized today that when i do I generally type the entire command. I tried an alias and sure enough, I had the same problem you did.

!alias shows two aliases which are defined in the non-interactive shell but none of my aliases. Now I’m intrigued.

With a little poking around in help, I found the shellcmdflag (shcf for short) option. When I set shcf=-ic and then run :!alias I get my full list of aliases. Success… or so I thought. Some of my commands now end with vim being suspended and dumping me to a prompt, where I then have to fg to get back to vim.

It also seems to royally screw up things like ctrl-p, which I have set to use ag for file finding.

@derekprior, welcome to my world (had the same issues). Now that I know others are having the same issues, and the approach is to just use tmux or full-length commands, I’m going to (temporarily) stop obsessing over this and start working actual code-writing.

As an early vim convert, I think one of the huge dangers of vim is that the extreme degree of customization is an extremely tempting procrastination sink.

1 Like

@geoffharcourt @derekprior Did you guys ever get to the bottom of this? I’ve just tried this myself and seen the exact same issues with setting shcf=-ic - ctrl-p suspending vim.

@aaronmcadam, for a while I ended up sourcing my aliases and other configs in .zshrc to get them loaded in vim. It wasn’t ideal, as you don’t want to be overriding those things for non-interactive shells, lest you get some unintended side effects.

The new thoughtbot dotfiles as of this week include a plugin from @christoomey, GitHub - christoomey/vim-run-interactive: Vim plugin to run shell commands in an interactive shell, which allows you to run commands in an interactive shell. I’ve mapped <leader> r so that I can hit that and then run commands I have aliased, etc.

1 Like

Thanks @geoffharcourt, I’ll give that plugin a go!