The vim equivalent of the tr command

There is a line like this:

xxAyayyBwedCdweDmdwCkDwedBAwe ;;;; cleaner example __A__B__C__D__C_D_BA_ 

need to replace ABCD with PQRT , for example. receive

 __P__Q__R__T__R_T_QP_ 

for example, the equivalent of the following bash or perl tr

 tr '[ABCD]' '[PQRT]' <<<"$string" 

How to do it in "vim"? (VIM-Vi IMproved 7.4 (2013 August 10, compiled May 9, 2014 12:12:40))

+5
source share
1 answer

You can use the tr() function in combination with :global

 :g/./call setline(line('.'), tr(getline('.'), 'ABCD', 'PQRS')) 

Easy to adapt it to the team :%Tr#ABCD#PQRS .

 :command! -nargs=1 -range=1 Translate <line1>,<line2>call s:Translate(<f-args>) function! s:Translate(repl_arg) range abort let sep = a:repl_arg[0] let fields = split(a:repl_arg, sep) " build the action to execute let cmd = a:firstline . ',' . a:lastline . 'g'.sep.'.'.sep \. 'call setline(".", tr(getline("."), '.string(fields[0]).','.string(fields[1]).'))' " echom cmd " and run it exe cmd endfunction 
+9
source

Source: https://habr.com/ru/post/1201817/


All Articles