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:
It would be nice to do this based on any arbitrary delimiter, not just the pipe character.
If you have not found a way to do it, but you know of a plug-in, that helps, but preference goes to someone who has a working solution that they have used before
If you use the Align plugin, you could simply:
V
:Align |
Where |
is any delimiter
OR
V
\t|
For a preset |
shortcut.
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.
:set noet
to ensure you're not expanding tabs into spaces.:set ts=25
to set a nice large tab stop size.:%s/\w*|\w*/\t|\t/g
to add a single tab-stop around pipes instead of arbitrary spacing.:set et
to make tabs convert back into spaces when we:retab
to convert them.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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With