Come circa la seguente funzione? Ho usato \% x08 invece di^H in quanto è più facile copiare e incollare il codice risultante. Si potrebbe digitarlo e utilizzare Ctrl - VCtrl - H, se si preferisce, ma ho pensato \% x08 potrebbe essere più facile. Questo tenta anche di gestire i backspaces all'inizio della riga (li elimina semplicemente).
" Define a command to make it easier to use (default range is whole file)
command! -range=% ApplyBackspaces <line1>,<line2>call ApplyBackspaces()
" Function that does the work
function! ApplyBackspaces() range
" For each line in the selected lines
for index in range(a:firstline, a:lastline)
" Get the line as a string
let thisline = getline(index)
" Remove backspaces at the start of the line
let thisline = substitute(thisline, '^\%x08*', '', '')
" Repeatedly apply backspaces until there are none left
while thisline =~ '.\%x08'
" Substitute any character followed by backspace with nothing
let thisline = substitute(thisline, '.\%x08', '', 'g')
endwhile
" Remove any backspaces left at the start of the line
let thisline = substitute(thisline, '^\%x08*', '', '')
" Write the line back
call setline(index, thisline)
endfor
endfunction
uso con:
" Whole file:
:ApplyBackspaces
" Whole file (explicitly requested):
:%ApplyBackspaces
" Visual range:
:'<,'>ApplyBackspaces
Per ulteriori informazioni, vedere:
:help command
:help command-range
:help function
:help function-range-example
:help substitute()
:help =~
:help \%x
Modifica
Si noti che se si desidera lavorare su una sola linea che, potrebbe fare qualcosa di simile:
" Define the command to default to the current line rather than the whole file
command! -range ApplyBackspaces <line1>,<line2>call ApplyBackspaces()
" Create a mapping so that pressing ,b in normal mode deals with the current line
nmap ,b :ApplyBackspaces<CR>
Oppure si può semplicemente fare:
nmap ,b :.ApplyBackspaces<CR>
Per curiosità, qual è lo scopo dei caratteri^H?Cosa intendono raggiungere, sia in una stampa, sia sulla console? –
@John Saunders: Sulle stampanti vecchio stile, hanno causato la sovrastampa che permetteva di eseguire il grassetto ripetendo: x^Hx^Hx^Hx o sottolineato: _^Ha_^Hb_^Hc. "less" su Linux usato per onorare questi e visualizzare il testo in grassetto o sottolineato (forse lo è ancora!) –
Funziona ancora: $ echo 'h_^Inferno^Hlo' | meno –