Search code examples
vimalignmenttabular

Vim: Receive char on function to tabularize


I learned that using this:

let mapleader=','
if exists(":Tabularize")
  nmap <Leader>a= :Tabularize /=<CR>
  vmap <Leader>a= :Tabularize /=<CR>
endif 

would give me a shortcut to tabularize with the '=' char. But I'd like to generalize it, so that I could use some shortcut like:

<Leader>a$
<Leader>a*

And it would read the '$' or '*' char and use it as the "char to tabularize". I.e., pass this char to the :Tabularize /CHAR function

Any ideas?


Solution

  • If you put that if block in your vimrc it will never work, because vimrc is sourced before any plugins, so the :Tabularize command doesn't exist yet when the expresion exists(':Tabularize') is evaluated and it will always be false.

    You could use these mappings:

    nnoremap <Leader>a :Tabularize /
    vnoremap <Leader>a :Tabularize /
    

    So when you press ,a* you'll be left in in command line mode with :Tabularize /*, ready to press Enter.