-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
binding keys #1008
Comments
Questions such as this are better sent to Stack Overflow: But something like (press ctrl-v, ctrl-l to insert the ^L):
|
I did look at the stackoverflow community and found a few answers that all seem to be related to using emacs; such as this one: https://unix.stackexchange.com/questions/294592/change-the-key-that-show-previous-command-lines-in-ksh I have set -o vi in my local .kshrc file as well as a few alias and EDITOR even after trying the above command in my terminal; pressing CTRL + L just shows ^L and pressing enter just say : not found Googling around for trap ksh I find this article: https://docstore.mik.ua/orelly/unix3/korn/ch08_04.htm Ant that successfully remaps CTRL + C to print that message but Its above my head why it works and the other one does not. |
The
Again, the The unix.stackexchange.com site may be a better fit for these kinds of questions (than StackOverflow). |
I think that I'm missing something here. Typically in the terminal pressing CTRL+C will cause whatever you've currently typed to get cleared; i'm running ksh --version
version sh (AT&T Research) 2017.0.0-devel-2040-g36a76aca If I press CTRL+C the current line gets cleared out but if I press CTRL+L I figured out that typing clear will clear the entire terminal screen. I'd like to bind the function clear to the key combination CTRL+L I haven't found any way to do that on my system, all the answers that I see on stackexchange community talks about setting editing mode to emacs; I've never used emacs and use vi. The suggested answers above do not work, that's what I am trying to sort out. Can I do anything, provide addtional debugging info or something to get the desired behavior? |
I can confirm that the code that I posted in my previous comment works in exactly the same development version that you are using, and that it works in Vi mode. If you are copying and pasting the code, then you have not read what we are writing. The |
You're correct I misunderstood your original post. How do I get the control sequence that represents ^ and L The only thing that I could think of doing is running xev and trying to get the key codes but after failing at that, how do I get what control sequence that ksh is looking for? |
@teamblubee This has nothing to do with X keycodes. Shells like ksh work on streams of bytes which historically were given meaning by the ASCII standard. Also, despite what others have said it is a really bad idea to embed literal control characters in a ksh script (other than a handful like newline and tab). It's better to use a symbolic representation and convert it to the literal character where needed. There are several ways to do this. I happen to like
|
@krader1961 Agreed. I just couldn't remember the proper escape sequence and was being lazy. But thanks to your example, it can be simplified even further, but without the
Note the leading |
This seems to work, it clears the terminal buffer but then it freeses
This one works as expected. Just to be clear I am not trying to put these in any shell scripts. I just want to configure my shell's behavior. I think in bash it's the bind command. I did read this: https://docstore.mik.ua/orelly/unix3/korn/ch10_03.htm before asking the question but the example they give is a bit over my head. # Quoted from Page 98 of
# The New KornShell Command and Programming Language
1 typeset -A Keytable
2 trap 'eval "${Keytable[${.sh.edchar}]}"' KEYBD
3 function keybind # key [action]
4 {
5 typeset key=$(print -f "%q" "$2")
6 case $# in
7 2) Keytable[$1]=' .sh.edchar=${.sh.edmode}'"$key"
8 ;;
9 1) unset Keytable[$1]
10 ;;
11 *) print -u2 "Usage: $0 key [action]"
12 return 2 # usage errors return 2 by default
13 ;;
14 esac
15 } Just trying to wrap my head around the control sequence and where can I read more about them in relation to ksh? |
No, it simply doesn't redraw the command line. You really do not want to replace the [ctrl-L] with [ctrl-M] as that causes the current command line, if any, to be executed. Type
I don't understand the question. At the end of the day I can't really recommend ksh as an interactive shell. Other shells, like fish, have far saner out-of-the-box interactive behavior. And make it far easier to customize key bindings. Not to mention providing a way to force the command line to be redrawn. For example, fish's [ctrl-L] binding does |
Oh, I see that CRTL+M and the script above trap $'[[ ${.sh.edchar} == "\cl" ]] && { .sh.edchar="\cm"; clear; }' KEYBD does seem to clear the buffer but it also executes unfinished commands. The reason why I settled on KSH is are; If there are any lightweight posix shells I could start digging there. I didn't realize that CTRL+M will clear the screen but also execute the partial commands and like you accurately point out, that is undesired behavior. I think if I could bind CTRL+L to the command clear that would be sufficient. I don't mind digging in deep to learn these things it's just a bit challenging to find where to start looking sometimes. |
+1 I think I now understand the problem. The
Personally, I prefer the However,
which is closer to the After further investigation, I discovered that it is possible to map the trap $'[[ ${.sh.edchar} == "\cL" ]] && { .sh.edchar="\e\cL"; }' KEYBD One problem/idiosyncrasy I noticed was that it is only reprinting the last line of a multi-line prompt (which is what I use), which might be fine for PS1=$'$USER@$HOST $PWD\cj$ '
I think this issue has highlighted the lack of freely available online documentation to augment the |
I will just get use to the current behavior. Addtional documentation would be greatly appreciated. [EDIT] |
Not that I'm aware of. There is the classic book "The New Kornshell" but the documentation in the project has many problems. Not least because the source for the ksh "man" page has very different text for builtin commands than you will see if you run |
@teamblubee, did you try my last suggestion, which should make Ctrl-L clear the screen and refresh the prompt like you are after: trap $'[[ ${.sh.edchar} == "\cL" ]] && { .sh.edchar="\e\cL"; }' KEYBD |
I did try this and it creates quite a few bugs. I am not sure if I am unclear in the description of what I am trying to do. ksh has a command 'clear' which clears out everything written to the terminal and puts the cursor at the top of the terminal window. I'd just like to map that to the key sequence 'CTRL + L' |
@teamblubee I'm betting you're using vi mode. The [meta][ctrl-L] (
But that keyboard trap doesn't work in vi mode because vi mode doesn't support that behavior. After looking at the code some more I don't think there is any way to get it to work the way you want. You'll need to If you look at the code in the src/cmd/ksh93/edit directory (the emacs.c and vi.c modules being relevant to this issue) I hope you'll agree with me that, like much of the ksh code, it needs to be refactored. And much of the functionality, such as forcing the command line to be redrawn, abstracted into functions that can be invoked from a ksh script by name rather than inserting magic, hard coded, sequences of bytes into the input stream. P.S., Not that it really matters but FWIW the |
As I mentioned earlier, despite having used ksh88 then ksh93 as my primary interactive shell for ~15 years, I would never recommend using ksh as an interactive shell given the far friendlier alternatives like fish. On the other hand I wouldn't recommend using fish for scripting other than for making its interactive behavior do what you want. I'd love for ksh to be the best scripting shell that has good backward compatibility with scripts written 30+ years ago while simultaneously being a great interactive shell. But that isn't going to happen unless a large number of people who care about improving the situation begin contributing changes to the project. |
I like this attitude. Since you have all the experience with ksh and know the interactive shells. I never liked shells like zsh, they seem to do too much, I used csh, tcsh which are decently interactive but I cannot wrap my head around their parsing. From your experienced perspective, what's the list of features/ todo that needs to get implemented? p.s. |
I switched from ksh to zsh after a job change then used zsh for ~5 years. Then abandoned zsh after encountering this issue. The main problem with zsh is that there don't appear to be any competent software engineers managing its evolution. Which means that every random feature someone wants implemented appears to get included.
There are quite a few. In terms of enhancements perhaps the most important one is to extricate the documentation for builtin commands from the C source code. So that the With respect to bugs I would say that fixing all the use-after-free and similar failures when running the code under Valgrind, ASAN, or a debug malloc is critical. Memory leaks are a problem but rarely as serious as the use-after-free type of bug which can result in using incorrect data. |
I had a feeling zsh was run like this, just from looking at all the "cool stuff it can do" I don't need core features to be cool, a solid core w/ addons would be more maintainable; in my opinion. But telling that to an established community will most likely get shouted out the room so why bother. Okay, so it seems like we have somewhere to start and a few goals to shoot for. Moving to a modern version of C17 would also highlight a lot of bugs in the code. Also please not C and not C++ I am sure just compiling the code with std=c17 would throw a lot of warning, possibly errors. If you want to setup a wiki page and we can set some official todo's then the issues can get fixed in a way that shows progression. |
We can't mandate that as it needs to be possible to build ksh on much older platforms. We did, as a stepping stone, decide to require the C99 standard as a minimum. See issue #145. Also, mandating a newer compiler version isn't really needed to detect problems with the code. Tools like cppcheck, oclint, and Coverity Scan (all of which are currently enabled) detects a large number of potential problems.
No need for anything like a wiki page. When I find problems with the code, whether or not identified by the aforementioned tools, I open a Github issue. That is what I would expect anyone interested in improving the code to do. |
Ah. And I'm not sure if I am missing something, but this is working for me in trap $'[[ ${.sh.edchar} == "\cL" ]] && { clear; }' KEYBD Or, even printing clear's ansi escape sequence directly: trap $'[[ ${.sh.edchar} == "\cL" ]] && { print -n "\e[3;J\e[H\e[2J"; }' KEYBD @teamblubee, can you try one of those in |
@dannyweldon Your solution doesn't work because it doesn't cause the command line to be refreshed after the external |
Yep. I double-checked and I'm definitely in I was using the system standard |
@dannyweldon Doing
doesn't really work like the O.P. wants. Or that I would want. Yes, it runs
when in emacs mode does exhibit the desired behavior. But in vi mode it just redraws the current command line without clearing the screen. |
@krader1961, I was wondering if the problems you were seeing was because you were in insert mode. This new version now seems to behave the same as
|
How can I bind the keyboard shortcut ^L to call the clear function?
pressing Ctrl+C clears the line
pressing Ctrl+L just prints ^L to the console.
I looked into creating .xinputrc but that's not working.
How can I bind ^L to call the clear function?
The text was updated successfully, but these errors were encountered: