Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dynamic alias, is it possible?

Tags:

alias

linux

zsh

I'm creating a library in the C language and have separate folders for each header/source file. I recently had the need to go into many of the folders and change some code in both the .c and .h files. So I'm frequently using the commands:

nano <some-detailed-filename.c>
nano <some-detailed-filename.h>

I wanted to look at creating an alias like the following:

alias nanc="nano $(ls ./*.c)"
alias nanh="nano $(ls ./*.h)"

The idea being that it will open whatever .c/.h files in the current directory. But when I add these lines to my .zshrc file, I get the following as the aliases:

# alias nanc
nanc='nano ./xh.c       ./yp.c'
# alias nanh
nanh='nano ./xh.h       ./yp.h'

This is not desired, as the files nano tries to open are only located in my home/login directory.

How can I go about creating this alias so that it uses the current directory I'm in when I issue the command? I'm not having much luck with my searches.

like image 805
Deanie Avatar asked Oct 26 '25 18:10

Deanie


1 Answers

Don't use double-quotes in the assignment; use single-quotes. If you use double-quotes, the value is substituted immediately.

By the way, there is another problem with your alias: the ls command will expand any wildcards which happen to be directory names (it is perfectly legal for a directory to have a name ending with ".c"). To guard against extra-odd results from that, you might want to use the "-d" option, e.g.,

alias nanc='nano $(ls -d ./*.c)'

However, even the ls is redundant. This works for me:

alias nanc='nano *.c'
like image 166
Thomas Dickey Avatar answered Oct 29 '25 09:10

Thomas Dickey