Updated:

Put a diamond helmet on the nearest player

VersionCommand
Java 1.17 and later/item replace entity @p armor.head with minecraft:diamond_helmet
Java 1.16 and earlier/replaceitem entity @p armor.head minecraft:diamond_helmet
Bedrock Edition/replaceitem entity @p slot.armor.head 0 diamond_helmet

Java replaced /replaceitem with /item in 1.17. Bedrock still uses /replaceitem and needs a slot number after the slot name (0 for armor slots).

Slot names

SlotJavaBedrock
Head, chest, legs, feetarmor.head, armor.chest, armor.legs, armor.feetslot.armor.head 0 etc.
Main handweapon.mainhandslot.weapon.mainhand 0
Off handweapon.offhandslot.weapon.offhand 0
Hotbarhotbar.0–hotbar.8slot.hotbar 0–8
Inventoryinventory.0–inventory.26slot.inventory 0–26
Ender chestenderchest.0–enderchest.26slot.enderchest 0–26

Blocks as hats (Java)

/item replace entity @p armor.head with minecraft:glass

In Java Edition any item can go in the head slot. Blocks such as pumpkins, glass or colored wool are drawn on the player's head, which is handy for costumes and team markers.

Equip a mob

/item replace entity @e[type=minecraft:zombie,limit=1,sort=nearest] weapon.mainhand with minecraft:diamond_sword

This equips a mob that already exists. To spawn a mob with gear, use summon with equipment (1.21.5 and later) or HandItems/ArmorItems (1.21.4 and earlier).

Count and enchantments

/item replace entity @p hotbar.0 with minecraft:golden_apple 16
/item replace entity @p armor.feet with minecraft:diamond_boots[enchantments={feather_falling:4}]

The count goes after the item. The enchantment format above is for 1.21.5 and later; on 1.20.5–1.21.4 write [enchantments={levels:{feather_falling:4}}].

Copy an item from another slot

/item replace entity @s armor.head from entity @s weapon.mainhand

With from instead of with, Java copies the item that is already in another slot, including its name and enchantments. This puts a copy of what you are holding on your head. The source can also be a block: from block 100 64 100 container.0.

Clear a slot

/item replace entity @p armor.head with minecraft:air

Putting air in a slot empties it. The old item is deleted, so be careful with valuable gear.

Put an item in a chest slot

/item replace block 100 64 100 container.0 with minecraft:diamond 64

Use block and coordinates instead of entity. container.0 is the top-left slot.

Common problems

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.