# Need help with key bindings in .cshrc



## athan (Dec 24, 2016)

I want to enable some common key bindings for t/csh on my dedicated server (freebsd 11 rel) but noticed some really weird behavior!
Let me explain the situation. My .cshrc follows

```
umask 22
set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin)

setenv  EDITOR vi
setenv  PAGER less
setenv  LESS -iM
setenv  BLOCKSIZE K
setenv  NCURSES_NO_UTF8_ACS 1
setenv  CLICOLOR 1

if ($?prompt) then
        # An interactive shell -- set some stuff up
        set prompt = "%N:%~ %# "
        set promptchars = "%#"

        set filec
        set history = 5000
        set savehist = (5000 merge)
        set autolist = ambiguous
        # Use history to aid expansion
        set autoexpand
        set autorehash
        set mail = (/var/mail/$USER)

        if ( $?tcsh ) then
            bindkey "^W" backward-delete-word
            bindkey -k up history-search-backward
            bindkey -k down history-search-forward
            bindkey "\e[1~" beginning-of-line
            bindkey "\e[4~" end-of-line
            bindkey "\e[3~" delete-char
            [color=red]bindkey "^[[OC" forward-word
            bindkey "^[[OD" backward-word[/color]
        endif
endif
```

Those two lines in red, work fine when entered manually from csh command line or running _source .cshrc_ but not working automatically when log in shell.
That's really weird, because .cshrc is parsed normally, all other commands executed including the rest bindings, everything works except those two lines!

Please give some advice because I'm really confused! If those bindings were not supported by terminal why they work fine when entering them from command line or manually sourcing .cshrc?

PS: terminal is xterm accessed via putty. Also tried others with exactly the same results.

Thank you folks!


----------



## ASX (Dec 24, 2016)

Don't' know the exact answer, honestly I don't have a complete picture of your setuo, however you are using a different notation for the ESCAPE char in those two red lines ... ^[  while the rest are using "\e", so I would ttry to change them as follow:

```
bindkey "\e[OC" forward-word
            bindkey "\e[OD" backward-word
```
They should be equivalent (assuming you used CTRL-V followed by ESC to obtain ^[ which is a single character), but may be they get expanded differently at some point.


----------



## athan (Dec 24, 2016)

Thanks for answering ASX, however still not worked when parsed from .cshrc and that is the actual problem. These two bindkey commands work fine when entered from command line but not when autoexecuted from ..cshrc when log in.

Not mentioned my setup details, because it's  a default 11r installation with all defaults including the dot files.

BTW, all other key bindings in cshrc  work fine, only those two seem to be ignored!


----------



## SirDice (Dec 27, 2016)

Notice how the other, working, three bindings use '\e' instead of '^['. And you appear to have a '[' too many. Just look at the default mappings:

```
"^[OC"         -> forward-char
"^[OD"         -> backward-char
```


----------



## athan (Jan 7, 2017)

CTRL-LEFT cursor control sequence is  "^[[OD" (via od or ctrl-V) and can be written as \e[OD.
It works fine when binding is manually entered from command line, but not when parsed from .cshrc and that  is the actual issue.
Other shells, including bash, zsh, mksh etc have no problem with these btw

PS: I had to bind backward-word to ALT-Left  "^[^[[D" or "\e^[[D". This works, but ctrl-left nope!


----------



## wblock@ (Jan 8, 2017)

I never use these, but they are defined in my setup and worked just now when I tried them:

```
bindkey "\e[1;5D" vi-word-back
bindkey "\e[1;5C" vi-word-fwd
```


----------



## wblock@ (Jan 9, 2017)

What is your console "type" in /etc/ttys?

Never mind, it doesn't work in a true console here either.


----------



## CrazyMihey (Jul 9, 2017)

I have the same Issue.
As a temporary decision, I had to copy this two Lines from ~/.cshrc to ~/.login, and it works, but I suspect that this is not correct…
~.login:

```
bindkey   "^[OD"   backward-word       # Ctrl+Left Arrow.
bindkey   "^[OC"   forward-word       # Ctrl+Right Arrow.
```


----------



## SirDice (Jul 12, 2017)

CrazyMihey said:


> I had to copy this two Lines from ~/.cshrc to ~/.login,


Check your shell, you're probably using /bin/sh (or /usr/local/bin/bash) instead of /bin/csh or /bin/tcsh. The ~/.cshrc file is only read by the CSH shells.

Edit: Had to look it up. There are so many different scripts, for different shells, interactive vs. non-interactive, it's hard to keep track. But the remarks in ~/.login state:

```
# .login - csh login script, read by login shell, after `.cshrc' at login.
```

If it didn't work for you in ~/.cshrc you probably made a mistake there, perhaps you added it at the wrong place? Or introduced an error that prevented further execution. You can easily test it without having to logout and login again, simply by running: `source ~/.cshrc`. It should not produce errors.


----------



## poorandunlucky (Jul 16, 2017)

what if you `cd ~ && mv .cshrc .cshrc.old && cat .cshrc.old .cshrc`?

What if you echo the commands in .cshrc?

just out of curiosity, does it do the same thing if you set it in /etc?


----------

