2

In vim we can type Ctrl+v then Number in insert mode to insert special characters. This is mostly used to insert control characters. But any characters can be inserted if we know the ASCII number. E.g. typing Ctrl+v then 1, 2, 6 will insert a '~' character. See: http://vimdoc.sourceforge.net/htmldoc/insert.html#ins-special-keys I'd like to have the same or similar behaviour with zsh line editor. Note, I am using zsh in vi mode, but I don't think that matters. I've found that inserting an enter can be done with Ctrl+v then Ctrl+j, see: In ZSH line editor, how to insert a new line?. But how do I insert a tilde?

I need this, becasue I am working with a dumb citrix client, with which I cannot type the '~' character.

1 Answer 1

1

Three possibilities (~ has the ASCII code 126 or 176 in decimal or octal notation, resp.):

  • use the $'\OCT' syntax, where OCT is the ASCII code in octal notation:

    $ echo foo$'\176'bar
    foo~bar
    
  • use command substitution with printf:

    $ echo foo$(printf "\176")bar
    foo~bar
    
  • use this zle widget to resemble vi'S special char input:

    insert-special-char() {
      LBUFFER+=$(printf "\x$(printf %x $NUMERIC)")
    }
    zle -N insert-special-char
    

    where I borrowed the printf line from broaden.

    To use this widget, bind it to e.g. S in zle's vi-command-mode (or any other key you don't use):

    bindkey -M vicmd "S" insert-special-char
    

    Then type in the ASCII-Code (in decimal notation) while in zle's vi-command-mode and press S. Then the special character is inserted:

    ESC126S

2
  • The problem with the first two options is that they do not handle the path expansion correctly. Here is what I mean: ``` --> % ls $'\176' ls: ~: No such file or directory ``` So ~ is not expanded to the absolute path of my home directory. However option 3 is working properly as if I just had typed the '~' character. Sep 27, 2015 at 12:47
  • @GaborMarton: I see... perhaps ls $HOME is an easier-to-type alternative for you.
    – mpy
    Sep 27, 2015 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .