Saturday, June 1, 2024

MacOS Setup Cheat Sheet II

In the previous post we checked out MacOS Setup Cheat Sheet to seamlessly transfer all keyboard shortcuts from Windows to Mac. Now we would like to transfer all Application navigation as M1 powered MacBooks are more appealing for companies than ever. Especially with the rise of A.I. requirements for faster data access.

Let's check it out!

Navigation
In the previous post, we began looking at using keyboard shortcuts on Mac. Now we would like to compare document shortcuts that we are used to for navigation on Windows however now using Ctrl instead of Cmd:
 Home  Move to beg of line  Ctrl + Home  Move to beg of file
 End  Move to end of line  Ctrl + End  Move to end of file
 Ctrl + Left  Move to prev word  Ctrl + Shift + Left  Select the prev word
 Ctrl + Right  Move to next word  Ctrl + Shift + Right  Select the next word
 Shift + Home  Select to beg of line  Ctrl + Shift + Home  Select to beg of file
 Shift + End  Select to end of line  Ctrl + Shift + End  Select to end of file
The main issue is there doesn't seem to be generic shortcut mapping; navigation seems application specific!

TextEdit
The Mac text system uses generalized key bindings mechanism that is completely re-mappable by all users. The standard bindings include all system defaults but you can override bindings in your own custom dict file:
 SYSTEM /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict
 CUSTOM ~/Library/KeyBindings/DefaultKeyBinding.dict

Here is how to override MacOS key bindings: Launch Terminal and create new a DefaultKeyBinding.dict file:
 sudo bash
 mkdir -p ~/Library/KeyBindings
 touch ~/Library/KeyBindings/DefaultKeyBinding.dict
 open -e ~/Library/KeyBindings/DefaultKeyBinding.dict

Typically, the default key bindings for the Home and End keys in MacOS are different to any other operating system. Thus you may like to fix the Home and End buttons first and in combination with Shift and Ctrl keys.

Next key bindings that may not be working is to move cursor from word to word. Integrate Keycode Modifier learnt from the previous post to complete custom dict file to change all MasOS key bindings. Here is my file:
 {   
  "\UF729" =  "moveToBeginningOfLine:" /* Home Button*/
  "\UF72B" =  "moveToEndOfLine:" /* End Button */
  "$\UF729" =  "moveToBeginningOfLineAndModifySelection:" /* Shift+ Home Button */
  "$\UF72B" =  "moveToEndOfLineAndModifySelection:" /* Shift+ End Button */
  "@\UF729" =  "moveToBeginningOfDocument:" /* Ctrl + Home Button */
  "^\UF729" =  "moveToBeginningOfDocument:" /* Alt + Home Button */
  "~\UF729" =  "moveToBeginningOfDocument:" /* Win + Home Button */
  "@\UF72B" =  "moveToEndOfDocument:" /* Ctrl + End Button */
  "^\UF72B" =  "moveToEndOfDocument:" /* Alt + End Button */
  "~\UF72B" =  "moveToEndOfDocument:" /* Win + End Button */
  "@$\UF729" =  "moveToBeginningOfDocumentAndModifySelection:" /* Ctrl + Shift + Home Button */
  "^$\UF729" =  "moveToBeginningOfDocumentAndModifySelection:" /* Alt + Shift + Home Button */
  "~$\UF729" =  "moveToBeginningOfDocumentAndModifySelection:" /* Win + Shift + Home Button */
  "@$\UF72B" =  "moveToEndOfDocumentAndModifySelection:" /* Ctrl + Shift + End Button */
  "^$\UF72B" =  "moveToEndOfDocumentAndModifySelection:" /* Alt + Shift + End Button */
  "~$\UF72B" =  "moveToEndOfDocumentAndModifySelection:" /* Win + Shift + End Button */
  "@\UF702" =  "moveWordLeft:" /* Ctrl + Left */
  "^\UF702" =  "moveWordLeft:" /* Alt + Left */
  "~\UF702" =  "moveWordLeft:" /* Win + Left */
  "@\UF703" =  "moveWordRight:" /* Ctrl + Right */
  "^\UF703" =  "moveWordRight:" /* Alt + Right */
  "~\UF703" =  "moveWordRight:" /* Win + Right */
  "@$\UF702" =  "moveWordLeftAndModifySelection:" /* Ctrl + Shift + Left */
  "^$\UF702" =  "moveWordLeftAndModifySelection:" /* Alt + Shift + Left */
  "~$\UF702" =  "moveWordLeftAndModifySelection:" /* Win + Shift + Left */
  "@$\UF703" =  "moveWordRightAndModifySelection:" /* Ctrl + Shift + Right */
  "^$\UF703" =  "moveWordRightAndModifySelection:" /* Alt + Shift + Right */
  "~$\UF703" =  "moveWordRightAndModifySelection:" /* Win + Shift + Right */
 }   
