Vim input

Revision as of 22:10, 18 September 2024 by Neaqbua (talk | contribs) (Add instructions to write Toaq in vim with a system keyboard layout)

This article explains how to write Toaq in the text editor vim.

Using a keymap

Copy the contents below into a file called toaq.vim inside a folder named keymap in your .vim directory (create it if it doesn’t exist). Then, in insert mode, you may use any of the two-keystroke “combinations” and have them replaced with accented letters on the fly. (See :help mbyte-keymap for more detailed information.)

Without underdot With underdot
  .
  / '
  : "
  > ^
Toaq vim keymap
" toaq keymap
" by uakci on 2022-12-20
scriptencoding utf8
let b:keymap_name = "toaq"
loadkeymap
.i	i
i	ı
//	/
::	:
>>	>
''	'
""	"
^^	^
..	.
.<	«
.>	»
.{	‹
.}	›
./	́
.:	̈
.>	̂
.,	̣
.A	Ạ
.U	Ụ
.I	Ị
.O	Ọ
.E	Ẹ
.a	ạ
.u	ụ
.i	ı̣
.o	ọ
.e	ẹ
vy	ꝡ
Vy	Ꝡ
VY	Ꝡ
/A	Á
/U	Ú
/I	Í
/O	Ó
/E	É
/a	á
/u	ú
/i	í
/o	ó
/e	é
:A	Ä
:U	Ü
:I	Ï
:O	Ö
:E	Ë
:a	ä
:u	ü
:i	ï
:o	ö
:e	ë
>A	Â
>U	Û
>I	Î
>O	Ô
>E	Ê
>a	â
>u	û
>i	î
>o	ô
>e	ê
'A	Ạ́
'U	Ụ́
'I	Ị́
'O	Ọ́
'E	Ẹ́
'a	ạ́
'u	ụ́
'i	ị́
'o	ọ́
'e	ẹ́
"A	Ạ̈
"U	Ụ̈
"I	Ị̈
"O	Ọ̈
"E	Ẹ̈
"a	ạ̈
"u	ụ̈
"i	ị̈
"o	ọ̈
"e	ẹ̈
^A	Ậ
^U	Ụ̂
^I	Ị̂
^O	Ộ
^E	Ệ
^a	ậ
^u	ụ̂
^i	ị̂
^o	ộ
^e	ệ

Using digraphs

Alternatively, for sporadic usage, you can enter Toaq characters using Ctrl+K followed by two keystrokes (see :help digraph). Ꝡ is not available to input using this method.

Character Combo
  Ctrl+K vowel '
  Ctrl+K vowel :
  Ctrl+K vowel >
dotless i (ı) Ctrl+K . i
left quote («) Ctrl+K < <
right quote (») Ctrl+K > >

Using an existing keyboard layout

If you already have a Toaq keyboard layout (for example, by following the XKB instructions), then you can :source the following script to make vim automatically switch layouts depending on mode.

Follow the TODO instructions to make it work on your system. If you use an XKB method, be sure to use the name you gave your layout in evdev.lst and evdev.xml.

.vimrc-toaq
" Known issues:
" 1. Modifiers do not persist through a layout switch. You need to release and re-press Shift for commands like FA. If you want a visual reminder of when the layout has changed, uncomment the lines "set cursorline! | redraw".
" 2. The cursor disappears when awaiting single-character input to commands like r (actually, it moves to the command line). This is indicated by a fake "TOAQ CHAR" mode message.

" TODO: You need to choose a layout switching method from the choices below that works best for your system. Desktop Environments have their own methods of tracking and toggling the current keyboard layout. More info:
" 	https://unix.stackexchange.com/questions/402719/how-to-get-current-keyboard-layout-from-the-command-line
" In the examples, the layout-variant pairs are:
"  Layout   Variant
" --------+---------------
"  us     | dvorak
" --------+---------------
"  toaq   | [empty string]

function SetNormalLayout()
	""" Method 1 (Recommended): Native method provided by *your* DE (Example: KDE Plasma 5)
	silent call system('qdbus org.kde.keyboard /Layouts setLayout 0')
	""" Method 2 (XKB): Set directly with xkbcomp. Likely to pakala your DE.
	"silent call system('setxkbmap us dvorak -print | xkbcomp - $DISPLAY')
	""" Method 3 (XKB): Switch order of keymaps reported by X to trick DE. Can cause odd problems, but might play nicer. Get this info from 'setxkbmap -query'.
	"silent call system('setxkbmap -layout us,toaq -variant dvorak,')
	""" Method 4 (Untested): xdotool to simulate keyboard toggle key shortcut(s).

	"set cursorline! | redraw
endfunction

function SetToaqLayout()
	silent call system('qdbus org.kde.keyboard /Layouts setLayout 1')
	"silent call system('setxkbmap toaq -print | xkbcomp - $DISPLAY')
	"silent call system('setxkbmap -layout toaq,us -variant ,dvorak')

	"set cursorline! | redraw
endfunction

augroup toaq_layout_switch
	autocmd!
	autocmd VimEnter * call SetNormalLayout()
	"autocmd VimLeave * call SetNormalLayout()
	autocmd InsertEnter * call SetToaqLayout()
	autocmd InsertLeave * call SetNormalLayout()
	" The '/,\?' specifies search (forward, backward) only
	autocmd CmdlineEnter /,\? call SetToaqLayout()
	autocmd CmdlineLeave /,\? call SetNormalLayout()
augroup END

""" Remap some odd commands which expect buffer chars in operator-pending mode to work with Toaq buffer text. Overrides previous mappings!
" For example, you can type rı or ctꝡ from normal mode.

function ToaqGetCharStr()
	call SetToaqLayout()
	echohl ModeMsg | echo '-- TOAQ CHAR -- ' | echohl None
	let c = getcharstr()
	echo ''
	call SetNormalLayout()
	return c
endfunction

" Normal
nnoremap <silent> <expr> r "r" .. ToaqGetCharStr()
nnoremap <silent> <expr> f "f" .. ToaqGetCharStr()
nnoremap <silent> <expr> F "F" .. ToaqGetCharStr()
nnoremap <silent> <expr> t "t" .. ToaqGetCharStr()
nnoremap <silent> <expr> T "T" .. ToaqGetCharStr()
" Visual
vnoremap <silent> <expr> r "r" .. ToaqGetCharStr()
vnoremap <silent> <expr> f "f" .. ToaqGetCharStr()
vnoremap <silent> <expr> F "F" .. ToaqGetCharStr()
vnoremap <silent> <expr> t "t" .. ToaqGetCharStr()
vnoremap <silent> <expr> T "T" .. ToaqGetCharStr()
" Motions
onoremap <silent> <expr> f "f" .. ToaqGetCharStr()
onoremap <silent> <expr> F "F" .. ToaqGetCharStr()
onoremap <silent> <expr> t "t" .. ToaqGetCharStr()
onoremap <silent> <expr> T "T" .. ToaqGetCharStr()

You might want to add a mapping to your ~/.vimrc to quickly source the script:

noremap <Leader>t :source ~/.vimrc-toaq