Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
Tabularize/|/l1
normal! 0
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
@roman

This comment has been minimized.

Show comment
Hide comment
@roman

roman Feb 23, 2011

I'm wondering, what does the "l1" option on the match of the Tabularize?

roman commented Feb 23, 2011

I'm wondering, what does the "l1" option on the match of the Tabularize?

@tpope

This comment has been minimized.

Show comment
Hide comment
@tpope

tpope Feb 27, 2011

l means left align; 1 means one space of padding.

Owner

tpope commented Feb 27, 2011

l means left align; 1 means one space of padding.

@agibralter

This comment has been minimized.

Show comment
Hide comment
@agibralter

agibralter Mar 29, 2011

Would you recommend putting this in a ftplugin so as to not slow down non-cucumber-related file editing?

Would you recommend putting this in a ftplugin so as to not slow down non-cucumber-related file editing?

@tpope

This comment has been minimized.

Show comment
Hide comment
@tpope

tpope Mar 29, 2011

If you do, make sure you add <buffer> to the map. In practice, I haven't had any issues with it being global.

Owner

tpope commented Mar 29, 2011

If you do, make sure you add <buffer> to the map. In practice, I haven't had any issues with it being global.

@agibralter

This comment has been minimized.

Show comment
Hide comment
@agibralter

agibralter Mar 29, 2011

Ah cool, thank you!

Ah cool, thank you!

@tswicegood

This comment has been minimized.

Show comment
Hide comment
@tswicegood

tswicegood Apr 27, 2011

FYI: I just added this to my after/ftplugin/cucumber.vim and it works like a charm. Very nice.

FYI: I just added this to my after/ftplugin/cucumber.vim and it works like a charm. Very nice.

@nmunson

This comment has been minimized.

Show comment
Hide comment
@nmunson

nmunson May 17, 2011

Thanks! Added this to my vimrc and it works perfectly.

nmunson commented May 17, 2011

Thanks! Added this to my vimrc and it works perfectly.

@jznhljg

This comment has been minimized.

Show comment
Hide comment
@jznhljg

jznhljg Jul 28, 2011

I copied this into my .vimrc. And I have installed Tabular.vim successfully.
But it doesn't work. I input ab | cd | ef and nothing happened. What's wrong?!

jznhljg commented Jul 28, 2011

I copied this into my .vimrc. And I have installed Tabular.vim successfully.
But it doesn't work. I input ab | cd | ef and nothing happened. What's wrong?!

@tpope

This comment has been minimized.

Show comment
Hide comment
@tpope

tpope Jul 28, 2011

@jznhljg, that doesn't look like a valid Cucumber table to me.

Owner

tpope commented Jul 28, 2011

@jznhljg, that doesn't look like a valid Cucumber table to me.

@jznhljg

This comment has been minimized.

Show comment
Hide comment
@jznhljg

jznhljg Jul 28, 2011

@tpope, I just want to edit some plain text, can I use this function to automatically adjust the padding space?

jznhljg commented Jul 28, 2011

@tpope, I just want to edit some plain text, can I use this function to automatically adjust the padding space?

@tpope

This comment has been minimized.

Show comment
Hide comment
@tpope

tpope Jul 28, 2011

Sure, if you relax the regex a bit. As it stands now, it's hell bent on avoiding false positives.

Owner

tpope commented Jul 28, 2011

Sure, if you relax the regex a bit. As it stands now, it's hell bent on avoiding false positives.

@jznhljg

This comment has been minimized.

Show comment
Hide comment
@jznhljg

jznhljg Jul 28, 2011

jznhljg commented Jul 28, 2011

@ests

This comment has been minimized.

Show comment
Hide comment
@ests

ests Sep 7, 2011

I was looking for some solution for dealing with Cuke tables in gVIM similar or close to the one TextMate bundle for Cucumber has.
But this is so much better, thanks a lot Tim, as usual :)

ests commented Sep 7, 2011

I was looking for some solution for dealing with Cuke tables in gVIM similar or close to the one TextMate bundle for Cucumber has.
But this is so much better, thanks a lot Tim, as usual :)

@MaienM

This comment has been minimized.

Show comment
Hide comment
@MaienM

MaienM Oct 2, 2011

For those interested, I made a modified version of this that does the same for equals (=) signs. You can find it in the list of forks to the right, or here: https://gist.github.com/1258015
Credits still go to Tim for the awesome original, I just made a few small changes.

MaienM commented Oct 2, 2011

For those interested, I made a modified version of this that does the same for equals (=) signs. You can find it in the list of forks to the right, or here: https://gist.github.com/1258015
Credits still go to Tim for the awesome original, I just made a few small changes.

@quentindecock

This comment has been minimized.

Show comment
Hide comment
@quentindecock

quentindecock Jul 11, 2012

For those who are interested I made a little plugin that you can use easily with pathogen, it is available here: https://github.com/quentindecock/vim-cucumber-align-pipes

Feedback are welcome,

Enjoy,

For those who are interested I made a little plugin that you can use easily with pathogen, it is available here: https://github.com/quentindecock/vim-cucumber-align-pipes

Feedback are welcome,

Enjoy,

@dhruvasagar

This comment has been minimized.

Show comment
Hide comment
@dhruvasagar

dhruvasagar Mar 18, 2013

For those interested, I made a little plugin that extends this concept to creating full fledged tables as you type :). It is available here - https://github.com/dhruvasagar/vim-table-mode. You can put let g:table_mode_border=0 in your vimrc to get the same behavior as this plugin.

Also you can change the separator itself by let g:table_mode_separator='=' or whatever you need to achieve the same effect for other type of characters. Please refer to the :h table-mode.txt for more information. Feedback is welcome :).

Thanks.

For those interested, I made a little plugin that extends this concept to creating full fledged tables as you type :). It is available here - https://github.com/dhruvasagar/vim-table-mode. You can put let g:table_mode_border=0 in your vimrc to get the same behavior as this plugin.

Also you can change the separator itself by let g:table_mode_separator='=' or whatever you need to achieve the same effect for other type of characters. Please refer to the :h table-mode.txt for more information. Feedback is welcome :).

Thanks.

@wildskyf

This comment has been minimized.

Show comment
Hide comment
@wildskyf

wildskyf Jan 21, 2016

This is really useful!
Thx a lot!

This is really useful!
Thx a lot!

@timabell

This comment has been minimized.

Show comment
Hide comment
@timabell

timabell Mar 21, 2016

Thanks tpope for this and all your other amazing work. I ended up here after asking this question: http://vi.stackexchange.com/questions/6908/how-can-i-replace-a-word-in-a-column-without-changing-the-width

Thanks tpope for this and all your other amazing work. I ended up here after asking this question: http://vi.stackexchange.com/questions/6908/how-can-i-replace-a-word-in-a-column-without-changing-the-width

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment