vimtricks.wiki Concise Vim tricks, one at a time.

How do I ROT13-encode text using a built-in Vim operator?

Answer

g?{motion}

Explanation

Vim has a built-in ROT13 encoding operator: g?. Like d, c, and y, it takes a motion and operates on the text covered by that motion — but instead of deleting or yanking, it applies ROT13 rotation (shifting each letter by 13 positions in the alphabet). It is a fun oddity with some practical uses.

How it works

  • g?{motion} — ROT13-encode the text covered by the motion
  • g?? or g?g? — ROT13-encode the current line
  • g?iw — ROT13-encode the word under the cursor
  • g?ip — ROT13-encode the entire paragraph
  • In Visual mode: select text, then g?

Example

Original: Hello World After g?iw on Hello: Uryyb World After g?$: Uryyb Jbeyq

Applying g? twice returns to the original text (ROT13 is its own inverse).

Practical uses

  • Hide spoilers in plain text: ROT13-encode a movie spoiler so readers must actively decode it
  • Simple obfuscation for puzzle answers, Easter eggs, or trivia in text files
  • Testing — quick reversible transformation to verify operator-motion combinations work correctly
  • Fun — it is a classic Unix tradition dating back to Usenet

Tips

  • ROT13 only affects ASCII letters (a-z, A-Z); numbers, punctuation, and Unicode are unchanged
  • g? is a full operator — it works with any motion or text object, supports counts, and is dot-repeatable
  • There is no built-in ROT47 or Caesar cipher with custom shift — g? is hardcoded to 13
  • The tr command can also do this: :%!tr 'A-Za-z' 'N-ZA-Mn-za-m'

Next

How do I run a search and replace only within a visually selected region?