tyson
tyson

Reputation: 53

VIM. Sending commands to the terminal?

I'm trying to configure vim as my primary coding program. I have figured how to compile single files, but when I go to execute the program from within vim, I keep getting a 127 error code. I have a aliased on my box to ./a.out, however when I issue the command :!a from vim, it doesn't work. :!./a.out does. Does anyone know why this is?

Upvotes: 2

Views: 287

Answers (2)

Amadan
Amadan

Reputation: 198526

Aliases are a feature of your shell (say, bash). ! operator directly executes a file - shell never sees it, and can't do alias expansion on it. (EDIT: See ZyX)

If you want to make executing your ./a.out easy, you can do something like:

command XX !./a.out

then you can do :XX. Or,

nnoremap X :!./a.out<CR>

then a single key X will suffice.

Another option (which is frowned upon for security reasons) is to add the current directory to your PATH:

PATH="./$PATH"

which will allow you to run your program with just a.out as opposed to ./a.out. If you then compile your program with (a language-dependent isomorphism of) -o switch, you can have it named just a:

gcc -o a foo.c

Given this, !a would work. Use at your own risk.

EDIT: ZyX is correct-er. :) I'll leave the answer here for the other information.

Upvotes: 2

ZyX
ZyX

Reputation: 53674

Aliases are defined in rc files that are sourced by interactive shell only and work only in interactive mode (vim does pass everything to shell, it never executes anything except the shell directly with fork+execve).

By default shell launched from vim starts in non-interactive mode hence bashrc is not read and no aliases are defined (though even if they were defined, they won’t be used in non-interactive mode). You may set

set shellcmdflag=-ic

, then shell will be launched in interactive mode and .bashrc file with your alias will be read.

Upvotes: 3

Related Questions