Language Desk Vim & Emacs

Programming languages,
side by side.

Everyday syntax and editor commands in one quick reference.

Scripting

TopicPythonJavaScriptTypeScript
Run a file
python3 main.py
node main.js
npx tsx main.ts
Variable
count = 3
let count = 3;
let count: number = 3;
Print
print("Hello")
console.log("Hello");
console.log("Hello");
String interpolation
f"Hello, {name}"
`Hello, ${name}`
`Hello, ${name}`
List
[1, 2, 3]
[1, 2, 3]
const nums: number[] = [1, 2, 3];
Dictionary / map
{"score": 3}
new Map([["score", 3]])
new Map<string, number>([["score", 3]])
Define a function
def add(a, b):
    return a + b
function add(a, b) {
  return a + b;
}
function add(a: number, b: number): number {
  return a + b;
}
Loop over values
for value in nums:
    print(value)
for (const value of nums) {
  console.log(value);
}
for (const value of nums) {
  console.log(value);
}
Keep matching values
[n for n in nums if n > 1]
nums.filter(n => n > 1)
nums.filter(n => n > 1)
Parse JSON
import json
obj = json.loads(text)
const obj = JSON.parse(text);
const obj: unknown = JSON.parse(text);
Read a UTF-8 file
from pathlib import Path
text = Path("notes.txt").read_text(encoding="utf-8")
import { readFile } from "node:fs/promises";
const text = await readFile("notes.txt", "utf8");
import { readFile } from "node:fs/promises";
const text: string = await readFile("notes.txt", "utf8");

JavaScript command-line examples use Node.js. The TypeScript launch command uses tsx. The file-reading examples use ECMAScript modules.

Systems

TopicC++GoRust
Build or run
c++ -std=c++20 main.cpp -o app
go run main.go
cargo run
Variable
int count = 3;
count := 3
let mut count = 3;
Print
std::cout << "Hello\n";
fmt.Println("Hello")
println!("Hello");
String interpolation
std::format("Hello, {}", name)
fmt.Sprintf("Hello, %s", name)
format!("Hello, {name}")
List
std::vector<int> nums{1, 2, 3};
nums := []int{1, 2, 3}
let nums = vec![1, 2, 3];
Dictionary / map
std::map<std::string, int> scores{{"score", 3}};
scores := map[string]int{"score": 3}
let scores = std::collections::HashMap::from([("score", 3)]);
Define a function
int add(int a, int b) {
    return a + b;
}
func add(a, b int) int {
    return a + b
}
fn add(a: i32, b: i32) -> i32 {
    a + b
}
Loop over values
for (auto value : nums) {
    std::cout << value << '\n';
}
for _, value := range nums {
    fmt.Println(value)
}
for value in &nums {
    println!("{value}");
}
Optional value
std::optional<int> value = std::nullopt;
var value *int = nil
let value: Option<i32> = None;
Read a UTF-8 file
std::ifstream input("notes.txt");
std::string text((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
data, err := os.ReadFile("notes.txt")
if err != nil {
    return err
}
text := string(data)
let text = std::fs::read_to_string("notes.txt")?;

Snippets omit imports, headers and program entry points. The Go and Rust file snippets assume an enclosing function that can return an error. C++ and Go read bytes in these examples.

Application languages

TopicJavaC#
Run
java Main.java
dotnet run
Variable
int count = 3;
int count = 3;
Print
System.out.println("Hello");
Console.WriteLine("Hello");
String interpolation
"Hello, %s".formatted(name)
$"Hello, {name}"
List
var nums = List.of(1, 2, 3);
var nums = new List<int> { 1, 2, 3 };
Dictionary / map
var scores = Map.of("score", 3);
var scores = new Dictionary<string, int> { ["score"] = 3 };
Define a function
static int add(int a, int b) {
    return a + b;
}
static int Add(int a, int b) => a + b;
Loop over values
for (int value : nums) {
    System.out.println(value);
}
foreach (int value in nums) {
    Console.WriteLine(value);
}
Keep matching values
nums.stream().filter(n -> n > 1).toList()
nums.Where(n => n > 1).ToList()
Read a UTF-8 file
String text = Files.readString(Path.of("notes.txt"));
string text = File.ReadAllText("notes.txt");

Java examples use modern JDK syntax. The C# run command assumes a .NET project. Some examples require standard-library imports.

Text editors

TopicVimEmacs
Open a file from the shell
vim notes.txt
emacs notes.txt
Open a file inside the editor
:e notes.txt
C-x C-f notes.txt RET
Enter text
i, then Esc to return to Normal mode
Type directly
Save
:w
C-x C-s
Quit
:q
C-x C-c
Search forward
/word RET  ·  n for next match
C-s word  ·  C-s for next match
Replace throughout the file
:%s/old/new/g
M-% old RET new RET  ·  ! for all remaining
Go to line 42
42G
M-g g 42 RET
Select text
v, then move the cursor
C-SPC, then move the cursor
Copy a selection
y
M-w
Paste
p
C-y
Undo
u
C-/
Split into side-by-side panes
:vsplit
C-x 3

Vim commands start in Normal mode. In Emacs, C means Control and M usually means Alt. RET is Enter. For a full-buffer replacement in Emacs, start at the beginning of the buffer.