【发布时间】:2019-06-09 19:15:25
【问题描述】:
如何创建快捷方式来突出显示终端文本:Ctrl+Shift+Right/Left、Ctrl+Shift+End、Ctrl+Shift+Home - 因为它在 PowerShell ISE 中实现?
如何创建按单词移动光标的快捷方式:Ctrl+Right/Left,类似于 PowerShell ISE?
【问题讨论】:
标签: visual-studio-code terminal
如何创建快捷方式来突出显示终端文本:Ctrl+Shift+Right/Left、Ctrl+Shift+End、Ctrl+Shift+Home - 因为它在 PowerShell ISE 中实现?
如何创建按单词移动光标的快捷方式:Ctrl+Right/Left,类似于 PowerShell ISE?
【问题讨论】:
标签: visual-studio-code terminal
我正在为愿意安装 zsh 的用户添加单独的答案。
这是一个不做任何假设的部分解决方案:
{
"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"
}
它将为您提供 Windows 风格的 Ctrl-Left/Right 导航。
这在您的 keybindings.json 中。我的地址是~\AppData\Roaming\Code\User\keybindings.json 和~\AppData\Roaming\Code - Insiders\User\keybindings.json。 (我在桌面上运行 Windows,但在 Linux 上进行远程开发。)
如果我设法进一步了解这一点,我会更新。但这并不容易,因为:
【讨论】:
我说你想使用 ctrl+left/right 通过单词的开头和结尾来导航光标是否正确?
转到您的键盘设置:代码 > 首选项 > 键盘快捷键
搜索:
cursorWordEndRight 并绑定到 Ctrl+Right 将允许您移动到单词的末尾
cursorWordStartLeft 绑定到Ctrl+Left 将允许您移动到单词的开头
您可能希望对选择执行相同的操作:
cursorWordStartLeftSelect 并绑定到Ctrl+Shift+Left
cursorWordEndRightSelect 并绑定到Ctrl+Shift+Right
您的keybindings.json 应该如下所示:
// 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"
}
]
您可能希望查看此链接以作为在 vscode 中重新绑定您的快捷键的参考。
https://github.com/Microsoft/vscode/issues/34457
和
https://code.visualstudio.com/docs/getstarted/keybindings
希望这会有所帮助:)
【讨论】:
这是一个几乎*完整的解决方案,需要使用 zsh 而不是 bash。它之所以有效,是因为 zsh 有一个名为 zle 的工具,它允许您标记区域 - 通常由控制台主机处理的功能。
* - 用于剪贴板复制的 Ctrl-C 对我不起作用,因为我使用的是远程 docker 和本机 Windows OpenSSH,它不允许 X11 转发;如果这不适用于您,我建议尝试使用 https://github.com/kutsan/zsh-system-clipboard 和 xsel(或 xclip)。在我的场景中,我对 tmux 和/或对文件的输出选择进行了实验,并有一个 vscode 文件观察器任务
感谢https://stackoverflow.com/users/480527/jamie-treworgy 在这里回答的人:Zsh zle shift selection
我们有 SSO,因此我在我的开发容器中使用了一个非 root 用户,该用户与我的桌面用户名具有相同的用户名,并且我依赖于一个带有我的桌面用户名标记的预构建图像。
在这些步骤中,我安装了 zgen,它是一个简单的插件管理器,然后我用它来安装 zsh-autosuggestions、zsh-history-substring-search 和 zsh-syntax-highlighting.git。我无法让 oh-my-zsh 工作,而 zgen 似乎是最轻量级的替代方案。
此处未显示 powerlevel10k 主题,它也与 zgen 一起加载。
如果你想要的只是 Ctrl-Shift-Left 等功能,你可以跳过 zgen 位,但这将是一个错失的机会 :-)
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
...显然您需要传入 USERNAME/USER_UID/USER_GID 作为构建参数。
.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
}
【讨论】: