Xkb configuration for emacs - emacs

I'm trying to remap three keys, left alt, right win and menu, to specific keys for emacs.
Specifically, I wants to use RWIN as Hyper, my left alt as Meta and My MENU as Alt.
I setup xkb in this way. there are two files, the first one is super_hyper (in ~/.xkb/symbols):
partial modifier_keys
xkb_symbols "standard" {
key <LALT> { [ Meta_L, Meta_L ] };
replace key <RWIN> { [ Hyper_R, Hyper_R ] };
replace key <MENU> { [ Alt_R, Alt_R ] };
modifier_map Mod1 { <META>, Meta_L };
modifier_map Mod3 { <HYPR>, Hyper_R };
modifier_map none { <ALT>, Alt_R };
};
the second file (~/.xkb/keymap/mykbd) is really simple:
xkb_keymap {
xkb_keycodes { include "evdev+aliases(qwerty)" };
xkb_types { include "complete" };
xkb_compat { include "complete" };
xkb_symbols { include "pc+it+inet(evdev)+compose(paus)+terminate(ctrl_alt_bksp)+super_hyper(standard)"};
xkb_geometry { include "pc(pc105)" };
};
Then I setup all with
xkbcomp -I$HOME/.xkb ~/.xkb/keymap/mykbd $DISPLAY
xev shows that all works perfectly. And emacs works well with the hyper (right windows key) and meta (left alt key) modifiers. But when I use the menu key it responds like a meta modifier, like the alt key. How can I solve?

