3

How do I create shortcuts to highlight Terminal text: Ctrl+Shift+Right/Left, Ctrl+Shift+End, Ctrl+Shift+Home - as it's implemented in PowerShell ISE?

How do I create shortcuts to move cursor by words: Ctrl+Right/Left, similar to PowerShell ISE?

TylerH
  • 20,799
  • 66
  • 75
  • 101
gene golub
  • 51
  • 3
  • 7

3 Answers3

1

I'm adding a separate answer for users willing to install zsh.

This is a partial solution that makes no assumptions:

    {
        "key": "ctrl+left",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u001bb" },
        "when": "terminalFocus"
    },
    {
        "key": "ctrl+right",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u001bf" },
        "when": "terminalFocus"
    }

It will give you Windows-style Ctrl-Left/Right navigation.

This goes in your keybindings.json. Mine are at ~\AppData\Roaming\Code\User\keybindings.json and ~\AppData\Roaming\Code - Insiders\User\keybindings.json. (I run Windows on the desktop but do remote dev on Linux.)

I'll update if I manage to get any further with this. But it's not easy, because:

FSCKur
  • 920
  • 7
  • 16
0

am I correct by saying you want to use ctrl+left/right to navigate your cursor by start and end of a word?

go to your keyboard settings: code > preferences > keyboardShortcuts

search for:

cursorWordEndRight and bind to Ctrl+Right will allow you to move to the end of a word

cursorWordStartLeft bind to Ctrl+Left will allow you to move to start of a word

you may want to do the same for selecting:

cursorWordStartLeftSelect and bind to Ctrl+Shift+Left

cursorWordEndRightSelect and bind to Ctrl+Shift+Right

your keybindings.json should look something like this:

// Place your key bindings in this file to override the defaults
[
  {
    "key": "ctrl+right",
    "command": "cursorWordEndRight",
    "when": "textInputFocus"
  },
  {
    "key": "ctrl+left",
    "command": "cursorWordStartLeft",
    "when": "textInputFocus"
  },

  {
    "key": "shift+ctrl+left",
    "command": "cursorWordStartLeftSelect",
    "when": "textInputFocus"
  },
  {
    "key": "shift+ctrl+right",
    "command": "cursorWordEndRightSelect",
    "when": "textInputFocus"
  }
]

you may want to look at this link for reference to rebind your key shortcuts in vscode.

https://github.com/Microsoft/vscode/issues/34457

and

https://code.visualstudio.com/docs/getstarted/keybindings

Hope this helps :)

0

This is a nearly*-complete solution that requires use of zsh instead of bash. It works because zsh has a tool called zle, which allows you to mark regions - functionality that's normally handled by the console host.

* - Ctrl-C for clipboard copy isn't working for me, because I'm using remote docker and native Windows OpenSSH, which does not allow X11 forwarding; if that doesn't apply to you, I suggest experimenting with https://github.com/kutsan/zsh-system-clipboard and xsel (or xclip). In my scenario, I have experimentation to do with tmux and/or with output selections to file and having a vscode file watcher task

Credit to https://stackoverflow.com/users/480527/jamie-treworgy who answered here: Zsh zle shift selection

We have SSO, so I use a non-root user in my dev container that has the same username as my desktop username, and I rely on a pre-built image tagged with my desktop username.

In these steps I install zgen, which is a simple plugin manager that I then use to install zsh-autosuggestions, zsh-history-substring-search, and zsh-syntax-highlighting.git. I could not get oh-my-zsh to work, and zgen seemed the lightweight-est alternative.

Not shown here is the powerlevel10k theme, which also loads with zgen.

If all you want is the Ctrl-Shift-Left functionality, etc, you can skip the zgen bits, but it would be a missed opportunity :-)

devcontainer.json:

