¿Qué le parece la función siguiente? He usado \% x08 en lugar de^H, ya que es más fácil copiar y pegar el código resultante. Usted puede escribir en y utilizar Ctrl - VCtrl - H si lo prefiere, pero pensé \ x08% podría ser más sencillo. Esto también intenta manejar espacios vacíos al comienzo de la línea (simplemente los elimina).
" 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
Para obtener más información, véase:
:help command
:help command-range
:help function
:help function-range-example
:help substitute()
:help =~
:help \%x
Editar
Tenga en cuenta que si quieres trabajar en una sola línea que, podría hacer algo como esto:
" 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>
o usted podría hacer:
nmap ,b :.ApplyBackspaces<CR>
Por curiosidad, ¿cuál es el propósito de los^H caracteres?¿Qué se supone que deben lograr, ya sea en una copia impresa o en la consola? –
@ John Saunders: en las impresoras antiguas, causaron sobreimpresiones que permitían hacer negrita por repetición: x^Hx^Hx^Hx o subrayado: _^Ha_^Hb_^Hc. "menos" en Linux se utiliza para respetar estos y mostrar el texto en negrita o subrayado (¡quizás todavía lo haga!) –
Todavía funciona: $ echo 'h_^Hell^Hlo' | menos –