Vim step by step: how do you produce arbitrary text with an arbitrary separator?

Background:

There are many great lessons and tricks for Vim, but it’s very difficult to find specific instructions on how to do some arbitrary thing that can be easily done in one familiar IDE text editor. Therefore, I ask for step-by-step instructions on how you will do something in Vim, that I already know how to do it in other text editors. I like Vim and a lot of built-in help and numerous online lessons, but sometimes a person has to break down and ask another person.

Question:

Suppose I have the following code in my file, how can I use Vim to switch from BEFORE, 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:

  • It would be nice to do this based on any arbitrary separator, not just a pipe character.

  • If you have not found a way to do this, but you know about the plug-in, this helps, but preference is given to someone who has a working solution that they used before

+7
vim editor
source share
3 answers

If you use the Align plugin, you can simply:

  • Visually select text with V
  • Type :Align |

Where | - any separator

OR

  • Visually select text with V
  • Type \t|

For speed dialing | .

+13
source share

This is not a magic bullet, and if you do it a lot, I hope you find / make a plugin, but these steps really work, do not require anything special, and they study well, since they generalize for a number of tasks.

  • :set noet so you don't expand the tabs into spaces.
  • :set ts=25 to set the large size of the stop calendar.
  • :%s/\w*|\w*/\t|\t/g to add a single tab around the pipes instead of an arbitrary interval.
  • :set et to make tabs converted back to spaces when we
  • :retab to convert them.
  • At this point, everything is well lined up, but there may be excessive distance. Therefore, use the visual block mode ( Ctrl + v ) to clear the contents.

I often use combinations of such things for a number of text manipulation tasks. The visual block mode is extremely useful, and I have not seen many other editors that have something comparable. See also :help blockwise-examples .

+3
source share

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

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

shows how to create a function to build assignment operators. It is located in the "Function" section, which will help you encode. I think you could generalize it to your needs. Or use the Align plugin. Sometimes I am torn between writing my own tool from which I can learn something, and get some other tool, which is often better than mine, and allows me to get back to work faster.

By the way, the whole series is excellent, and ahead is yet to come. The last paragraph of the last article says:

So, in the next article in this series, take a good look at the simple Vims architecture that allows you to separate parts of your .vimrc and isolate them in separate modules. See how this plug-in works by creating a stand-alone module that improves some of the horrors of working with XML.

+2
source share

All Articles