I want emacs to format the space-separated columns in a nice way.

Asked 2 years ago, Updated 2 years ago, 79 views

For example, the following is the migration code for rails:

class CreatePosts<ActiveRecord::Migration [6.0]
  def change
    create_table —posts do | t |
      t.string: title, comment: 'Title'
      t.text:body, comment:'Body'
      t.integer:user_id, comment: 'User ID'

      t.timestamps
    end
  end
end

However, since it is difficult to read the code, I would like to do the following if possible.

class CreatePosts<ActiveRecord::Migration [6.0]
  def change
    create_table —posts do | t |
      t.string: title, comment: 'Title'
      t.text:body, comment:'Body'
      t.integer:user_id, comment: 'User ID'

      t.timestamps
    end
  end
end

However, I thought that inserting this kind of space would be a little too hard to do by hand every time.

Is there a way to shape the lines of code that I want to align vertically, separated by space, as shown above?

emacs elisp

2022-09-29 22:56

1 Answers

There seems to be a way to shape it with M-x align-regex.

According to the question, specifying columns corresponding to :title and :body may be a bit troublesome.

Note:
How to shape source code with emacs

You can use M-x align-regex to align text with a specified word (strictly regular expression).
For example, if you want to sort by variable name msg, M-x align-regex and type "msg".

const char*foo(int code){
    const char*msg = NULL;
    switch(code){
    caseLS_SUCCESS: msg = "success"; break;
    caseLS_NO_RECORD: msg = "no record"; break;
    caseLS_NO_TOKEN: msg = "no token"; break;
    }
    return msg;
}


2022-09-29 22:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.