1

I am a noob at this.

alias gc = git commit -m

I want:

  • After typing gc in terminal
  • press space and it will turn into git commit -m
  • if i don't want that, press esc
    and it turn into previous gc
  • correct that suppose gca and press space

You get the idea.

Any solution for this?

PS: Tried the shopt option shopt -s expand_aliases
If i write gc, it should turn into git commit -m in terminal
but it doesn't.

8
  • 4
    Note that the syntax for the alias is wrong, you can't have spaces around the = and if you have spaces in the command, the command needs to be quoted. So your alias should be: alias gc='git commit -m'. Now, do you also need to see this gc automatically expanded to git commit -m before execution? Can I ask why? Is it just to see it or is it so you can do something else? (If we understand why you want this, we can give a better answer).
    – terdon
    Apr 28, 2022 at 12:19
  • 1
    OK, but please explain why. You want to see it in order to be able to do something else. Maybe you want to see it to be sure before running. Or maybe you want to be able to edit it before running. We need to know what the final objective is in order to be able to give you a good answer. See What is the XY problem?.
    – terdon
    Apr 28, 2022 at 13:39
  • 1
    I see. That will require a tool, I think, it won't be easy. The expand_aliases option isn't relevant here, that is just to make aliases available where they would not normally be available (in scripts, for example).
    – terdon
    Apr 28, 2022 at 14:51
  • 1
    abbr -a sounds like a fish thing. Are you using fish or bash?
    – muru
    Apr 28, 2022 at 15:23
  • 1
    sorry.. yeah i used fish for abbr -a . fish shell already has half of the feature i mentioned in this post. But i want bash's one
    – Mega Bang
    Apr 28, 2022 at 15:43

1 Answer 1

2

I will be assuming that you have an alias in the bash shell, like

alias gc='git commit -m'

and that you want to expand this on the command line before you press Enter to execute the command.

You can do this by calling the Readline function alias-expand-line. This is easiest done by "binding" a key combination to trigger that function. Below I do that, choosing Ctrl+X+a fairly arbitrarily (it just need to not collide with any other key combination that I commonly use):

bind '"\C-xa": alias-expand-line'

After giving that bind command, you can start typing some command like so:

$ gc "my message"

Pressing Ctrl+X+a at any point on the line would expand the gc alias and turn that into

$ git commit -m "my message"

The bind command would typically be put into your ~/.bashrc file or wherever you put customisations for your interactive shell.

Readline functions can't "un-expand" aliases, but you could use Ctrl+X+Ctrl+U or Ctrl+_ to undo the expansion (or bind the undo Readline function to some other key combination of your choice).

6
  • Can't we make some function or something to do that?
    – Mega Bang
    Apr 28, 2022 at 16:46
  • @MegaBang It's unclear what "that" refers to.
    – Kusalananda
    Apr 28, 2022 at 16:49
  • by that i meant un-expand aliases
    – Mega Bang
    Apr 28, 2022 at 16:59
  • @MegaBang See added bit at the end of the answer, regarding "undo".
    – Kusalananda
    Apr 28, 2022 at 17:11
  • 1
    @MegaBang On some systems, using \C-@ might work. On my macOS system, that is hijacked by the terminal, so I can't test it. You can also use \M- (note the space at the end) to use Alt in place of Ctrl. See the answer to the following question why binding Ctrl+Space is (or might be) an issue: Why can't I use Ctrl+` as prefix in tmux?
    – Kusalananda
    Apr 29, 2022 at 6:02

You must log in to answer this question.

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