Search code examples
shellunixtcsh

Move word by word in Unix command line


I work on tcsh and would like to navigate word forwards, backwards in Unix command line.

Google search shows Alt + f, Alt + b for moving forward by one word and moving backward by one word. But it doesn't work on Unix command line. Could anyone please help?


Solution

  • You can use bindkey to list keybinds:

    [~]% bindkey | grep -E '(forward|backward)-word'
    
    "^[B"          -> backward-word
    "^[F"          -> forward-word
    "^[b"          -> backward-word
    "^[f"          -> forward-word
    

    ^[ is the escape character, so this is Esc followed by f or F. Some terminal emulators may also send Alt as the escape character (but yours obviously doesn't).

    Your settings may be different, but this seems to be the default, on my Linux & FreeBSD systems.

    To set this to, for example, CTRL + f, you can use:

     bindkey '^f' forward-word
    

    Or for Alt + f:

    bindkey 'M-f' forward-word
    

    Add this to your ~/.tcshrc to make it permanent.

    See the manpage entry on bindkey for more information.