I'd like to add a keybinding to my .inputrc that puts the value of an environment variable, USER, on the command line.

I tried all kinds of escaping but the string $USER is written verbatim to the command line, not the value of that environment variable.

$if mode=vi
  # Keymaps when we are in insert mode
  set keymap vi-insert

  # Insert path before mountpoint
  "C-e": "/run/media/$USER"

Is there a reasonable way to do this with .inputrc or am I better off using other means like .bashrc?

  • 1
    Note, if you have a partly typed command with $variables in it, you can have them expanded by calling the readline function shell-expand-line, which you can bind to some character sequence. – meuh May 15 '18 at 18:25
  • Check out a similar question. – meuh May 15 '18 at 18:35

User meuh's tip to use shell-expand-line led me to this solution which puts the environment variable's value on the command line:

$if mode=vi
  # Keymaps when we are in insert mode
  set keymap vi-insert

  # Expand variables like ~ and $USER to their values
  "\C-a": shell-expand-line
  # Insert path before mountpoint, then expand the variable
  "\C-e": "/run/media/$USER/\C-a"

Now, pressing Ctrl+e results in /run/media/me/ on the command line.

  • 1
    Oh, nicely. If I was using bash, I might make some kind of use of this. – Kusalananda May 15 '18 at 19:19

Inserting the literal string $USER rather than the expanded value of the USER variable may not make a big difference, as the variable is likely defined with the proper value (assuming a Linux system) and would be expanded by the shell.

This answers an earlier incarnation of the question:

You can not use environment variables in .inputrc.

To incorporate the username of the current user in bash's primary prompt, set PS1 to a value containing the escape sequence \u.

See the section labelled "PROMPTING" in the bash manual.

  • Thanks for your answer, Kusalananda! I was thinking of putting text on the command line, not changing the command prompt. I edited the question accordingly. – Matthias Braun May 15 '18 at 17:48
  • @MatthiasBraun Ah, in that case, since $USER is most likely a valid environment variable on Linux systems (other systems use $LOGUSER), then I don't really see a problem with having the literal string $USER inserted. Care to elaborate? – Kusalananda May 15 '18 at 17:50
  • You're right, thinking about it, it's not a big difference between /run/media/$USER/ and /run/media/me/ for my purposes. Path completion works as well using bash on Arch Linux. – Matthias Braun May 15 '18 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.