command substitution not working in alias?

242 views Asked by At

I wanted to make an alias for launching a vim session with all the c/header/makefiles, etc loaded into the buffer.

shopt -s extglob
alias vimc="files=$(ls -A *.?(c|h|mk|[1-9]) .gitconfig [mM]akefile 2>/dev/null); [[ -z $files ]] || vim $files"

When I run the command enclosed within the quotations from the shell, it works but when run as the alias itself, it does not. Running vimc, causes vim to launch only in the first matched file(which happens to be the Makefile) and the other files(names) are executed as commands for some reason(of course unsuccessfully). I tried fiddling around and it seems that the command substitution introduces the problem. Because running only the ls produces expected output.

I cannot use xargs with vim because it breaks the terminal display.

Can anyone explain what might be causing this ? Here is some output:

$ ls
Makefile  readme  main.1  main.c  header.h  config.mk

$ vimc
main.1: command not found
main.c: command not found
.gitignore: command not found
header.h: command not found
config.mk: command not found

On an related note, would it be possible to do what I intend to do above in a "single line", i.e without storing it into a variable files and checking to see if it is empty, using only the output stream from ls?

0

There are 0 answers