dreftymac
dreftymac

Reputation: 32370

Vim step-by-step: How do you line up arbitrary text by arbitrary delimiter?

Background:

There are a lot of great tutorials and "tricks" pages for Vim, but one thing that is very difficult is to find specific instructions on how to do some arbitrary thing that one can easily do in one's own familiar text editor IDE. Therefore I am asking for step by step instructions on how you I would do something in Vim that I already know how to do in other text editors. I like Vim and the great built-in help and numerous on-line tutorials, but sometimes a human has to break down and ask another human.

Question:

Suppose I have the following code in my file, how can I use Vim to get from BEFORE, to AFTER?

  BEFORE:
  Lorem ipsum dolor |  sit amet, consectetur | adipisicing elit,
  sed do eiusmod | tempor incididunt | ut 
  labore et | dolore magna aliqua. | Ut enim ad minim veniam,
  quis nostrud | exercitation ullamco | laboris 
  nisi ut | aliquip ex ea commodo | consequat. Duis aute irure

  AFTER:
  Lorem ipsum dolor  |  sit amet, consectetur   |  adipisicing elit,         
  sed do eiusmod     |  tempor incididunt       |  ut                        
  labore et          |  dolore magna aliqua.    |  Ut enim ad minim veniam,  
  quis nostrud       |  exercitation ullamco    |  laboris                   
  nisi ut            |  aliquip ex ea commodo   |  consequat. Duis aute irure

Notes:

Upvotes: 7

Views: 557

Answers (3)

Andrew Langman
Andrew Langman

Reputation: 1771

Damian Conway has a series of articles about Vim at IBM Developer Works. This one:

http://www.ibm.com/developerworks/linux/library/l-vim-script-2/index.html

shows how to create a function to line up assignment operators. It's in the section called A function to help you code. I think you could generalize it to your needs. Or use the Align plugin. I'm sometimes torn between writing my own tool, from which I can learn something, and getting somebody else's tool, which is often better than mine, and lets me get back to work more quickly.

By the way, the entire series is excellent, and there are more to come. The last paragraph of the last article says:

So, in the next article in this series we’ll explore Vim’s simple plug-in architecture, which allows you to factor out parts of your .vimrc and isolate them in separate modules. We’ll look at how that plug-in system works by developing a standalone module that ameliorates some of the horrors of working with XML.

Upvotes: 2

Randy Morris
Randy Morris

Reputation: 40927

If you use the Align plugin, you could simply:

  1. Visually select the text with V
  2. Type :Align |

Where | is any delimiter

OR

  1. Visually select the text with V
  2. Type \t|

For a preset | shortcut.

Upvotes: 13

Peter
Peter

Reputation: 132247

It's no magic bullet, and if you're doing this a lot I hope you'll find/make a plugin, but these steps do work, don't require anything special installed, and are good to learn anyway since they generalize for a whole range of tasks.

  1. :set noet to ensure you're not expanding tabs into spaces.
  2. :set ts=25 to set a nice large tab stop size.
  3. :%s/\w*|\w*/\t|\t/g to add a single tab-stop around pipes instead of arbitrary spacing.
  4. :set et to make tabs convert back into spaces when we
  5. :retab to convert them.
  6. At this point, things are nicely lined up, but there might be excessive spacing. So use visual-block mode (Ctrl+v) to tidy stuff up.

I use combinations of things like this frequently, for a range of text manipulation tasks. Visual block mode is exceedingly useful and I haven't seen many other editors that have something comparable. See also :help blockwise-examples.

Upvotes: 3

Related Questions