0

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?

First User
  • 704
  • 5
  • 12
  • 2
    Please read bout difference between single quotes and double quotes. So why not a function? – KamilCuk Nov 22 '20 at 19:03
  • I tried single quotes instead and it worked. I'm reading up on it right now, thanks for the advice, I never really thought too much about the quotations I used in aliases. Any thoughts about the second part ? – First User Nov 22 '20 at 19:13
  • 1
    @FirstUser : In your definition, the command substitution is executed at the time the alias is defined, not executed. Aside from this, why do you need `ls` to generate a list of files? – user1934428 Nov 23 '20 at 08:42
  • I dont want to open vim unless there are files in the directory matching the pattern. – First User Nov 23 '20 at 10:00

0 Answers0