For completeness, here is a thorough KeyBindings tutorial explaining MacOS Keybinding Key Syntax and the Action Code. Detailed DefaultKeyBinding.dict examples shared online by users Belphemu, trusktr and zsimic.

Finally, in researching Default Key Bindings many posts promote Karabiner-Elements as a powerful keyboard customizer for macOS. However, it has been recommended not to use on M1 hardware due to kernel panics.

Terminal
Ideally we would like to update navigation on Terminal due to frequent use: Terminal | Settings... | Profiles. Keyboard tab customize Key and Action settings. Check Use Option as Meta Key to "backward delete word". Unfortunately, Terminal navigation not aligned therefore will prefer IDEs like VS Code or PyCharm terminals.

VS Code
Visual Studio Code is a cross-platform code editor available on Windows, Linux and Mac. However, unlike the TextEdit, VS Code does not respect DefaultKeyBinding.dict on MacOS. Therefore, change keyboard shorcuts!

Customize VS Code keybindings to align navigation: Launch VS Code | Settings | Keyboard Shortcuts | Add:
 Command Keybinding Description
 cursorTop Ctrl + Home Move to beg of file
 cursorBottom Ctrl + End Move to end of file
 cursorWordLeft Ctrl + Left Move to prev word
 cursorWordRight Ctrl + Right Move to next word
 cursorTopSelect Ctrl + Shift + Home Select to beg of file
 cursorBottomSelect Ctrl + Shift + End Select to end of file
 cursorWordLeftSelect Ctrl + Shift + Left Select the prev word
 cursorWordRightSelect Ctrl + Shift + Right Select the next word
 deleteWordLeft Alt + Backspace Delete the prev word

Continue to customize Menu keyboard shortcuts that one is accustomed to aligning VS Code navigation e.g.:
 Command Keybinding Description
 File: Open Folder... Ctrl + K Ctrl + O workbench.action.files.openFolderViaWorkspace
 Terminal: Clear Ctrl + L workbench.action.terminal.clear
 Terminal: Create New Terminal Ctrl + Shift + T workbench.action.terminal.new

The keybindings for VS Code are updated in ~/Library/Application\ Support/Code/User/keybindings.json file. Update keybindings.json directly for Terminal keyboard shortcuts like delete word prior + cursor movement:
 {  
  "key": "alt+backspace",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "\u001b\u007f" },
  "when": "terminalFocus"
 },  
 {  
  "key": "cmd+backspace",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "\u001b\u007f" },
  "when": "terminalFocus"
 },  
 {  
  "key": "cmd+left",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "\u001bb" },
  "when": "terminalFocus"
 },  
 {  
  "key": "cmd+right",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "\u001bf" },
  "when": "terminalFocus"
 },  
 {  
  "key": "cmd+z",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "\u0003" },
  "when": "terminalFocus"
 }  

PyCharm Community
Align PyCharm Keymap to VS Code Keymap for consistent keyboard shortcuts and developer IDE experience. In the "Welcome to PyCharm" popup choose Plugins | VSCode Keymap | Install. Navigate back to Customize. Under Keymap change default value VSCode (macOS). Now most VS Code shortcuts will work like Windows.

