2

Fo example, this piece of code in rails:

  t.string :receipt_number
  t.timestamp :transaction_timestamp
  t.string :transaction_type
  t.decimal :cash_amount
  t.decimal :card_amount

Now, I want new code should be:

  t.string      :receipt_number
  t.timestamp   :transaction_timestamp
  t.string      :transaction_type
  t.decimal     :cash_amount
  t.decimal     :card_amount

I have tried align left/right/center, but It seems not work.

first case:

  t.string:    receipt_number
  t.timestamp: transaction_timestamp
  t.string:    transaction_type
  t.decimal:   cash_amount
  t.decimal:   card_amount
  t.decimal:   voucher_amount
  t.decimal:   total_amount
  t.int:       sa_id
  t.string:    sa_name

Second case:

     t.string: receipt_number
  t.timestamp: transaction_timestamp
     t.string: transaction_type
    t.decimal: cash_amount
    t.decimal: card_amount
    t.decimal: voucher_amount
    t.decimal: total_amount
        t.int: sa_id
     t.string: sa_name

How can I make alignment as expected code.

0

3 Answers 3

2

Here's an admittedly hacky solution in pure vimscript:

:%norm dt::left 17<C-v><CR>0:exec "norm R".getreg('"')<C-v><CR>

Obviously, you can changed 17 to whatever value works best with your text.

Explanation:

The way this works is by deleting everything up to the first colon before aligning. Then after that, it uses replace mode (R) to paste the deleted text over the extra spaces.

:%norm                          " On every line:
       dt:                      " Delete until a colon
          :left 17<C-v><CR>     " Align to the 17'th column

Then, we move back to the first column with 0 and run the rest. We use exec so that we can construct a normal command with the contents of the deleted text.

:exec                               " Execute the following string
      "norm                         " Enter normal mode
            R"                      " Enter replace mode
              .                     " Concatenate this string 
               getreg('"')          " With the contents of the unnamed register
                          <C-v><CR> " Enter

We use <C-v><cr> so that it enters a literal newline rather than running the command right then.

2
  • 1
    Your code golf background is showing (paying off?). :P
    – Tumbler41
    Oct 21, 2016 at 18:21
  • @Tumbler41 Hehe, maybe a little. But if I wanted to really golf it I would have used <C-v><C-r>" instead of getreg('"')
    – DJMcMayhem
    Oct 21, 2016 at 18:23
1

Aligning delimiter is controlled by stick-to-left option. Its a boolean controlling whether to position delimiter on the left-side. so your wanted result is achievable through selecting the desired lines with V(visual linewise mode) and running the following command:

'<,'>EasyAlign : { 'right-margin':0, 'left-margin':3, 'stick-to-left':0 }

Of course this is too much typing for a such simple aligment task! In practice you could use interesting Live interactive mode. just type :LiveEasyAlign command or even faster, map it to a key using <Plug>(LiveEasyAlign). now you can simply using key combinations to align items.

Aligning delimiter is <Left> and <Right> keys and you can view your code alignment Live.

0

I don't get what the difficulty is, unless you are not aligning using Space as delimiter, and/or you don't specify the region to align. Nobody forces you to use : as delimiter. Steps:

  • Visually select all lines you want (using V etc.)
  • call your EasyAlign key map; the command-line should look like this: :'<,'>EasyAlign(_) (if you type the command manually, you need to hit Enter once)
  • hit Space

... That's it, the text is aligned as in your 2nd sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.