Skip to content

Instantly share code, notes, and snippets.

@wolandark
Created September 30, 2023 00:36
Show Gist options
  • Save wolandark/6af85607ee26055bb519ac69d03edb07 to your computer and use it in GitHub Desktop.
Save wolandark/6af85607ee26055bb519ac69d03edb07 to your computer and use it in GitHub Desktop.
Settings for using an alternative keymapping in Vim
let g:alt_keymap = 'persian' " Change to your prefered keymap located at $VIMRUNTIME/keymap/
let g:alt_enabled = 1
function! CallToggleKeymap()
if g:alt_enabled
call ToggleKeymap()
endif
endfunction
function! ToggleKeymap()
if &keymap == ''
execute 'setlocal keymap=' . g:alt_keymap
silent !echo -ne "\033]12;cyan\007"
redraw!
autocmd VimLeave * silent !echo -ne "\033]112\007"
else
set keymap=
silent !echo -ne "\033]112\007"
redraw!
endif
endfunction
command! SwitchKeymap call CallToggleKeymap()
function! ListKeymapFiles()
:Explore $VIMRUNTIME/keymap/
endfunction
" Optionally create custom key mappings to SwitchKeymap command
@wolandark
Copy link
Author

wolandark commented Sep 30, 2023

Note on the gist

The escape sequences are there to change the cursor color when the keymap changes. I chose cyan.
This is not needed and can be removed from the functions.

Essentially all that is needed is to provide a truthy value to g:alt_enabled, such as 1 and to set your keymap variable in g:alt_keymap.

How it works

  1. We check to see if g:alt_enabled is truthy and then call the main function.
  2. In the main function, we check to see if the keymap variable is set to empty, because :set keymap= sets the keymap to default, which is English. If the check comes out true, we switch the keymap to the value of the g:alt_keymap and send escape codes to the terminal for a different cursor color.

Vim can't change the cursor color or shape when running in the terminal.

  1. We make sure that are cursor color is reseted at VimLeave and also reset the cursor color when the default empty string keymap is set.
  2. The redraw command is essential, even though it makes the terminal flash for a second, without it statuslines and other things will look broken.

There is an easier way to change cursor color for Gvim, but I don't use Gvim. See :h guicursor and :h lcursor.

Commands:

  • To Toggle Keymaps:
    :CallToggleKeymap || :SwitchKeymap

  • To Browse Available Keymaps"
    :ListKeymapFiles

Keybinding

inoremap <C-p> <C-o>:SwitchKeymap<CR>
nnoremap <C-p> :SwitchKeymap<CR>

Keep in mind that C-p in insert mode is bound to match previous (see :h i_CTRL-P) and in normal mode does the same as k.
You may want to use other keys.

About Escape Codes

Most decent terminals support changing the cursor shape and color with ANSI escape codes.
Terminals such as but not limited to:

  • Xterm
  • RXVT
  • XFCE4-Teminal
  • LxTerminal
  • Kitty
  • Alacritty
    etc ...

You can test your terminal with these commands:
printf '\e]12;rgb:ff/88/cc\007' should give you a pink cursor
echo -ne "\033]12;cyan\007" should give you a cyan cursor just like in the gist

@wolandark
Copy link
Author

keymap-2023-09-30_05.36.13.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment