programing

VIM에서 CAPS LOCK 키를 매핑하는 방법은 무엇입니까?

firstcheck 2021. 1. 17. 10:54
반응형

VIM에서 CAPS LOCK 키를 매핑하는 방법은 무엇입니까?


Windows에서 GVIM을 사용하고 있습니다. 그리고 CAPSLOCK을 Ctrl + ^에 매핑하고 싶습니다.

이 작업을 수행 할 방법이 있습니까?

Btw, 나는 레지스트리 해킹을 사용하여 CAPS와 Esc를 바꾸는 방법을 웹에서 수많은 샘플을 보았지만, 그들 중 누구도 외부 도구와 레지스트리 변경 대신 VIM 맵 명령을 사용하지 않습니다.


리눅스? X에서 xmodmap을 사용하여 키 매핑을 변경합니다. 예 :

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

Esc를 CapsLock 키에 매핑합니다. 더 많은 예를 보려면 Google.


Vim 외부에서 작업하지 않으려는 의도라면 .vimrc에 다음 줄을 넣을 수 있습니다.

au VimEnter * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au VimLeave * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'

첫 번째 줄은 Vim에 들어갈 때 Caps Lock 키로 이스케이프되고 두 번째 줄은 종료 할 때 Caps Lock으로 정상적인 기능을 반환합니다.

이를 위해서는 xorg-xmodmap 패키지가 설치된 Linux가 필요합니다.


창문 아래? AutoHotkey를 사용하십시오 . vim 매핑은 아니지만 다른 사람들이 언급했듯이 매핑 할 수 없습니다. AHK를 사용하여 CAPSLOCK을 CTRL에 매핑합니다.


Mac OS의 경우 '시스템 환경 설정'에서 'Caps Lock'키 시스템 전체를 다시 매핑 할 수 있습니다.

다음 경로를 따르십시오.

시스템 환경 설정> 키보드> 수정 자 키

그런 다음 'Caps Lock'옆에있는 드롭 다운 상자를 클릭하고 '^ Control'을 선택합니다.


Capslock (및 Control, Shift 등)은 수정 자 키로, 해당 키의 의미를 수정하기 위해 다른 일반 키와 함께 사용됨을 의미합니다. AFAIK OS는 일반 키도 눌러지지 않는 한 수정 자 키를 응용 프로그램에 전달하지 않습니다. 예를 들어 CTRL응용 프로그램에서 누르는 것은 표시되지 않지만 표시 CTRL-C됩니다.


Linux 시스템에서는 xmodmap을 사용하여 수행 할 수 있습니다.

홈 폴더의 텍스트 파일에 저장

! Swap caps lock and escape
remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock

이 파일을 .capstoescswitc와 같은 이름으로 저장하십시오.

그런 다음 터미널을 통해이 파일을 실행합니다.

xmodmap ~/.capstoescswitc 

존경하고 싶다면 스크립트 파일에서 키 변수를 전환하기 만하면됩니다.

자세한 정보는이 페이지를 참조 하십시오.


Caps LockVim 외부에서 깨지지 않는 솔루션

윈도우

  1. 자동 단축키를 설치합니다.
  2. autohotkey 스크립트 실행 :
;caps_to_esc.ahk
#IfWinActive, ahk_class Vim ; vim window class
Capslock::Esc
#IfWinActive

리눅스

다음 명령을 실행하십시오.

wget -O - https://raw.githubusercontent.com/grabantot/scripts/master/install/install_caps_to_esc.sh | bash

또는 다음 작업을 수동으로 수행하십시오.

  1. sudo apt-get install xdotool xbindkeys. xpropxset(기본적으로 설치되어야 함) 도 사용 합니다.
  2. ~/caps_to_esc.sh스크립트 생성 :
debug_file=/dev/shm/caps_to_esc.debug
debug_msg () {
  echo $(date +%s%3N) "$@" >> $debug_file
}

caps_off () {
  is_caps_on="false"
  xset q | grep "Caps Lock:\s*on" && is_caps_on="true"
  debug_msg "is_caps_on ""$is_caps_on"

  [ "$is_caps_on" == "false" ] && return 3
  debug_msg "Sending Caps Lock"
  debug_msg "ignore_next"
  xdotool key Caps_Lock
}

should_ignore="false"
tail -n 1 $debug_file | grep "ignore_next" && should_ignore="true"

if [ "$should_ignore" == "true" ]; then
  debug_msg "ignored"
  exit 1
fi

echo -n "" > $debug_file

# get wm_class by 'xprop | grep WM_CLASS'
declare -a wm_classes=( \
  'WM_CLASS(STRING) = "gnome-terminal-server", "Gnome-terminal"' \
  'WM_CLASS(STRING) = "gvim", "Gvim"' \
  'WM_CLASS(STRING) = "code", "Code"' \
  'WM_CLASS(STRING) = "google-chrome", "Google-chrome"' \
)

active_window_id=$(xdotool getactivewindow)
active_window_wm_class=$(xprop -id $active_window_id WM_CLASS)
debug_msg "active_wm_class   ""$active_window_wm_class"

detected_wm_class=""
for wm_class in "${wm_classes[@]}"; do
  # debug_msg "$wm_class"
  if [ "$active_window_wm_class" == "$wm_class" ]; then
    detected_wm_class="$wm_class"
    debug_msg "detected_wm_class ""$detected_wm_class"
  fi
done

[ "$detected_wm_class" == "" ] && exit 2
xdotool keyup "Caps_Lock" # !!! very important
caps_off
debug_msg "Sending Escape"
xdotool key "Escape"
debug_msg "sent"
  1. 다음에 새 bindnig 추가 ~/.xbindkeysrc:
"bash $HOME/caps_to_esc.sh"
Caps_Lock
  1. killall xbindkeys && xbindkeys

작동 원리 :

  1. xbindkeysCaps_Lock을 누르고 caps_to_esc.sh스크립트를 호출 할 때 감지합니다.
  2. 스크립트에서 활성 창 wm_class 감지 xprop
  3. wm_class가 우리에게 관심이 있는지 확인하고 (gnome-terminal, vscode, gvim, chrome) 그렇지 않으면 종료하십시오.
  4. Escape 키를 통해 xdotool
  5. check if Caps Lock is on via xset and if it is then send Caps_Lock key via xdotool
  6. xbindkeys will detect the Caps_Lock sent by us but we ignore it

I dont think you can. I believe CAPS-LOCK is probably translated by the OS before vim ever sees it. So you'd need to do a hack at the OS level, like the registry hacks you've already seen.

EDIT: autohotkey looks like it could be used to bridge the vim-OS gap. This way a thirdparty app is doing the hacks at the OS level, and you're just hooking that app.


Since there is a solution for Linux and Windows(Autohotkey), I´d like to suggest to use pckeyboardhack for Mac to remap CapsLock everywhere.


I guess one of the reasons for doing this is to create a soft capslock, like others have mentioned, possibly to avoid keeping capslock on while in normal mode. I've used the vimcaps plugin to turn off capslock when leaving insert mode, seems to work okay.


On mac, it is also possible to use Karabiner (https://pqrs.org/osx/karabiner/)

$ brew cask install karabiner-elements

Once installed, you can map capslock key to esc key in the simple modifications tab. Caveat is this is system wide, meaning that you lose capslock key everywhere. IMO who needs capslock.

ReferenceURL : https://stackoverflow.com/questions/2176532/how-to-map-caps-lock-key-in-vim

반응형