0

I frequently use EasyAlign in my tex files to align tables around the $ delimiter such that this

header1 & header2 & header3 \\
data1 & data2 & data3 \\

becomes this

header1 & header2 & header3 \\
data1   & data2   & data3   \\

As I have EasyAlign mapped to gA, my normal-mode keystrokes to achieve this are gAip*$.

So my first thought was nnoremap <buffer> gT gAip*$, but this doesn't invoke EasyAlign.

As I can vip to select inner paragraph, then :EasyAlign *& to get the alignment, I tried putting these two in a mapped function in my vimfiles\ftplugin\tex.vim:

function! EasyAlignTable()
  normal! vip
  EasyAlign *&
endfunction
nnoremap <buffer> gT :call EasyAlignTable()<cr>

but this somehow doesn't complete: vim remains in visual-mode, and the alignment is not done.

So how do I write this function incorporating EasyAlign?

6
  • 1
    The reason the mapping doesn't call EasyAlign is because you're using nnoremap which ignores existing mappings. Try nmap. This is one of those not-too-common cases where you actually don't want the "noremap" variation. – B Layer Jul 20 at 10:50
  • The issue might be in EasyAlign and a range that is in effect when you select a paragraph. Try '<,'>EasyAlign *& in your function instead. – Maxim Kim Jul 20 at 11:18
  • You probably just have to add the full normal-sequence: normal! vip:EasyAlign *&<CR> – D. Ben Knoble Jul 20 at 14:59
  • @BLayer, nope, the mapping I'm now using is nnoremap <buffer> gT :normal gAip*&<CR> successfully over-mapping default gT. – joharr Jul 22 at 7:31
  • 1
    @joharr "Nope"? I was just referring to the nnoremap gT mapping in your question which absolutely won't get any mapped keys within. A mapping that uses :normal (without !) is a totally different situation. – B Layer Jul 22 at 23:45
2

You can also try :normal command:

:normal gAip*&

And then @: to repeat last command.

Or if you want to map it:

nmap <buffer> gT gAip*&
2
  • 2
    If you're writing a normal-mode mapping, why do :normal? Just nmap <buffer> gT gAip*&<CR>, though beware of shadowing gT builtin – D. Ben Knoble Jul 20 at 14:58
  • @D.BenKnoble arrrghhh, indeed :) – Maxim Kim Jul 20 at 16:21
0

I've found a work-around, with minor disadvantage that it's global, and could be overwritten: let @t = 'gAip*&'. Then, when my cursor is in the table, I just @t.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.