2014-09-04 13 views
5

"tr" hanno tale linea:Vim equivalente del comando

xxAyayyBwedCdweDmdwCkDwedBAwe 
;;;; cleaner example 
__A__B__C__D__C_D_BA_ 

vogliono sostituire il ABCD in PQRT esempio per ottenere

__P__Q__R__T__R_T_QP_ 

per esempio l'equivalente il del prossimo bash o perl tr

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

come fare questo in "vim"? (VIM - Vi migliorato 7.4 (2013 10 agosto, compilato 9 Mag 2014 12:12:40))

+6

è questo inganno? '%! tr 'ABCD' 'PQRT'' – Kent

+0

@Kent heh ... chiama shell? funziona, ma ha problemi occasionali con la fuga di metacarattere, specialmente quando si vuole 'tr' il' [] 'a'() 'e tale ... – kobame

+1

chiamando'! tr' è solo per un breve cmd. il 'tr()' è migliore, in particolare quando '(external) tr' non è disponibile. tuttavia, con '! tr','() o [] 'non sarà un problema. non è regex. – Kent

risposta

9

è possibile utilizzare la funzione di tr() combinato con :global

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

E 'facile da adattarlo ad un Comando :%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 
+0

Ottimo! Per me basta la 1a linea. :) Grazie mille. – kobame

+1

Bello. Non hai bisogno di ': g /./' BTW, dato che ': call' può prendere un intervallo :. –

+0

Oh. Non lo sapevo. Grazie. –

Problemi correlati