You can usually lock the screen by hitting ctrl-shift-power, but that will power down external monitors, which might take several boring seconds to start up again. Also, a MacBook with a TouchBar doesn't have the power button anymore. With the following guide you can lock the screen by starting the screensaver, keeping the monitors running.
Set System Preferences » Security & Privacy » General » ✅ Require password immediately
after sleep...
-
Download enable-screensaver.zip and unzip it to
$HOME/Library/Services/
(If you use OSX Sierra or older, use enable-screensaver-10.12.zip instead)
-
Go to System Preferences » Keyboard » Shortcuts » Services » General » enable-screensaver.
-
Choose a hotkey for it (I use ctrl-alt-cmd-k). You're good to go!
-
Start Automator, choose File » New and Service
-
Select that the Service receives
no input
inany application
-
Double click Library » Utilities » Run Shell Script
-
In the shell text field, add
/System/Library/CoreServices/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine > /dev/null 2>&1 &
(If you use OSX Sierra or older, use
/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine
instead) -
Save the Service as
enable-screensaver
-
Perform steps 2. and 3. above