Align text on an equals sign in vim
For a simple solution that does not involve installing a plugin, just filter through the Unix column command.
Note there are two ways to do this depending on whether your column
command supports -o
.
GNU column command (Linux etc)
:% ! column -t -s= -o=
That's it.
BSD column command (Mac OS X etc)
Step one, filter through column -t
:
:% ! column -t
Step two, remove the padding around the delimiter:
:%s/ = /=/
Initial text is
$ = jQuery.sub()
Survey = App.Survey
Sidebar = App.Sidebar
Main = App.Main
After step one it becomes
$ = jQuery.sub()
Survey = App.Survey
Sidebar = App.Sidebar
Main = App.Main
And after step two
$ = jQuery.sub()
Survey = App.Survey
Sidebar = App.Sidebar
Main = App.Main
Or, if you want to do it in one step:
:% ! column -t | sed 's/ = /=/'
For more info, man column
.
This isn't the easiest way to do things, but it can be done without any plugins.
- Use
V
and:s/=/ =/
to insert a bunch of spaces before each equals sign. - Use Ctrl-V to select the column that you want the equals signs to be moved into.
- Press
<<
to "unindent" the right hand side of each equation towards the column you selected, then press.
repeatedly until the equals signs are lined up in that column.
The best plugin I found so far is Tabular.vim.
Easiest way to install it is by using the Pathogen plugin, and then cloning the Tabular git repository to ~/.vim/bundle/tabular
. Full instructions in the Pathogen README.
After it's installed, using it is just a matter of putting your cursor somewhere in the paragraph you want to align and running:
:Tab /=