Updated:

1. An edible diamond

/give @s minecraft:diamond[consumable={consume_seconds:1.6},food={nutrition:4,saturation:2.4}]

consume_seconds is how long eating takes. nutrition is the hunger restored and saturation the hidden saturation. From 1.21.2 an item needs consumable to be eaten.

2. Eat when full

/give @s minecraft:diamond[consumable={consume_seconds:1.6},food={nutrition:4,saturation:2.4,can_always_eat:true}]

3. Effects on eating

/give @s minecraft:cookie[consumable={consume_seconds:0.8,on_consume_effects:[{type:"minecraft:apply_effects",effects:[{id:"minecraft:speed",duration:600,amplifier:1}]}]}]

This cookie gives Speed II for 30 seconds. duration is in ticks (20 per second) and amplifier 0 is level I. See the effect ID list.

4. Random teleport

/give @s minecraft:apple[consumable={consume_seconds:1.6,on_consume_effects:[{type:"minecraft:teleport_randomly",diameter:32}]}]

Works like a chorus fruit. Other types include clear_all_effects.

5. Make a drink

/give @s minecraft:honey_bottle[consumable={consume_seconds:1,animation:"drink",sound:"minecraft:entity.generic.drink"},food={nutrition:2,saturation:1,can_always_eat:true}]

For plain potions, the give potion command is simpler.

6. Leave a container after eating

/give @s minecraft:cookie[consumable={consume_seconds:1.6},food={nutrition:6,saturation:7},use_remainder={id:"minecraft:bowl",count:1}]

use_remainder leaves an item in your hand after eating, so a stew-style dish can return its bowl (1.21.2+).

7. A usable item that is not food

/give @s minecraft:paper[consumable={consume_seconds:2,animation:"bow",on_consume_effects:[{type:"minecraft:apply_effects",effects:[{id:"minecraft:night_vision",duration:1200}]}]},custom_name="Scroll of Sight"]

With consumable but no food, the item fills no hunger and works even when full. This scroll gives a minute of night vision after two seconds. Great for adventure map magic. The name uses the 1.21.5+ form.

8. Version differences

1.21.2+: use both consumable and food. 1.20.5 to 1.21.1: food only, with eat_seconds for eating time. 1.20.4 and older, and Bedrock, cannot do this with a command alone (NBT vs components).

Troubleshooting

Check your command

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