{
    "name": "devcontainer",
    "image": "devcontainer:${env:USERNAME}",

    "runArgs": [
        // Username
        "-u", "${env:USERNAME}",

        //    ...etc...

Dockerfile:

RUN echo "Setting up user ${USERNAME} with UID ${USER_UID} and GID ${USER_GID}" \
    && groupadd --gid $USER_GID $USERNAME \
    && useradd -s /usr/bin/zsh --uid $USER_UID --gid $USER_GID -m $USERNAME

RUN echo 'Installing zsh and zgen...' \
    && apt-get update \
    && apt-get install -y zsh \
    && git clone https://github.com/tarjoilija/zgen /home/${USERNAME}/.zgen --depth=1 \
    && chown ${USERNAME}:${USERNAME} /home/${USERNAME}/.zgen -R \
    #
    && echo 'Installing fonts...' \
    && apt-get install -y fonts-powerline \
    #
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

COPY --chown=${USERNAME}:${USERNAME} .zshrc /home/${USERNAME}/.zshrc
RUN chmod +x /home/${USERNAME}/.zshrc

...obviously you need to pass in USERNAME/USER_UID/USER_GID as build args.

.zshrc:

# Set up the prompt
autoload -Uz promptinit
promptinit
prompt adam1

setopt histignorealldups sharehistory

# Use emacs keybindings even if our EDITOR is set to vi
bindkey -e

# Keep 1000 lines of history within the shell and save it to ~/.zsh_history:
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.zsh_history

# Use modern completion system
autoload -Uz compinit
compinit

zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' menu select=long
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true

zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'

# load zgen
source "${HOME}/.zgen/zgen.zsh"

zgen load zsh-users/zsh-autosuggestions
zgen load zsh-users/zsh-history-substring-search
zgen load zsh-users/zsh-syntax-highlighting.git


# Windows-style keyboard bindings!
# https://stackoverflow.com/questions/5407916/zsh-zle-shift-selection
r-delregion() {
  if ((REGION_ACTIVE)) then
     zle kill-region
  else
    local widget_name=$1
    shift
    zle $widget_name -- $@
  fi
}

r-deselect() {
  ((REGION_ACTIVE = 0))
  local widget_name=$1
  shift
  zle $widget_name -- $@
}

r-select() {
  ((REGION_ACTIVE)) || zle set-mark-command
  local widget_name=$1
  shift
  zle $widget_name -- $@
}

for key     kcap   seq        mode   widget (
    sleft   kLFT   $'\e[1;2D' select   backward-char
    sright  kRIT   $'\e[1;2C' select   forward-char
    sup     kri    $'\e[1;2A' select   up-line-or-history
    sdown   kind   $'\e[1;2B' select   down-line-or-history

    send    kEND   $'\E[1;2F' select   end-of-line
    send2   x      $'\E[4;2~' select   end-of-line

    shome   kHOM   $'\E[1;2H' select   beginning-of-line
    shome2  x      $'\E[1;2~' select   beginning-of-line

    left    kcub1  $'\EOD'    deselect backward-char
    right   kcuf1  $'\EOC'    deselect forward-char

    end     kend   $'\EOF'    deselect end-of-line
    end2    x      $'\E4~'    deselect end-of-line

    home    khome  $'\EOH'    deselect beginning-of-line
    home2   x      $'\E1~'    deselect beginning-of-line

    csleft  x      $'\E[1;6D' select   backward-word
    csright x      $'\E[1;6C' select   forward-word
    csend   x      $'\E[1;6F' select   end-of-line
    cshome  x      $'\E[1;6H' select   beginning-of-line

    cleft   x      $'\E[1;5D' deselect backward-word
    cright  x      $'\E[1;5C' deselect forward-word

    del     kdch1   $'\E[3~'  delregion delete-char
    bs      x       $'^?'     delregion backward-delete-char

  ) {
  eval "key-$key() {
    r-$mode $widget \$@
  }"
  zle -N key-$key
  bindkey ${terminfo[$kcap]-$seq} key-$key
}

FSCKur
  • 920
  • 7
  • 16