Jump to content

Vim input: Difference between revisions

Add instructions to write Toaq in vim with a system keyboard layout
(Created page with "This article explains how to write Toaq in the text editor ''vim''. == Using a keymap == Copy the contents below into a file called <code>toaq.vim</code> inside a folder named <code>keymap</code> in your <code>.vim</code> 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 <code>:help mbyte-keymap</code> for more detailed information.) {...")
 
(Add instructions to write Toaq in vim with a system keyboard layout)
Line 137: Line 137:
| right quote ({{t|»}}) || {{Key|Ctrl+K}} {{Key|&gt;}} {{Key|&gt;}}
| right quote ({{t|»}}) || {{Key|Ctrl+K}} {{Key|&gt;}} {{Key|&gt;}}
|}
|}
== Using an existing keyboard layout ==
If you already have a Toaq keyboard layout (for example, by following [[Input_methods#XKB|the XKB instructions]]), then you can <code>:source</code> 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.
{{collapsible|.vimrc-toaq|<pre>" 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()
</pre>}}
You might want to add a mapping to your <code>~/.vimrc</code> to quickly source the script:
<code>noremap <Leader>t :source ~/.vimrc-toaq</code>


[[Category:Input methods]]
[[Category:Input methods]]
3

edits