Solution 1:

In OS X 10.9 and later:

pmset displaysleepnow

By itself, this command only causes the display to sleep, resulting in a black screen. By configuring your computer to require a password immediately after sleep, this one-liner works as a "lock computer" command. The preference is available at System Preferences > Security & Privacy > General.

Source: How to lock screen on MacBook Air?

Solution 2:

Note: This solution is unique in that it uses the Keychain Access menu bar status functionality, but it does not require you to enable the Show keychain status in menu bar option as the AppleScript methods do.

I was also looking for a solution for this. Today I just had some time to play around and found a way to programmatically actually call the functionality from the keychain menu plugin. This solution works perfectly as long as Apple doesn't change the relevant parts in the keychain menu plugin. You can create a small binary for locking your screen by pasting this into your terminal...

# Do our work in the temporary directory that gets cleaned on boot
cd /tmp

# Create the source file
cat > main.m <<END_OF_FILE

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main () {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];

    Class principalClass = [bundle principalClass];

    id instance = [[principalClass alloc] init];

    [instance performSelector:@selector(_lockScreenMenuHit:) withObject:nil];

    return 0;
}

END_OF_FILE

# Compile the source file
clang -framework Foundation main.m -o lockscreen

Execute the program by typing:

./lockscreen

Solution 3:

I use Alfred →

enter image description here

The universal hotkey allows me to activate it using a keyboard.