How to map Delete and End keys on tcsh shell? - shell

I use tcsh , and when Delete/End is pressed on cmd line, it simply shows up as ~ ; I have to press <Ctrl><e> to go to end of line. Can anyone help me to be able to use Delete/End keys as their name suggests ?

Those keys already worked on my Debian system. I found these commands in the /etc/csh.cshrc file:
if ($?tcsh && $?prompt) then
bindkey "\e[1~" beginning-of-line # Home
bindkey "\e[7~" beginning-of-line # Home rxvt
bindkey "\e[2~" overwrite-mode # Ins
bindkey "\e[3~" delete-char # Delete
bindkey "\e[4~" end-of-line # End
bindkey "\e[8~" end-of-line # End rxvt
endif

You need to use the bindkey builtin. See the tcsh man page discussion for details (search for 'bindkey'), but you want to add a line like
bindkey [end] end-of-line
to your .cshrc or .tcshrc file, replacing '[end]' with the actual end keypress. See this page for the list of binding names and helpful examples.

There's a great resource on fixing inconsistencies with delete/backspace here. The document also addresses mapping home/end, though that's not the focus.

Related

Insert character asigned to bindkey

In my .zshrc I have this setting:
# vim bindings
bindkey -v
bindkey ',' menu-complete
Now how can I map another key to insert literal ','?
Something like:
bindkey '^,' '\,' but precisely this is just like another alias for menu-complete (an alias to the alias)
The best solution for me would be cmd+, but I neither can't found how to use command as a modifier key!
The question is not only for this particular case. It's only an example.
Thanks in advance. Any contribution will be appreciated!

bindkey, arrow keys, windows version of tcsh

I have installed tcsh (www.tcsh.org) onto windows using the binary tcsh-6.18.00-x86.exe.gz at ftp://ftp.astron.com/pub/tcsh/
I am trying to use the arrow keys in the standard way (e.g. up arrow is the previous command in history, etc.)
bindkey does not recognize arrow keys, and I can't figure out how to bind arrow keys without using the pre-defined symbolic names such as "up" etc. I am running tcsh within consoleZ, but it does not work in a standard windows shell command window either.
When I type "bindkey", no arrow key assignments are shown. When I try the command:
bindkey -k up up-history
I get an error:
Invalid key name 'up'
I have lots of muscle memory invested in the arrow keys, which is why I am trying to resolve this.
Is there a newer version of tsch that resolves this error? Thank you. I am running Window 7 Professional.
This seems to be true for 6.16 also (x86). I've tried the x86 and x64 binary versions without success.
Thank you.
-Tony
From my tcshrc:
bindkey -b N-up history-search-backward
bindkey -b N-down history-search-forward
bindkey -b N-right forward-char
bindkey -b N-left backward-char
bindkey -b N-del delete-char
bindkey -b N-ins overwrite-mode
bindkey -b N-1 which-command
bindkey -b N-2 expand-history
bindkey -b N-3 complete-word-raw
bindkey -b N-home beginning-of-line
bindkey -b N-end end-of-line
bindkey -b M-x e_copy_to_clipboard
bindkey -b M-y e_paste_from_clipboard
This fixes most keys, it also sets Alt+x and Alt+y to copy & paste, since that won't work otherwise.
Also see this message I sent to the tcsh mailinglist a while back.
With the correct Google search, I found the answer here. The names of the keys are "N-up", "N-down", etc. rather than "up", "down", etc.
One uses the -b option, not -k.
http://mx.gw.com/pipermail/tcsh/2013-May/004657.html

Looking for ALT+LeftArrowKey solution in zsh

