Search and replace VIM

I made a riddle where every English char is replaced with 2 char advanced. For example, "apple" will be written as "crrng" a + 2 = c, ... and similarly for pple. In python maketrans, I was able to do this.

I was wondering if something like this is possible in finding and replacing VIM?

Any ideas ???

+5
source share
4 answers

If alphabetical characters are arranged sequentially in target encoding 1, use the following substitution command 2 .

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

However, this replacement implements a non-circular letter shift. A circular shift can be implemented by two substitutions processing 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

- tr(). , a , a1 , a ( ).

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

, a ,

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

, ,

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

1 ASCII UTF-8, . .

2 , encoding .

+9

, \=

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

vim, unix 'tr' (, ).

0

The puzzle you are describing is commonly known as the Caesar code and is usually implemented using the tror command sed -e y/. Since y is not available in vim, you will need a pretty dirty hack like ib, but calling tr is much nicer.

Especially considering the angular case of y and z: I assume that they should be matched with a and b, respectively?

0
source

All Articles