How to insert enough spaces to align text to column number using Vim?

4

1

  COMP_ID=`      echo $SRC | sed -e 's/.*COMP_ID=//'  -e 's/:.*$//'`
  SRC_TYP=`      echo $SRC | sed -e 's/.*SRC_TYP=//'  -e 's/:.*$//'`
  DOC_TYP=`      echo $SRC | sed -e 's/.*DOC_TYP=//'  -e 's/:.*$//'`
  SRC_ID=`       echo $SRC | sed -e 's/.*SRC_ID=//'      -e 's/:.*$//'`
  ACC=`          echo $SRC | sed -e 's/.*ACC=//'      -e 's/:.*$//'`
  PASS=`         echo $SRC | sed -e 's/.*PASS=//'  -e 's/:.*$//'`
  POP=`          echo $SRC | sed -e 's/.*POP=//'      -e 's/:.*$//'`
  REMOTE_HOST=`  echo $SRC | sed -e 's/.*REMOTE_HOST=//'  -e 's/:.*$//'`
  REMOTE_PATH=`  echo $SRC | sed -e 's/.*REMOTE_PATH=//'  -e 's/:.*$//'`
  ARCHIVE_PATH=` echo $SRC | sed -e 's/.*ARCHIVE_PATH=//' -e 's/:.*$//'`

Using vim (or vi) I want to align the sections with -e 's/:.*$//' to the same column number. What's the easiest and fastest way to do this? (ignore the ugly echo ..| sed .. bits for now)

Felipe Alvarez

Posted 2014-06-20T06:13:19.097

Reputation: 1 666

Did you consider using a plugin like Align or Tabular?

– romainl – 2014-06-20T06:44:47.197

Answers

3

For alignment, there are three well-known plugins:

With the first, your problem can be solved via

:%Align -e

Ingo Karkat

Posted 2014-06-20T06:13:19.097

Reputation: 19 513

Well done. thanks for the those tips. For history's sake, the first two failed to install or operate correctly for me. vim-easy-plugin was the ticket. Did exactly what I want. vip<enter>-<CTRL-/> -e <enter> did it for me. My version VIM - Vi IMproved 7.0 (2006 May 7, compiled Jun 12 2009 07:08:36) – Felipe Alvarez – 2014-06-27T06:05:31.463

Why are you using such an outdated Vim version?! By updating, you'll get many fixes and new features! – Ingo Karkat – 2014-06-27T06:12:12.773

2

You can do it with no plug-in, like this:

:%s#\(.*\)\zs\ze-e#\=repeat(' ',58-len(submatch(1)))

Note: This assumes that -e is the last of line. But you can capture it otherwise if it is not suitable to your case.

Explanation:

  • %s#\(.*\) - captures the line before the -e.
  • \zs\ze - starts and stops the match here.
  • -e# - just before the -e.
  • Using \zs and \ze here let us to add our spaces directly before -e (otherwise concatenation with .submatch(x) would have been possible).
  • \=repeat(' ',58-len(submatch(1))) - replace this location with a variable number of spaces and where 58 is your aimed column.

bzbzh

Posted 2014-06-20T06:13:19.097

Reputation: 21