37

I found this helpful command bind -x '"\C-r"':reset to clear the terminal but I wanted to make a simple bash script:

#!/bin/bash
bind -x '"\C-r"':reset

output:

alfred@alfred-laptop:~/bash$ ./bind 
./bind: line 2: bind: warning: line editing not enabled

Could someone please explain:

  1. How can I fix this?
  2. What does warning: line editing not enabled mean?
squircle
  • 6,773
Alfred
  • 571

5 Answers5

16

You need to source that script. Do . ./bind or source ./bind to make that key binding active in the current session.

Running it normally, it doesn't have a terminal so it gives you that error message. Also, if it were to work, it would only be active for the duration of the script.

If you want that keybinding to be persistent, add that command to your ~/.bashrc.

10

I had a similar message but mine was from a script being run outside of an interactive (login) shell; it was a shell script being run via a CGI script. My message was:

/home/richard/.bash_profile: line 4: bind: warning: line editing not enabled

and although it wasn't actually on line 4, the only bind in that file was:

bind 'set completion-ignore-case on'

which of course only makes sense if line editing is enabled, i.e. if it's an interactive shell.

IpsRich
  • 200
9

Just trying to improve the accepted answer.

You get "line editing not enabled" when running in a non-interactive shell. The shell becomes non-interactive when started with -c or e.g. when stdin/stderr are not TTYs.

Wrap your bind commands in a check like this:

if [[ $- = *i* ]]
then
    bind -x '"\C-r"':reset
fi

The special parameter $- lists all active bash options. The i option signifies an interactive shell.

For more details about bash options, see here.

rustyx
  • 1,118
2

I don't have enough reputation to comment the answer of rustyx. So I add an answer instead but just improving the answer of rustyx.

Using test "-t 1" to check if bind will work is not enough. See Remote SSH Commands - bash bind warning: line editing not enabled for explanation and also some improved checks.

What worked best for me for Bash was using one of the checks described in https://www.gnu.org/software/bash/manual/bash.html#Is-this-Shell-Interactive_003f :

  • test if $PS1 is not zero
  • test if $- contains "i"

E.g.

if [ "$PS1" ] ; then
  # shell is interactive
  bind -x '"\C-r"':reset
fi
lgnom
  • 41
  • 3
1

I would test if the shell is interactive instead.
Here's a block of code I found in my distro's bashrc file

# Test for an interactive shell.  There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
# if [[ $- != *i* ]] ; then
#   # Shell is non-interactive.  Be done now!
#   return
# fi
ychaouche
  • 541