Your left alt key should already be mapped as a meta key. I will show you on an example how to configure Emacs to recognize the AltGr (right alt) as a hyper key. You can adapt the example then to your needs.
Find out the key code of the AltGr key:
Run in a console: xev. Hit afterwards repeatedly the AltGr key and close then the program. In the output, you can read the key code for AltGr. I get:
...keycode 108 (keysym 0xffea, Alt_R)...
Next, we check how our keys are bound with the following command: xmodmap -pm. In my case, I get:
shift Shift_L (0x32), Shift_R (0x3e)
lock Caps_Lock (0x42)
control Control_L (0x25), Control_R (0x69)
mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd)
mod2 Num_Lock (0x4d)
mod3
mod4
mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
In hexadecimal, the keycode 108 (hex 0x6c) is associated with Alt_R.
Remove Alt_R from mod1 as we want to map it to hyper. This step is optional. For example, my menu key (hex code: 0x87) does not appear in this output. Therefore I can skip this step.
# remove all keys associated to mod1
xmodmap -e "clear mod1"
# but as we are only interested in Alt_R, re-assign those we removed wrongly
xmodmap -e "add mod1 = Alt_L Meta_L"
Now, we can re-assign AltGr key to Hyper (you can choose other keys such as: Alt_R or Super_L, Super_H, ...:
xmodmap -e "keycode 108 = Hyper_R"
Finally, the next step consists in assigning this key symbol to a modifier. I chose mod3 (mainly because it is empty):
xmodmap -e "add mod3 = Hyper_R"
In Emacs, when I hit now AltGr-f, it tells me that H-f is undefined. I can go now and use the hyper key for new keybindings:
(global-set-key (kbd "H-f") 'find-file)
If I had chosen in step 4-5 to assign AltGr and mod3 to Super_R, I would have used "S-f" for the new keybinding in step 6. Similarly, assigning AltGr to Alt_R in step 4-5 would have resulted in using e.g. "A-f" when setting the binding.
EDIT
If you want to assign a key (e.g. menu key) to Alt_R, one has to make sure that the Meta_L key is mapped (e.g. to mod1). If Meta_L (e.g. assigned to Alt_L) does not appear in your xmodmap -pm output, then very likely, your key (e.g. menu) acts as a further meta modifier.

Related

How can I use Autokey to grab highlighted text + url and then insert said highlighted text + url in the placeholders of a phrase?

I'm trying to get Autokey to work like Autohotkey worked for me in Windows.
One very useful function that was possible to set up in Autohotkey was assigning a keyboard key to grab highlighted text, then go to url, grab that url, and then insert the highlighted text and URL in specific places within a predetermined phrase.
It was extremely useful to create text with links in different formats.
The Autohotkey script for what I'm describing looked something like this:
insert::
clipboard =
Send, ^c
ClipWait
myText = %clipboard%
Send, !d
clipboard =
Send, ^c
ClipWait
myURL = %clipboard%
myLink = %myText%
clipboard = %myLink%
return
Is there a way to translate that into a working Autokey script?
# assigning a keyboard key to
# `hotkey - a key or combination of keys that, when pressed, will trigger AutoKey to do something; run a script, insert a phrase or display a menu. A hotkey can be created using any key on the keyboard (within reason), with or without one or more modifier keys. The modifier keys are Shift, Control, Alt and Super (a.k.a. Windows).`
# [Guide](https://github.com/autokey/autokey/wiki/Beginners-Guide)
import os, time
# grab highlighted text
myText = clipboard.get_selection()
# then go to url
myCmd = "/usr/bin/firefox --new-window http://www. your site .biz/"
os.system(myCmd)
time.sleep(0.25)
# grab that url, and then
keyboard.send_keys('<F6>')
time.sleep(0.25)
myURL = clipboard.get_selection()
# insert the highlighted text and URL in specific places within a predetermined phrase.
# run a window wait, then paste the texts there. Following example opens a msgbox2sec.ahk (create it first):
myCmd = 'wine ~/.wine/drive_c/Program\ Files/AutoHotkey/AutoHotkey.exe /home/administrator/Desktop/msgbox2sec.ahk'
os.system(myCmd)
time.sleep(0.25)
active_class = window.get_active_class()
if not active_class == "your class name": # for example: "autokey-gtk.Autokey-gtk":
time.sleep(0.25) # sleep longer
myLink = "" + myText + ""
# paste your link, then copy it:
keyboard.send_keys(myLink)
# select select a line:
keyboard.send_keys('<home>')
keyboard.send_keys("<shift>+<end>")
# copy line to clipboard:
keyboard.send_keys('<ctrl>+c')
# or copy line to variable:
c = clipboard.get_selection()

Remapping issue Autohotkey

Please, bear with me, I'm a autohotkey noob. I want left: control and shift to switch from their current key value to arrow: down and up respectively when pressing the F2 key. And when the F2 key is pressed again, I want left: control and shift to return to their original key value. This is my code so far:
F2::
{
if LShift = Up
{
LShift::Send {LShift}
}
else
{
LShift::Send {Up}
}
if LControl = Down
{
LControl::Send {LControl}
}
else
{
LControl::Send {Down}
}
}
Regrettably I haven't been able to make it work. Any help will be truly appreciated.
Hotkeys that go over multiple lines don't use curly braces. They
start with the hotkey label and end with a simple Return.
You can't enclose hotkey labels by normal if statements. You have to use
special #If statements instead.
Nesting hotkeys is also definitely not what you want to do.
To remap a key you can use the single line hotkey syntax.
You can pretty much short your code down to:
F2::(F2Toggle:=!F2Toggle)
#If F2Toggle
LShift::Up
LControl::Down
#If
Pressing F2 will toggle the variable F2Toggle between true and false. #If F2Toggle means if the variable F2Toggle is currently set to True then enable the hotkeys below. #If marks the end of the special #If statement.

How do I collapse sections of code in Visual Studio Code for Windows?

How do I fold or collapse sections of code in Visual Studio Code?
Is this feature supported?
Folding has been rolled out and is now implemented since Visual Studio Code version 0.10.11. There are these keyboard shortcuts available:
Fold folds the innermost uncollapsed region at the cursor:
Ctrl + Shift + [ on Windows and Linux
⌥ + ⌘ + [ on macOS
Unfold unfolds the collapsed region at the cursor:
Ctrl + Shift + ] on Windows and Linux
⌥ + ⌘ + ] on macOS
Fold All folds all regions in the editor:
Ctrl + K, Ctrl + 0 (zero) on Windows and Linux
⌘ + K, ⌘ +0 (zero) on macOS
Unfold All unfolds all regions in the editor:
Ctrl + K, Ctrl + J on Windows and Linux
⌘ + K, ⌘ + J on macOS
References: https://code.visualstudio.com/docs/getstarted/keybindings
As of Visual Studio Code version 1.12.0, April 2017, see Basic Editing > Folding section in the docs.
The default keys are:
Fold All: CTRL+K, CTRL+0 (zero)
Fold Level [n]: CTRL+K, CTRL+[n]*
Unfold All: CTRL+K, CTRL+J
Fold Region: CTRL+K, CTRL+[
Unfold Region: CTRL+K, CTRL+]
*Fold Level: to fold all but the most outer classes, try CTRL+K, CTRL+1
Macs: use ⌘ instead of CTRL (thanks Prajeet)
Also see the ability to fold any arbitrary lines of code as of Insiders v1.70. That is any lines you select can be folded!
See https://stackoverflow.com/a/72954133/836330 for the command and demo.
Create Manual Folding Range from Selection
editor.createFoldingRangeFromSelection
This is bound to the above create command: Ctrl+K Ctrl+,
Remove Manual Folding Ranges
editor.removeManualFoldingRanges
This is bound to the above remove command: Ctrl+K Ctrl+.
Code folding by regions has arrived with v1.17. Folding by regions documentation. And v1.19 and 1.23.
[Generally you can add a space, for example // region and // endregion to //region and //endregion and it will also work.]
TypeScript/JavaScript: //#region and //#endregion or // #region and // #endregion
C#: #region and #endregion
C/C++: #pragma region and #pragma endregion
F#: //#region and //#endregion
PowerShell: #region and #endregion
Python: #region and #endregion
VB: #Region and #End Region
PHP: #region and #endregion
Bat: ::#region and ::#endregion or REM #region and REM #endregion
Markdown: <!-- #region --> and <!-- #endregion -->
Golang //region and //endregion or //#region and //#endregion
Java //#region and //#endregion
CSS/SCSS/Less: /* #region */ and /* #endregion */ or /*#region*/ and /*#endregion*/
SCSS/Less: // #region and // #endregion
Go: // region, // endregion and // #region, // #endregion
shellscript: # region and # endregion
Perl5 #region and #endregion or =pod and =cut
sql --#region and --#endregion
Important: If you don't see your language in the list ::
Each language also has snippets available for the markers. Type '#' and invoke code completion to see them. To have region markers configured for your language, contact the language extension provider.
So type # and then Ctrl+Space to see the region markers for any language.
This feature is available in the standard build now. To make the collapse/expand controls appears, you need to mouse over the area just to the right of the line numbers as shown in this screenshot:
You should add user settings:
{
"editor.showFoldingControls": "always",
"editor.folding": true,
"editor.foldingStrategy": "indentation",
}
ctrl + k + 0 : Fold all levels (namespace , class , method , block)
ctrl + k + 1 : namspace
ctrl + k + 2 : class
ctrl + k + 3 : methods
ctrl + k + 4 : blocks
ctrl + k + [ or ] : current cursor block
ctrl + k + j : UnFold
The default shortcut for collapse/extend are:
Ctrl + Shift + [ : "Fold"
Ctrl + Shift + Alt + [ : "Fold all"
Ctrl + Shift + ] : "Unfold"
Ctrl + Shift + Alt + ] : "Unfold all"
Or go to keybindings.json and change as you wish.
For example:
{
"key": "cmd+k cmd+m",
"command": "editor.foldAll",
"when": "editorFocus"
},
{
"key": "cmd+m cmd+k",
"command": "editor.unfoldAll",
"when": "editorFocus"
},
If none of the shortcuts are working (like for me), as a workaround you can also open the command palette (Ctrl + 3 or View -> Command Palette...) and type in fold all:
Collapsing is now supported in release 1.0:
Source Code Folding Shortcuts
There are new folding actions to collapse source code regions based on
their folding level.
There are actions to fold level 1 (Ctrl+K Ctrl+1) to level 5 (Ctrl+K
Ctrl+5). To unfold, use Unfold All (Ctrl+Shift+Alt+]).
The level folding actions do not apply to region containing the
current cursor.
I had a problem finding the ] button on my keyboard (Norwegian layout), and in my case it was the Å button. (Or two buttons left and one down starting from the backspace button.)
With JavaScript:
//#region REGION_NAME
...code here
//#endregion
This is the latest built-in(default) keyboard shortcuts for folding and unfolding the code
vscode Keyboard shortcut
Ctrl+Shift+[ Fold (collapse) region
Ctrl+Shift+] Unfold (uncollapse) region
Ctrl+K Ctrl+[ Fold (collapse) all subregions
Ctrl+K Ctrl+] Unfold (uncollapse) all subregions
Ctrl+K Ctrl+0 Fold (collapse) all regions
Ctrl+K Ctrl+J Unfold (uncollapse) all
Nb: But in some cases, your vs code extension or user will alter the keyboard binding(shortcut). So best option that Checks like this
view->command palette OR cntrl+shift+p
type "fold" .It will suggest the fold and unfold and there shortcut. You can type that shortcut instead of command-palette
eg:
Fold All
UnFold All
Just press ctrl + shift + p, and then type 'fold'.
all keybinds about (un)fold will be shown.
If ctrl k does not work, probably it's because the vim extension overrides the key.
in this case, you should modify settings.json (press ctrl + shift + p, and then type 'settings' ) with
"vim.handleKeys": {
"<C-k>": false,
},
This feature is now supported, since Visual Studio Code 1.17. To fold/collapse your code block, just add the region tags, such as //#region my block name and //#endregion if coding in TypeScript/JavaScript.
Example:
v1.42 is adding some nice refinements to how folds look and function. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#folded-range-highlighting:
Folded Range Highlighting
Folded ranges now are easier to discover thanks to a background color
for all folded ranges.
Fold highlight color Theme: Dark+
The feature is controled by the setting editor.foldingHighlight and
the color can be customized with the color editor.foldBackground.
"workbench.colorCustomizations": {
"editor.foldBackground": "#355000" }
Folding Refinements
Shift + Click on the folding indicator first only folds the inner
ranges. Shift + Click again (when all inner ranges are already folded)
will also fold the parent. Shift + Click again unfolds all.
When using the Fold command (kb(editor.fold))] on an already folded
range, the next unfolded parent range will be folded.
No technical tips here, just simple adjustments of the preferences of VsCode.
I managed to show code folding controls always in VsCode by going to Preferences and searching for 'folding'. Now just select to always show these controls. This works with the Typescript code and HTML of templates in the Angular 8 solution I tested it with.
This was tested with VsCode Insiders 1.37.0 running on a Windows 10 OS.
Here is the most common useful default keymap of VSCode. And you cab easily customize them with your own keymap. CTRL + K and then:
Fold All: CTRL + 0
Unfold All: CTRL + J
Fold Region: CTRL + [
Unfold Region: CTRL + ]
Fold Level 1: CTRL+ 1
Fold Level 2: CTRL+ 2
Fold Level 3: CTRL+ 3
Fold Level 1: CTRL+ 4
As of version 1.3.1 (2016-07-17), Block Collapse is much more convenient.
Any line followed by an indented line will have a '-' character to allow collapse. If the block is collapsed, it will then be replaced by a '+' character that will open the collapsed block.
The (Ctrl + Shift + Alt + ]) will still affect all blocks, closing one level. Each repeated use closed one more level. The (Ctrl + Shift + Alt + [) works in the opposite way.
Hooray, block collapse finally works usefully.
to fold/unfold current block use (ctrl+k)+(ctrl+l)
VSCode extension: Fold Level, one key fold to the level you want.
Note: these shortcuts only work as expected if you edit your keybindings.json
I wasn't happy with the default shortcuts, I wanted them to work as follow:
Fold: Ctrl + Alt + ]
Fold recursively: Ctrl + ⇧ Shift + Alt + ]
Fold all: Ctrl + k then Ctrl + ]
Unfold: Ctrl + Alt + [
Unfold recursively: Ctrl + ⇧ Shift + Alt + [
Unfold all: Ctrl + k then Ctrl + [
To set it up:
Open Preferences: Open Keyboard Shortcuts (JSON) (Ctrl + ⇧ Shift + p)
Add the following snippet to that file
Already have custom keybindings for fold/unfold? Then you'd need to replace them.
{
"key": "ctrl+alt+]",
"command": "editor.fold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+alt+[",
"command": "editor.unfold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+]",
"command": "editor.foldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+[",
"command": "editor.unfoldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+[",
"command": "editor.unfoldAll",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+]",
"command": "editor.foldAll",
"when": "editorTextFocus && foldingEnabled"
},
On a Mac, it's the RHS Command key, ⌘K, not the left for the code folding commands.
Otherwise the left hand Command key will delete the current line, ⌘K.
I wish Visual Studio Code could handle:
#region Function Write-Log
Function Write-Log {
...
}
#endregion Function Write-Log
Right now Visual Studio Code just ignores it and will not collapse it.
Meanwhile Notepad++ and PowerGUI handle this just fine.
Update: I just noticed an update for Visual Studio Code. This is now supported!
Or, if you want to remove the folding buttons, for extra space:
"editor.folding": false
(add to your settings.json file)
The command K + command 0 does not work.
More info here: region extensionhttps://marketplace.visualstudio.com/items?itemName=maptz.regionfolder
After installing the extension and using python, it works like this:
# region ARBITRARY_REGION_NAME
code goes here...
# endregion
Also, selecting the desired area, use Ctrl+M+Ctrl+R.(that is: first hit and hold Ctrl, then hit m, let go m, then hit r, and let go all)

mapping the shift key in vim

I'm having an issue when mapping the Shift key in VIM. I want Ctrl+L to behave differently than Ctrl+Shift+L
so I have this
" for insert mode remap <c-l> to:
" Insert a hash rocket for ruby
" Insert a -> for php
" for coffee the shift key decides
function! SmartHash(...)
let shift = a:0 > 0
let ruby = &ft == 'ruby'
let php = &ft == 'php'
let coffee = &ft == 'coffee'
if php
return "\->"
end
if coffee
return shift ? "\ =>\n" : "\ ->\n"
end
if ruby
return "\ => "
end
return ""
endfunction
imap <c-l> <c-r>=SmartHash()<cr>
imap <C-S-L> <c-r>=SmartHash(1)<cr>
...but it's just triggering the second mapping whether I have the Shift key pressed or not.
Updated based on accepted answer
" for insert mode remap <c-l> to:
" Insert a hash rocket for ruby
" Insert a -> for php and coffeescript
" double tapping does alternate symbol for php and coffescript
function! SmartHash(...)
let alt = a:0 > 0
let ruby = &ft == 'ruby'
let php = &ft == 'php'
let coffee = &ft == 'coffee'
if php || coffee
return alt ? "\ =>\n" : "\ ->\n"
end
if ruby
return "\ => "
end
return ""
endfunction
imap <c-l> <c-r>=SmartHash()<cr>
imap <c-l><c-l> <c-r>=SmartHash(1)<cr>
Due to the way that the keyboard input is handled internally, this unfortunately isn't possible today, even in GVIM. This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
I usually work around this by starting my alternate mapping with g, e.g. <C-l> and g<C-l>, but that won't work in insert mode. In your case, with a little bit of additional logic, you could make a second <C-l> at the same position change the previously inserted -> to =>, so that you get the first alternative via <C-l>, and the second via <C-l><C-l>.

How do I send an arrow key in Perl using the Net::Telnet module?

Using the Perl module Net::Telnet, how do you send an arrow key to a telnet session so that it would be the same thing as a user pressing the down key on the keyboard?
use Net::Telnet;
my $t = new Net::Telnet();
my $down_key=?; #How do you send a down key in a telnet session?
t->print($down_key);
This list of VT102 codes says that cursor keycodes are the following:
Up: Esc [ A
033 133 101
Down: Esc [ B
033 133 102
Right: Esc [ C
033 133 103
Left: Esc [ D
033 133 104
How would I send these in telnet? Are these codes the same as an arrow key pressed at the keyboard?
Try printing "\e[B". These codes are indeed the same - try running the Bourne shell sh without readline support and hit the up/down arrows, you'll see something like ^[[A where ^[ represents the escape character.
Some programs expect SS3 escapes, rather than CSI. If "\e[A" and friend don't work, try:
%ss3 = (
up => "\eOA",
down => "\eOB",
right => "\eOC",
left => "\eOD",
);
(those are upper case letter o's, not zeros)