I just recently switched from bash to zsh, however I miss my Alt+LeftArrowKey and Alt+RightArrowKey to go back and forth a word at a time.
Right now, if I press Alt+LeftArrowKey I go back a couple of letters and then I'm stuck. I won't go any further backwards and it won't back to the end of the line with Alt+RightArrowKey as I would expect. I can't even use the arrow keys to go to the end of the line, only to the second to last. Can't input new chars on the line either or indeed delete.
How do I get my beloved shortcut back?
I'm on Mac OS X using Terminal if that's important.
Run cat then press keys to see the codes your shortcut send.
(Press Ctrl+C to kill the cat when you're done.)
For me, (ubuntu, konsole, xterm) pressing Alt+← sends ^[[1;3D, so i would put in my .zshrc
bindkey "^[[1;3C" forward-word
bindkey "^[[1;3D" backward-word
(Actually I prefer to use Ctrl + arrow to move word by word, like in a normal textbox under windows or linux gui.)
Related question: Fix key settings (Home/End/Insert/Delete) in .zshrc when running Zsh in Terminator Terminal Emulator
For anyone using iTerm, regardless of shell
All of the solutions offered here take a backwards approach in my opinion. You're essentially telling your shell to listen for some esc sequence or other key binding you have set in your terminal, creating compatibility issues when you switch shells (If you SSH into some other shell, switch from BASH to ZSH, etc and you lose some if not all of your keybindings).
Most shells have a set of default sequences that come pre-bound. Furthermore, while they aren't 100% consistent, they're close enough. So the easiest way that I have found to create keybinding for a particular action in the shell is to tell your terminal application to bind to the default keybindings that are consistent across shells.
I wrote a compressive solution for getting your terminal to respond as close to native mac keybindings here
Open the iTerm preferences ⌘+, and navigate to the Profiles tab (the Keys tab can be used, but adding keybinding to your profile allows you to save your profile and sync it to multiple computers) and keys sub-tab and enter the following:
Move cursor one word left
⌥+← Send Hex Codes: 0x1b 0x62
Move cursor one word right
⌥+→ Send Hex Codes: 0x1b 0x66
And that should give you the desired behavior not just in ZSH, but also if you SSH into a server running BASH, irb/pry, node etc.
Adding the following to ~/.zshrc worked for me on OSX Mountain Lion.
bindkey -e
bindkey '[C' forward-word
bindkey '[D' backward-word
On MacOS High Siera 10.13.6 or Mojave 10.14.2 and using iTerm2 with ZSH
To move from words I have to put like this:
bindkey "\e\e[D" backward-word
bindkey "\e\e[C" forward-word
Another solutions doesn't work fo rme
For iTerm, go to where this screenshot shows and select "Natural Text Editing"
if you already had some key mappings it will ask below, select accordingly not to lose any special bindings you set before. however, if you don't remember adding any bindings or just started using iTerm (on this machine), you will be safe to choose "Remove"
Though not strictly answering your question, the default binding for forward-word and backward-word are alt-f resp. alt-b.
This works everywhere, does not require you to leave the home row, and has a nice mnemonic property (f=forward, b=back), while also being consistent with ctrl-f and ctrl-b being forward-character and backward-character.
Rip out your arrow keys!
To make it work for me I used this answer, however I had to swap the codes (left <-> right)
⌥+← Send Hex Codes: 0x1b 0x66
⌥+→ Send Hex Codes: 0x1b 0x62
and add the following to my ~/.zshrc
bindkey -e
bindkey "^[b" backward-word
bindkey '^[f' forward-word
On MacOS Monterey, use the following in ~/.zshrc to make SHIFT + Arrows jump words:
bindkey "^[[1;2C" forward-word
bindkey "^[[1;2D" backward-word
And this for Option + Arrows:
bindkey "^[^[[C" forward-word
bindkey "^[^[[D" backward-word
On Mavericks (10.9.4) the code is 1;5... so for binding alt with arrows I have my .zshrc using this:
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
You can use CTRL+V and then the command you want to use
in Yosemite use Rob's solution
In zsh, you can use the bindkey command to see keyboard shortcuts.
Use bindkey to explore options that are available without custom keybindings.
Namely ^[b to move backward a word and ^[f to move forward a word.
These keybindings work with Alacritty on Arch Linux, just add them to the ~/.zshrc file
bindkey -e
bindkey "^[[3~" delete-char # Key Del
bindkey "^[[5~" beginning-of-buffer-or-history # Key Page Up
bindkey "^[[6~" end-of-buffer-or-history # Key Page Down
bindkey "^[[H" beginning-of-line # Key Home
bindkey "^[[F" end-of-line # Key End
bindkey "^[[1;3C" forward-word # Key Alt + Right
bindkey "^[[1;3D" backward-word # Key Alt + Left
If you're using iTerm in CSI u mode, the bindings for your .zshrc end up being:
bindkey '^[[1;3D' backward-word
bindkey '^[[1;3C' forward-word
If you want iTerminal to respect Emacs style shortcuts like ^Mf and ^Mb for forward/back a word I found best way to use this tip:
Making iTerm to translate 'meta-key' in the same way as in other OSes

Can I make control-u behavior be the same for ZSH as it is for Bash?

In Bash, control-u will clear from the curser to the beginning of the line, and put that text into the paste buffer, which can be pasted with control-y.
In Zsh, it will clear the text, but does not put it into the buffer.
sadness.
This can be done really easily with just two bindkey commands:
bindkey '^U' backward-kill-line
bindkey '^Y' yank
It may be worth noting that at least '^Y' should be bound in the default Emacs keymap.

Which shortcut in Zsh does the same as Ctrl-U in Bash?

In Bash, when I am typing a command, I press Ctrl+U, all characters from the beginning of the line to the cursor are going to be removed. However, in zsh, if I pressed Ctrl+U, the whole line is gone.
How to do the same in Zsh as in Bash?
It sounds like you'd like for Ctrl+U to be bound to backward-kill-line rather than kill-whole-line, so add this to your .zshrc:
bindkey \^U backward-kill-line
The bindkey builtin and the available editing commands (“widgets”) are documented in the zshzle man page.

Resources