162

I switch back and forth between two computers constantly:

  • OSX Snow Leopard
  • Ubuntu 10.10

I'd like to be able to make Ubuntu use the same keyboard shortcuts as OSX System Wide, for the following keyboard sequences only (ie I don't want to make Ctrl act like Super)

  • Super+C -> Copy

  • Super+V -> Paste

  • Super+T -> Open a new tab in whatever browser I'm in.

  • Super+W -> Close a tab in whatever browser I'm in.

So in short, is there anyway for me to map just these keyboard sequences to the following, system wide ?

  • Super+C -> Ctrl+C
  • Super+V -> Ctrl+V
  • Super+T -> Ctrl+T
  • Super+W -> Ctrl+W

I know there are ways for me to do this for vim, and Firefox, and I'm sure specifically for most applications... but I would prefer to have to do this just once and have it work that way system wide!

I'm using a standard PC keyboard, that is "Generic 105 key (intl) PC" on Ubuntu. I'm also using the same keyboard on my Mac mini.

Braiam
  • 69,112
brad parks
  • 2,507

16 Answers16

52

If you install the xautomation package, you can add a command in System → PreferencesKeyboard ShortcutsCustom like:

xte "keyup Meta_L" "keyup Meta_R" "keyup c" "keydown Control_L" "key c" "keyup Control_L"

and map that to Meta+C.

You may need to use "keyup Super_L" "keyup Super_R" depending on your keyboard.


Alternative Solution:

Install AutoKey (apt-get install autokey-gtk) and set a phrase to:

Phrase Text: <ctrl>+C (actually type out the <ctrl>+ here)
Paste Using: Keyboard
Abbreviation: None
Hotkey: <super>+v
Window Filter: None
cscarney
  • 19,443
50

Since Ubuntu 14 you can use GNOME Tweaks:

sudo apt install gnome-tweaks # formerly gnome-tweak-tool

Then, look for Tweak Tool > Keyboard & Mouse > Additional Layout Options > Alt/Win key behavior.

And check: Ctrl is mapped to Win keys and usual Ctrl keys.

screenshot

Glorfindel
  • 975
  • 3
  • 15
  • 21
caiolopes
  • 601
18

Some of the above solutions are old and not working with Ubuntu 18.04. Here is an updated answer

  1. Install gnome-tweaks if not installed in the system

    sudo apt-get install gnome-tweaks

  2. Open the list of applications by pressing Show Applications on at the left bottom.

  3. Note that this will only change the behavior of WINDOWS key to CMD key and keeps CTRL key as same as before (you will have 2 CTRL keys). If you don't like this, skip step 4 and go to step 5

  4. Look for Tweaks application --> Keyboard and Mouse --> Additional Layout options --> Alt/Win behavior --> Select Control is mapped to Win keys and the usual Ctrl Keys.

  5. Skip this step if you completed step 4. This step will swap Windows key Behaviour to Ctrl and vice versa. Look for Tweaks application --> Keyboard and Mouse --> Additional Layout options --> Ctrl Position --> Select Swap Left Win with Left Ctrl

  6. One more thing I miss from macOS is the ability to switch windows with CMD+Tab (In this case Windows+Tab). To achieve this go to Settings --> Keyboard --> Look for Switch Windows under navigation section --> Double Click it and press Windows+Tab when pop up appears

18

There is a way to map Ctrl to Win/Super key:

  1. go to system->preferences->keyboard,

  2. open tab Layouts,

  3. click Options...,

  4. open Alt/Win key behavior,

  5. and select Control is mapped to Win keys

And for console there is a forum post how to do that here.

For more digging check general Linux keyboard HOWTO here.

regards

David Foerster
  • 36,890
  • 56
  • 97
  • 151
danizmax
  • 2,780
12

Bumping this! Your pinkies will thank me

