9

ときどき、Emacs で次のようなテキスト ファイルを作成します。

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

数字を(スペースを使用して)右揃えし、次のように変更したいと思います

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

Emacsでこれを行うにはどうすればよいですか?

4

1 に答える 1

13

align-regexpこれを行うことができます。領域をマークしてから、次を使用します。

C-uM-x align-regexp RET \(\s-+[0-9]*\)[0-9] RET -1 RET 4 RET y

それが最も簡単なアプローチです。

(編集:実際、最後の桁を分離する必要さえありません\(\s-+[0-9]+\)。正規表現でも同様に機能します。)

実際に何をしているかについては、インタラクティブなプロンプトと変数を参照してくださいC-hf align-regexp RETalign-rules-list

注目すべき部分は、グループに負の数を指定することで、属性をalign-regexp設定することです: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.

別の方法として、さまざまなテーブル編集オプション (org、ses、table-capture/release など) を使用するか、elisp 置換パターンを使用してこれを処理することもできます。

たとえば、ファイルがすでに整列のためにスペースを使用していて (untabifyそうでない場合はタブを削除するために使用できます)、すべての行が同じ長さである (つまり、末尾のスペースが最後の列が可変長の場合、一部の行で必要です)。

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

于 2012-06-05T14:39:49.627 に答える