However, in order to better align full navigation, update the following from PyCharm | Settings... | Keymap:
 Editor Actions Move Caret to Text Start Ctrl + Home
 Editor Actions Move Caret to Text End Ctrl + End
 Editor Actions Move Caret to Text Start with Selection Ctrl + Shift + Home
 Editor Actions Move Caret to Text End with Selection Ctrl + Shift + End
 Editor Actions Move Caret to Previous Word Ctrl + Left
 Editor Actions Move Caret to Next Word Ctrl + Right
 Editor Actions Move Caret to Previous Word with Selection Ctrl + Shift + Left
 Editor Actions Move Caret to Next Word with Selection Ctrl + Shift + Right
 Editor Actions Move Up and Scroll with Selection Ctrl + Shift + Up
 Editor Actions Move Down and Scroll with Selection Ctrl + Shift + Down
 Editor Actions Delete to Word Start Alt + Backspace
 Main Menu Navigate Back Alt + Up
 Main Menu Navigate Forward Alt + Down
IMPORTANT: typically use Alt + Left to Navigate Back but this is now doubled to Move Word left as priority!

NOTE
If Ctrl + Shift + O launches Symbols then remove duplicate: Choose Settings | Keymap | filter as "symbol". Double click Navigate | Go to Symbol | Edit | Remove Ctrl+Shift+O | Apply | OK. Repeat this as necessary.

Excel
Keyboard shortcuts in Excel like Ctrl+Home and Ctrl+End won't work so prefer to use arrow keys to navigate
 Ctrl + Left Move to first cell of selected row
 Ctrl + Right Move to last cell of selected row
 Ctrl + Up Move to first cell of selected column
 Ctrl + Down Move to last cell of selected column

PostgreSQL
PostgreSQL is preferred by companies because it offers data types not found in MySQL. Plus MySQL can be used for web-based/online or mapping functions, whereas PostgreSQL is recommended for large analytical processes.

Postgres
Setup a local PostgreSQL database server on Mac OS/X. Download Postgres.app PostgreSQL 16 (Universal).

pgAdmin
Download and install local pgAdmin database client on Mac OS/X to administer and PostgreSQL instances.

PyCharm Professional
After installing Postgres server one can use pgAdmin client for data access. However, it seems challenging to customize navigation here. An excellent alternative is Database Tools + SQL plugin available from PyCharm.

This plugin is available only in PyCharm Professional but may prove to be a valuable investment longterm J
 PyCharm Community  PyCharm Professional

Download and install PyCharm Professional for macOS. Launch PyCharm Professional. Import Settings from PyCharm Community Edition. Thus you should have all navigation aligned! Note the Database Tools + SQL plugin will be enabled by default. Interesting plugins PostgreSQL Debugger and SQL Optimizer + Indexing.

Connect to database: View menu | Database | Click "+" | Data Source | PostgreSQL. Enter credentials | OK.

Checkout the PyCharm documentation to Run queries. Align F5 to Execute queries similar to SQL Server Mgt Studio: Settings | Keymap | Database | Console Toolbar | Execute. Right click | Add keyboard shortcut | F5.

Checkout the PyCharm documentation to update Word Case as per SQL convention: Settings | Editor | Code Style | SQL | PostgreSQL. Inherit general SQL style DISABLE | Word Case | Keywords To upper. Also update Code Completion as Ctrl + Space: Settings | Keymap | Main Menu | Code | Code Completion | Ctrl + Space.

Finally in Settings | Editor | General | Code Completion enable the following options: Automatically insert single suggestions, Sort suggestions alphabetically, Show suggestions as you type, Match case: First letter.

Summary
To summarize, armed with this knowledge we have setup the MacOS to better align keyboard shortcuts and application navigation techniques accumulated from years of Windows development experience. From here, we are in now an excellent position to leverage the M1 chip for faster data access as per A.I. requirements!

No comments:

Post a Comment