0

I'd like to bind Ctrl+J to a command in BASH, specifically I'd like to do this:

bind '"\C-j":"clear; ls *\n"'

But it seems that because Ctrl+J is the linefeed key, the escape sequence \n calls whatever Ctrl+J is bound to, so the result of pressing Ctrl+J is:

clear; ls *clear; ls *clear; ls *clear; ls *clear; ls * ...

Is there any way to bind Ctrl+J and retain the meaning of \n? Or is there an alternative to \n?

Abhishek
  • 6,912
  • 14
  • 59
  • 85
mskel
  • 842
  • 9
  • 16
  • I'm voting to close this question as off-topic because it's not about programming, it's about shell configuration. Could be a candidate for http://SuperUser.com/. – ghoti Sep 16 '17 at 13:23

4 Answers4

0

bind -p | grep "not bound" | grep -v "self-insert" | cat >> keycodes-notbound.txt bind -p | grep -v "not bound" | grep -v "self-insert" | cat >> keycodes-used.txt will give you a list of all keybindings.

That way you can see in the .txt file which keycodes are not bound/in use.

Could be, that you have to unbind the bound function (accept-line, IIRC) that is bound to C-j.

Somthing along in this How can I unbind and remap C-w in Bash? could help you, maybe.

Community
  • 1
  • 1
sjas
  • 18,644
  • 14
  • 87
  • 92
0

I assume you want the \n to make the command execute immediately. In that case, yes, there is an alternative:

bind '"\C-j":"clear; ls *\r"'

... should do what you want. (The \r is carriage return, usually bound to Ctrl+M, which usually has the same effect.)

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
psmears
  • 26,070
  • 4
  • 40
  • 48
0

If you want a shell command to be executed, you have to use the -x option:

bind -x '"\C-j":"clear; ls *"'

From help bind:

bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq]
 [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
Set Readline key bindings and variables.

Compare the parts:

[-x keyseq:shell-command]

and

[keyseq:readline-function or readline-command]

Without -x, bind controls readline functions or macros, and with -x bind will bind to a shell command to be executed.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
-1

Maybe you can use your window manager keyboard key bindings ?

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223