How to implement Caesar cipher-like text substitution in Vim?

1. If the alphabetic characters are arranged sequentially in the target encoding (as is the case for ASCII and some alphabets in UTF-8, like English), one can use the following substitution command:

:%s/./\=nr2char(char2nr(submatch(0))+2)/g

(Before running the command, make sure that the encoding option is set accordingly.)

However, this replacement implements a non-circular letter shift. A circular shift can be implemented by two substitutions separately handling lowercase and uppercase letters:

:%s/\l/\=nr2char(char2nr('a') + (char2nr(submatch(0)) - char2nr('a') + 2) % 26)/g
:%s/\u/\=nr2char(char2nr('A') + (char2nr(submatch(0)) - char2nr('A') + 2) % 26)/g

2. Another way is to translate characters using the tr() function. Let us assume that the variable a contains lowercase characters of an alphabet arranged in correct order, and the variable a1 hold the string of characters corresponding to those in a (below is an example for English letters).

:let a = 'abcdefghijklmnopqrstuvwxyz'
:let a1 = a[2:] . a[:1]

To avoid typing the whole alphabet by hand, the value of a can be produced as follows:

:let a = join(map(range(char2nr('a'), char2nr('z')), 'nr2char(v:val)'), '')

Then, to replace each letter on a line by the letter two positions down the alphabet, one can use the following substitution:

:%s/.*/\=tr(submatch(0), a . toupper(a), a1 . toupper(a1))

Yes, \= will execute the function

%s/\(.\)/\=nr2char(char2nr(submatch(1)) + 2)/g

Tags:

Vim