2012-06-05 9 views
5

A volte, ho un file di testo come questo in Emacs:destro in Emacs

some text  123  17 
other text 1  0 
still more 12  8 
last one  1234 123 

Vorrei destra -align i numeri (con spazi), trasformandolo in qualcosa di simile :

some text  123  17 
other text  1  0 
still more  12  8 
last one  1234 123 

Come è possibile farlo in Emacs?

+0

org-mode e un tavolo? – Reactormonk

+1

tabella-mode, org-mode non è necessario qui, anche se generalmente molto utile –

+0

Alla fine preferisco una soluzione basata su 'rect' (http://stackoverflow.com/a/10916207/357313). –

risposta

8

align-regexp può fare questo. Mark la regione, e quindi utilizzare:

C-uM-xalign-regexpRET\(\s-+[0-9]*\)[0-9]RET-1RET4RETy

che dovrebbe essere l'approccio più semplice.

(Edit: In realtà, non è nemmeno bisogno di separare tale cifra finale; \(\s-+[0-9]+\) funziona altrettanto bene per la regexp.)

Vedi le istruzioni interattive e Chfalign-regexpRET e la variabile align-rules-list per ciò che effettivamente sta facendo.

La parte degno di nota è che specificando un numero negativo per il gruppo, align-regexp imposta l'attributo justify:

`justify' 
It is possible with `regexp' and `group' to identify a 
character group that contains more than just whitespace 
characters. By default, any non-whitespace characters in 
that group will also be deleted while aligning the 
alignment character. However, if the `justify' attribute 
is set to a non-nil value, only the initial whitespace 
characters within that group will be deleted. This has 
the effect of right-justifying the characters that remain, 
and can be used for outdenting or just plain old right- 
justification. 

alternativa le varie opzioni tabella di modifica possono affrontare questo (es org, ses, tavola -capture/release), oppure potresti farlo con un pattern di sostituzione elisp.

ad es. Quanto segue dovrebbe fare più o meno quello che stai cercando, a condizione che il file stia già usando spazi per l'allineamento (puoi usare untabify per rimuovere le schede se non lo sono), e che tutte le linee hanno la stessa lunghezza (cioè gli spazi finali sono necessario su alcune linee se la colonna finale è di lunghezza variabile).

C-M-%\([0-9]+\)\([[:space:]]+\)RET\,(format (concat "%" (number-to-string (1- (length \&))) "d ") (string-to-number \1))RET