All of my muscle memory is Mac based, and I use a Mac at work and in my personal life, but I happened across a Dell Latitude E7470 and figured I might as well give Linux a go on it (since my Macbook Pro is from 2012 and is pretty heavy, and I just didn't want to have to bother with \r\n line endings). So it's been a slow-moving work in progress to figure out how to modify the system to match my muscle memory instead of relearning keyboard shortcuts.

At first, I was just using the Keyboard > Layouts > Options... settings for "Ctrl is mapped to Alt; Alt is mapped to Win" but, and this is a very minor thing, I didn't like that my settings didn't apply until after I got past the greeter. I was also having trouble getting ~/.Xmodmap to run on start (either by itself, as part of .xinitrc and as a custom startup command in startup applications and in /etc/default/

I'm currently running Ubuntu MATE 19.10 (Eoan), and the keyboard is recognized as a pc105

You'll have to modify the script below (or the individual files, if you've already made other modifications to them that you want to keep) to match whatever keyboard your computer has.

Make ~/.Xmodmap:

echo '! -*- coding: utf-8 -*-
! keycode  37 = Control_L NoSymbol Control_L
! keycode  64 = Alt_L Meta_L Alt_L Meta_L
! keycode 105 = Control_R NoSymbol Control_R
! keycode 108 = Alt_R Meta_R Alt_R Meta_R
! keycode 133 = Super_L NoSymbol Super_L
! keycode 134 = Super_R NoSymbol Super_R
! keycode 135 = Menu NoSymbol Menu
! keycode 147 = XF86MenuKB NoSymbol XF86MenuKB
! keycode 204 = NoSymbol Alt_L NoSymbol Alt_L
! keycode 206 = NoSymbol Super_L NoSymbol Super_L
!
! keycode 105 = Alt_R Meta_R
clear control
clear mod1
clear mod4
keycode 37 = Super_L
keycode 105 = Super_R
keycode 133 = Alt_L Meta_L
keycode 64 = Control_L
keycode 108 = Control_R
add control = Control_L Control_R
add mod1 = Alt_L Meta_L
add mod4 = Super_L Super_R' > ~/.Xmodmap

make ~/.xinitrc:

echo 'if [ -s ~/.Xmodmap ]; then
    xmodmap ~/.Xmodmap
fi
xmodmap ~/.Xmodmap' > ~/.xinitrc

Modify X11 Keyboard symbol file for your keyboard (the important part here is the include "altwin(ctrl_alt_win)" before // End of modifier mappings. This is an otherwise unmodified /usr/share/X11/xkb/symbols/pc file):

echo 'default  partial alphanumeric_keys modifier_keys
xkb_symbols "pc105" {

    key <ESC>  {    [ Escape        ]   };

    // The extra key on many European keyboards:
    key <LSGT> {    [ less, greater, bar, brokenbar ] };

    // The following keys are common to all layouts.
    key <BKSL> {    [ backslash,    bar ]   };
    key <SPCE> {    [    space      ]   };

    include "srvr_ctrl(fkey2vt)"
    include "pc(editing)"
    include "keypad(x11)"

    key <BKSP> {    [ BackSpace, BackSpace  ]   };

    key  <TAB> {    [ Tab,  ISO_Left_Tab    ]   };
    key <RTRN> {    [ Return        ]   };

    key <CAPS> {    [ Caps_Lock     ]   };
    key <NMLK> {    [ Num_Lock      ]   };

    key <LFSH> {    [ Shift_L       ]   };
    key <LCTL> {    [ Control_L     ]   };
    key <LWIN> {    [ Super_L       ]   };

    key <RTSH> {    [ Shift_R       ]   };
    key <RCTL> {    [ Control_R     ]   };
    key <RWIN> {    [ Super_R       ]   };
    key <MENU> {    [ Menu          ]   };

    // Beginning of modifier mappings.
    modifier_map Shift  { Shift_L, Shift_R };
    modifier_map Lock   { Caps_Lock };
    modifier_map Control{ Control_L, Control_R };
    modifier_map Mod2   { Num_Lock };
    modifier_map Mod4   { Super_L, Super_R };

    // Fake keys for virtual<->real modifiers mapping:
    key <LVL3> {    [ ISO_Level3_Shift  ]   };
    key <MDSW> {    [ Mode_switch       ]   };
    modifier_map Mod5   { <LVL3>, <MDSW> };

    key <ALT>  {    [ NoSymbol, Alt_L   ]   };
    include "altwin(meta_alt)"

    key <META> {    [ NoSymbol, Meta_L  ]   };
    modifier_map Mod1   { <META> };

    key <SUPR> {    [ NoSymbol, Super_L ]   };
    modifier_map Mod4   { <SUPR> };

    key <HYPR> {    [ NoSymbol, Hyper_L ]   };
    modifier_map Mod4   { <HYPR> };
    include "altwin(ctrl_alt_win)"
    // End of modifier mappings.

    key <OUTP> { [ XF86Display ] };
    key <KITG> { [ XF86KbdLightOnOff ] };
    key <KIDN> { [ XF86KbdBrightnessDown ] };
    key <KIUP> { [ XF86KbdBrightnessUp ] };
};

hidden partial alphanumeric_keys
xkb_symbols "editing" {
    key <PRSC> {
    type= "PC_ALT_LEVEL2",
    symbols[Group1]= [ Print, Sys_Req ]
    };
    key <SCLK> {    [  Scroll_Lock      ]   };
    key <PAUS> {
    type= "PC_CONTROL_LEVEL2",
    symbols[Group1]= [ Pause, Break ]
    };
    key  <INS> {    [  Insert       ]   };
    key <HOME> {    [  Home         ]   };
    key <PGUP> {    [  Prior        ]   };
    key <DELE> {    [  Delete       ]   };
    key  <END> {    [  End          ]   };
    key <PGDN> {    [  Next         ]   };

    key   <UP> {    [  Up           ]   };
    key <LEFT> {    [  Left         ]   };
    key <DOWN> {    [  Down         ]   };
    key <RGHT> {    [  Right        ]   };
};' >/usr/share/x11/xkb/symbols/pc

And just in case, here's the relevant part of /usr/share/X11/xkb/symbols/altwin if you need to add it manually:

// Ctrl is mapped to the Alt, Alt to the Super, and Win to the Ctrl keys.
partial modifier_keys
xkb_symbols "ctrl_alt_win" {
    key <LALT> { [ Control_L, Control_L  ] };
    key <RALT> { type[Group1] = "TWO_LEVEL",
                 symbols[Group1] = [ Control_R, Control_R ] };
    key <LWIN> { [ Alt_L, Meta_L ] };
    key <RWIN> { [ Alt_R, Meta_R ] };
    key <LCTL> { [ Super_L ] };
    key <RCTL> { [ Super_R ] };
    modifier_map Control { <RALT>, <LALT> };
    modifier_map Mod1 { <LWIN>, <RWIN> };
    modifier_map Mod4 { <LCTL>, <RCTL> };
};

Modify /etc/defaults/keyboard:

echo '# KEYBOARD CONFIGURATION FILE

# Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS="caps:shift_nocancel,ctrl:alt,alt:win,win:ctrl"

BACKSPACE="guess"' >/etc/default/keyboard

And finally, here's my GNOME and Marco keybinding dconf dumps:

GNOME:

[org/gnome/desktop/wm/keybindings]
activate-window-menu=['disabled']
begin-move=['disabled']
close=['<Control>q']
maximize=['<Alt><Super>m']
minimize=['<Control>m']
move-to-corner-ne=['disabled']
move-to-corner-nw=['disabled']
move-to-corner-se=['disabled']
move-to-corner-sw=['disabled']
move-to-side-e=['disabled']
move-to-side-n=['disabled']
move-to-side-s=['disabled']
move-to-side-w=['disabled']
move-to-workspace-1=['<Shift><Super>Home']
move-to-workspace-down=['<Shift><Super>Next']
move-to-workspace-up=['<Shift><Super>Prior']
show-desktop=['<Control>F3']
switch-to-workspace-1=['<Super>Home']
switch-to-workspace-down=['<Super>Next']
switch-to-workspace-up=['<Super>Prior']
switch-windows=['disabled']
switch-windows-backward=['disabled']
toggle-fullscreen=['<Control><Super>f']
toggle-maximized=['<Shift><Control>m']
toggle-shaded=['<Alt><Super>s']
unmaximize=['<Control><Alt>m']

Marco:

[org/mate/marco/global-keybindings]
cycle-group='<Primary>grave'
cycle-panels='disabled'
cycle-windows='<Alt>grave'
run-command-screenshot='<Primary><Shift>numbersign'
run-command-window-screenshot='<Primary><Shift>dollar'
show-desktop='<Primary>F3'
switch-group='<Alt>Tab'
switch-to-workspace-1='<Primary><Mod4>1'
switch-to-workspace-2='<Primary><Mod4>3'
switch-to-workspace-3='<Primary><Mod4>2'
switch-to-workspace-4='<Primary><Mod4>4'
switch-to-workspace-down='<Mod4>Down'
switch-to-workspace-left='<Mod4>Left'
switch-to-workspace-prev='<Shift><Mod4>Tab'
switch-to-workspace-right='<Mod4>Right'
switch-to-workspace-up='<Mod4>Up'
switch-windows='<Primary>Tab'

[org/mate/marco/window-keybindings]
activate-window-menu='disabled'
begin-move='<Primary><Shift><Mod4>m'
begin-resize='<Primary><Shift><Mod4>r'
close='<Primary>q'
maximize='<Alt><Mod4>m'
maximize-horizontally='<Alt><Mod4>h'
maximize-vertically='<Alt><Mod4>v'
minimize='<Primary>m'
move-to-corner-ne='<Alt><Mod4>2'
move-to-corner-nw='<Alt><Mod4>1'
move-to-corner-se='<Alt><Mod4>4'
move-to-corner-sw='<Alt><Mod4>3'
move-to-side-e='<Alt><Mod4>Left'
move-to-side-n='<Alt><Mod4>Up'
move-to-side-s='<Alt><Mod4>Down'
move-to-side-w='<Alt><Mod4>Right'
move-to-workspace-1='<Primary><Alt><Mod4>1'
move-to-workspace-2='<Primary><Alt><Mod4>2'
move-to-workspace-3='<Primary><Alt><Mod4>3'
move-to-workspace-4='<Primary><Alt><Mod4>4'
move-to-workspace-down='<Primary><Mod4>Down'
move-to-workspace-left='<Primary><Mod4>Left'
move-to-workspace-right='<Primary><Mod4>Right'
move-to-workspace-up='<Primary><Mod4>Up'
tile-to-corner-ne='<Shift><Alt>at'
tile-to-corner-nw='<Shift><Alt>exclam'
tile-to-corner-se='<Shift><Alt>dollar'
tile-to-corner-sw='<Shift><Alt>numbersign'
tile-to-side-e='<Shift><Alt>Right'
tile-to-side-w='<Shift><Alt>Left'
toggle-fullscreen='<Primary><Mod4>f'
toggle-maximized='<Primary><Shift>m'
toggle-shaded='<Alt><Mod4>s'
unmaximize='<Alt><Mod4>r'
Alex Torma
  • 121
  • 1
  • 2
10

Edit: The simple approach I describe here has some drawbacks (mentioned below, most noticeably with sticky keys). So I now recommend checking out Kinto which avoids such issues by remapping the keyboard proactively.


I wanted a solution that would work in any desktop environment / window manager, so I combined cscarney's suggestion to use xautomation with the SXHKD (the simple X hotkey daemon).

Here is the basic ~/.config/sxhkd/sxhkdrc config file I produced.

Advanced: My latest version is more complete, and can send different keystrokes for different applications, but it requires an external script, and probably has some bindings you don't want.

Here is a snippet:

# Reload this config
control + alt + shift + r
  killall -USR1 -u "$USER" sxhkd && echo 'sxhkd config reloaded'

Cut, copy and paste

alt + x xte "keyup x" "keyup Meta_L" "keydown Control_L" "key x" "keyup Control_L" "keydown Meta_L" alt + c xte "keyup c" "keyup Meta_L" "keydown Control_L" "key c" "keyup Control_L" "keydown Meta_L"
alt + v xte "keyup v" "keyup Meta_L" "keydown Control_L" "key v" "keyup Control_L" "keydown Meta_L"

Open tab, close tab, restore closed tab

alt + t xte "keyup t" "keyup Meta_L" "keydown Control_L" "key t" "keyup Control_L" "keydown Meta_L" alt + w xte "keyup w" "keyup Meta_L" "keydown Control_L" "key w" "keyup Control_L" "keydown Meta_L"
alt + shift + t xte "keyup t" "keyup Meta_L" "keydown Control_L" "key t" "keyup Control_L" "keydown Meta_L"

Move to home/end

alt + Left xte "keyup Meta_L" "keyup Left" "key Home" "keydown Meta_L" alt + Right xte "keyup Meta_L" "keyup Right" "key End" "keydown Meta_L" alt + shift + Left xte "keyup Meta_L" "keyup Left" "key Home" "keydown Meta_L" alt + shift + Right xte "keyup Meta_L" "keyup Right" "key End" "keydown Meta_L"

Move one word

super + Left xte "keyup Super_L" "keyup Left" "keydown Control_L" "key Left" "keyup Control_L" "keydown Super_L" super + Right xte "keyup Super_L" "keyup Right" "keydown Control_L" "key Right" "keyup Control_L" "keydown Super_L" super + shift + Left xte "keyup Super_L" "keyup Left" "keydown Control_L" "key Left" "keyup Control_L" "keydown Super_L" super + shift + Right xte "keyup Super_L" "keyup Right" "keydown Control_L" "key Right" "keyup Control_L" "keydown Super_L"

Note:

  • As you can see, if I keyup the original modifier key before performing the virtual stroke, then I keydown it again afterwards. This means the user can repeat they key press without having to release and depress the modifier key again. In other words, after pressing Alt-W, the system sees the Alt key is depressed.

Caveats:

  • That can be a problem (especially on slower machines) if you release Alt key while the xte command is still running. You will end up with a "sticky" Alt key, because xte has pressed it down for you!

  • Using this approach, the triggers will not repeat if the key is held down. (So you can't move 12 words by holding down Super-Left.)

joeytwiddle
  • 2,009
8

To expand on cscarney's AutoKey hint, I am developing my AutoKey configuration to achieve all this in Ubuntu (OS X-like <cmd>+c, <cmd>+v, ... Emacs-style <ctrl>+f, <ctrl>+b, ...) in my dotfile repo.

I don't use basic phrases (where window filtering is currently broken), but Python scripts, to be able to disable AutoKey in apps such as Emacs, Eclipse, Vim, gnome-terminal ... where I don't want to mess up the existing shortcuts.

You can check out the README on the bottom for instructions on how to set it up.

metakermit
  • 2,650
4

You may like to check out Kinto, which is a fairly comprehensive solution with a full GitHub repository.

"A better Linux & Windows keyboard layout for professional mac users"

It avoids some of the problems I had with sxhkd, because it doesn't reinterpret keystrokes, rather it remaps them ahead of time using xmodmap.

Specifically, it watches your X session to see when window focus changes, and then it quickly switches to the appropriate keybindings for that application.

joeytwiddle
  • 2,009
4

This is an easy task once you know how to do it.

  1. Check the keycode of yours key. Run this program at terminal.

    xev</pre>  
    

    At this example, the terminal shows that the keycode for my k is "45".

  2. Change them as you like creating this file:

    gedit ~/.Xmodmap</pre>  
    
  3. It's contents should look like this example:

    keycode 64 = Alt_L Meta_L Alt_L Meta_L  
    keycode 108 = ISO_Level3_Shift NoSymbol ISO_Level3_Shift  
    

    (Change the keycode number as needed - look at step "1")

    Ilustration:

    Obs.: if I want to change my k I should use "keycode 45" as showed at step "1".

  4. Logout and log back in or reboot or run this:

    xmodmap ~/.Xmodmap</pre>  
    

    Hope you enjoy ;-)

Pablo Bianchi
  • 17,371
desgua
  • 33,215
3

Similar to and based on the excellent work of others, I made a repo with my Autokey shortcuts and instructions for getting this setup:

https://github.com/bliles/autokey-macos

bliles
  • 31
  • 1
3

Use kinto: https://github.com/rbreaves/kinto/

I'm on Ubuntu 20.04 and it works wonderfully! It uses system wide rebindings so you don't need to deal with configuring individual applications and installation is super simple and takes a few seconds.

Keyan P
  • 131
3

The easiest way which I found to remap the keys with mac layout was by installing the app Kinto. After the installation, you can use mac os keyboard shortcuts with the command key on Linux/Windows

https://github.com/rbreaves/kinto

Installation:

git clone https://github.com/rbreaves/kinto.git
cd kinto
./setup.py

https://youtu.be/kd4al45XD1g

enter image description here

Nabin
  • 337
2

First
Install Guest Additions CD (Devices->Insert Guest Additions CD image).

Activate clipboard sharing
Then after a reboot, in Virtualbox go to Machine->Settings->General->Advanced and set
Shared Clipboard: Bidrectional

Change VirtualBox Host Key
In Vitualbox/Preferences/Input/Virtual Machine/Host Key Combination
choose sth different then

Key Mapping:
For the cmd/ctrl mapping for me adding the English (Macintosh) keyboard seems to be the most comfortable solution :)

Go to Ubuntu Settings->Text Entry
(I guess in older versions it might be in Keyboard Layout Settings)
Under Input sources to use: hit plus (+)
add English (Macintosh)

I'm on Ubuntu 14.04.3 LTS 64bit in VirtualBox 5.0.4

Allisone
  • 121
1

I have an Apple aluminum keyboard. A slight modification (using Super_L instead of Meta_L) to @cscarney's suggestion made it work in Ubuntu 18.04

Command: xte "keyup Super_L" "keyup c" "keydown Control_L" "key c" "keyup Control_L"
Short cut: Super+C

Command: xte "keyup Super_L" "keyup v" "keydown Control_L" "key v" "keyup Control_L"
Short cut: Super+V
mpprdev
  • 111
  • 2
0

I don't have Ubuntu to hand (Mac's here, Ubu's at the office) but I use a Mac keyboard there and for the same reasons as you, wanted to do this.

First thing is to make sure your keyboard was installed as a Mac keyboard. I think you can change this in system->preferences->keyboard. While you're there one of the tabs will get you to a key mapping page where you can set up how the cmd key works.

Like I say, I can't remember exactly - I can point to the bits on my screen here, but that doessn't work because I'm not there.

If you're still stuck tomorrow, I'll look again.

Leo
  • 121
0

Autokeys did work for me. Now win+c (v,x,a) work around my system just like I'd expect it.

1. Install autokeys

sudo apt install autokey-gtk

2. Configure autokeys

  1. Add root folder, call it something like MacKeys or whatever
  2. You'll create 4 scripts in there for ctrl-c, v,x and a
  3. assign to each script hotkey super-c, super-v etc
  4. fill the scripts with code below (replace letter c for other combos)

winClass = window.get_active_class() keys = "<ctrl>+c" if winClass == "gnome-terminal-server.Gnome-terminal": keys = "<ctrl>+<shift>+c"; keyboard.send_keys(keys)

4a. For CmdZ, CmdA or CmdX work with simpler script like keyboard.send_keys("<ctrl>+x")

3. Add /usr/bin/autokey-gtk to startup applications

4. Disable win-A in keyboard settings

Normally win-a makes switch to overview.

5. Optional, fully prevent win to switch the overview

Probably you will want to remove it. Install gnome-tweak-tool, go to keyboard and rebind switching from desktop to overview. That worked for my gnome ubuntu 16.04.

Lauri
  • 446