3

I am trying to set up Vim so that I can compile C code directly from the editor itself:

.vimrc:

let $BASH_ENV = "$HOME/.bashrc"

autocmd FileType c map <F6> :!gcc -o "%:p:r.out" "%:." <bar> more<CR>
autocmd FileType c map <F7> :!%:p:r.out <CR>

.bash_aliases:

alias gcc="gcc -lm -ansi -Wall -g -O0 -Wwrite-strings -Wshadow -pedantic-errors -fstack-protector-all"

.bash_aliases is being sourced from .bashrc.

When I compile anything with F6, the gcc alias is not used. How can I get vim to use the alias?

This is a follow up question to my previous question.

2 Answers 2

2

The alias feature is disabled by default in non-interactive shells.

Put the following in your .bash_aliases file:

shopt -s expand_aliases

(You might want to have a custom file for non-interactive shells triggered from vim. There are probably lots of things in your .bashrc file which you don't want to happen.)

0

Most shells do not source their rc file (.shrc, .bashrc, .kshrc, .zshrc, etc.) for non-interactive shells, and Vim does not invoke interactive shells for :!-commands. If it did, Bad Things™ would happen.

I would recommend creating an actual shell script instead, then call it from your mapping. (And you could make your shell alias call that shell script too.)

4
  • Could you elaborate on "bad things"? I'm also trying to get all the things I like in normal shells to work within vim when run with "!". Running bashrc seems the obvious thing to do to me but is failing due to some odd interaction in my environment and installed plugins. Any background & references you could add would also be useful. Thanks
    – Tim Abell
    Apr 5, 2014 at 23:50
  • 1
    Well, for one, Vim is not a terminal emulator, and an interactive shell under Vim would misbehave, messing up display. Most shells have options and such that are only appropriate for a truly interactive shell. There are a lot of more subtle problems that can occur--there's a reason shells are designed to distinguish between running interactively and non-interactively. In general, if you're trying to make shell aliases work from :!-commands in Vim, you're probably approaching the problem the wrong way.
    – Heptite
    Apr 6, 2014 at 0:24
  • 1
    Thanks that's helpful. As an example I have aliased ls -al to ll and I want to be able to type !ll. I'm not sure how else to approach this :-) It's all part of my grand plan to never leave vim.
    – Tim Abell
    Apr 6, 2014 at 9:43
  • 1
    One of the "bad things" is that vim may immediately be stopped when you return to it. If you want to use all your functions an aliases, you can try a solution from this question.
    – cjs
    Jul 9, 2018 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .