18

vim-xkbswitch 0.15.2 -- Automatic Keyboard Layout Switcher

 2 years ago
source link: https://www.vim.org/scripts/script.php?script_id=4503%230.15.2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Automatic Keyboard Layout Switcher : vim online

vim-xkbswitch : Automatic Keyboard Layout Switcher

 script karma  Rating 385/105, Downloaded by 11496  Comments, bugs, improvements  Vim wiki

created by Alexey Radkov   script type utility   description About:

Vim plugin XkbSwitch can be used to easily switch current keyboard layout back
and forth when entering and leaving Insert mode. Say you are typing some
document in Russian and have to leave Insert mode: when you press <Esc> your
keyboard layout switches to US/English automatically. When you further enter
Insert mode once again the Russian keyboard layout will be automatically
switched back!

XkbSwitch requires OS dependent keyboard layout switcher. Currently it depends
on xkb-switch (http://github.com/ierton/xkb-switch) for UNIX / X Server and
xkb-switch-win (http://github.com/DeXP/xkb-switch-win) for Windows.

For Mac OS X you can try Input Source Switcher
(http://github.com/vovkasm/input-source-switcher) and set library path as

let g:XkbSwitchLib = '/usr/local/lib/libInputSourceSwitcher.dylib'

Gnome 3 no longer works correctly with xkb-switch, so consider switching to
g3kb-switch (https://github.com/lyokha/g3kb-switch) if you are using this
environment. The library path should be set as

let g:XkbSwitchLib = '/usr/local/lib/libg3kbswitch.so'

github: http://github.com/lyokha/vim-xkbswitch


Features:

- Supported OS: UNIX / X Server, Windows, Mac OS X
  - Switches keyboard layout when entering / leaving Insert and Select modes
  - Keyboard layouts are stored separately for each buffer
  - Keyboard layouts are kept intact while navigating between windows or
    tabs without leaving Insert mode
  - Automatic loading of language-friendly Insert mode mappings. For example
    when Russian mappings have loaded then if there was a mapping

<C-G>S        <Plug>ISurround

a new mapping

<C-G>Ы        <Plug>ISurround

will be loaded. Insert mode mappings duplicates make it easy to apply
    existing maps in Insert mode without switching current keyboard layout
  - Fast and easy building of custom syntax based keyboard layout switching
    rules in Insert mode


Configuration:

Basic configuration requires only 1 line in your .vimrc:

let g:XkbSwitchEnabled = 1

Additionally path to the backend switcher library can be defined:

let g:XkbSwitchLib = '/usr/local/lib/libxkbswitch.so'

However normally it is not necessary as far as the plugin is able to find it
automatically. To enable Insert mode mappings duplicates user may want to add

let g:XkbSwitchIMappings = ['ru']

Here Insert mappings duplicates for Russian winkeys layout will be generated
whenever Insert mode is started. It is possible to define a list of different
layouts, for example

let g:XkbSwitchIMappings = ['ru', 'de']

but currently only Russian winkeys layout translation map ('ru') is supported
out of the box. There are 2 ways how a user can provide extra definitions of
keyboard layout translation maps (or replace existing default 'ru' map):

- Define variable g:XkbSwitchIMappingsTr:

let g:XkbSwitchIMappingsTr = {
                \ 'ru':
                \ {'<': 'qwertyuiop[]asdfghjkl;''zxcvbnm,.`/'.
                \       'QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?~@#$^&|',
                \  '>': 'йцукенгшщзхъфывапролджэячсмитьбюё.'.
                \       'ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,Ё"№;:?/'},
                \ 'de':
                \ {'<': 'yz-[];''/YZ{}:"<>?~@#^&*_',
                \  '>': 'zyßü+öä-ZYÜ*ÖÄ;:_°"§&/(?#'},
                \ }

- Create a file with layout translation maps and put its path into variable
    g:XkbSwitchIMappingsTrData, for example:

let g:XkbSwitchIMappingsTrData = $HOME.'/opt/xkbswitch.tr'

File with maps must follow this format:

ru  Russian winkeys layout
  < qwertyuiop[]asdfghjkl;'zxcvbnm,.`/QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?~@#$^&|
  > йцукенгшщзхъфывапролджэячсмитьбюё.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,Ё"№;:?/

de
  < yz-[];'/YZ{}:"<>?~@#^&*(_\
  > zyßü+öä-ZYÜ*ÖÄ;:_°"§&/()?#

Sample file xkbswitch.tr with exactly this content is shipped with this
    plugin distribution. It is encoded in UTF-8 and it is important as far as
    its content is read using readfile()! If your locale is not UTF-8 and
    you want to use this sample file then it seems that you will have to
    re-encode it in your locale standard encoding

Be very careful with mapping duplicates! They won't replace existing Insert
mode mappings but may define extra mappings that will change normal Insert
mode user experience. For example plugin echofunc defines Insert mode mappings
for '(' and ')', therefore assuming that in Deutsch translation map there
could be ')' to '=' translation, we would get '=' unusable in any keyboard
layout (as far as echofunc treats ')' in a very specific way). That is why
this translation is missing in example above and in file xkbswitch.tr content.

There are multiple examples of similar issues. For instance Russian winkeys
translate '.' into 'ю' and when you are editing a C/C++ source file with
enabled omnicompletion plugin character 'ю' (which you can use in comments)
will always be replaced by '.'. To address these issues starting from version
0.10 a new variable g:XkbSwitchSkipIMappings was introduced. It defines which
original Insert mode mappings should not be translated for specific filetypes.
Add into your .vimrc lines

let g:XkbSwitchSkipIMappings = {'c': ['.', '>', ':', '/*', '/*<CR>'],
            \ 'cpp': ['.', '>', ':', '/*', '/*<CR>']}

and now you will be able to print 'ю' in C and C++ source files. In this
example five Insert mode mappings were prohibited for translation in two
filetypes: C and C++, the first three correspond to omnicompletion plugin
and the last two address plugin c.vim. Why mappings duplicates starting from
'/' were added: Russian winkeys translate '/' into '.' and this makes vim
wait for a next character input after '.' was inserted which makes
omnicompletion plugin almost unusable.

Beware: variable g:XkbSwitchSkipIMappings is not parameterized by keyboard
layouts but only by filetypes.

By default saved Normal mode keyboard layout is restored when leaving Insert
mode, but you can specify to use particular layout for that:

let g:XkbSwitchNLayout = 'us'

Also you can specify original Insert mode keyboard layout:

let g:XkbSwitchILayout = 'us'

It makes sense to disable XkbSwitch for buffers with specific filetypes, for
example various file system or tag navigators. For example to disable
XkbSwitch for NerdTree add in your .vimrc line

let g:XkbSwitchSkipFt = [ 'nerdtree' ]

By default (e.g. when g:XkbSwitchSkipFt is not defined in .vimrc) following
filetypes are skipped: 'tagbar', 'gundo', 'nerdtree' and 'fuf' (FuzzyFinder).

You can enable XkbSwitch in runtime (e.g. when g:XkbSwitchEnabled is not set
in your .vimrc) by issuing command

:EnableXkbSwitch

This command will respect current settings of g:XkbSwitchIMappings etc. Be
aware that there is no way to disable XkbSwitch after it has been enabled.


Troubleshooting:

- There is a known issue when vim-latex package is installed. In this case
    entering Russian symbols in Insert mode when editing tex files becomes
    impossible. The issue arises from clashing XkbSwitch Insert mappings
    duplicates with mappings defined in vim-latex. To work this issue around
    you can disable XkbSwitch Insert mode mappings duplicates for filetype
    'tex':

let g:XkbSwitchIMappingsSkipFt = ['tex']

- Related to X Server only. When editing files on a remote host via ssh the
    ssh -X option must be supplied:

ssh -X remote.host

This option will make ssh forward X Server protocol messages between the
    local host and the remote host thus making it possible to switch the local
    host keyboard layouts.

- Related to GTK based gvim only. In bare X terminals keycodes for <C-S> and
    <C-Ы> are the same which makes it possible to leave sequences with control
    keys in Insert mode mappings duplicates as they are. But this is not the
    case in GTK based gvim. The issue is still investigated.

- XkbSwitch supports switching via Select mode too. But there is a case when
    switching from Select mode to Normal mode will fail to restore Normal mode
    keyboard layout. This will happen when leaving Select mode without any
    character having been entered. The reason is simple: vim does not generate
    events that could be caught by autocommands when switching from Select
    mode to Normal mode. A workaround could be: when you are leaving Select
    mode without any character entered do it via Visual mode, e.g. enter
    <C-G><Esc> instead simply <Esc>.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK