Align text on an equals sign in vim

后端 未结 9 2443
一生所求
一生所求 2020-12-12 22:48

I tend to align code on equal signs for better readability. From this:

$ = jQuery.sub()
Survey = App.Survey
Sidebar = App.Sidebar
Main = App.Main


        
相关标签:
9条回答
  • 2020-12-12 22:49

    I believe this is easily done with the Tabular plugin. Here it is in action.

    Select the range in Visual mode (not actually necessary), and do:

    :Tabularize /=
    

    The plugin can actually find the correct range on its own often, without needing to select it visually or specify a range to the ex command.

    0 讨论(0)
  • 2020-12-12 22:49

    The vim-easy-align plugin does that as well. Here is one of the many examples given:

    0 讨论(0)
  • 2020-12-12 22:51

    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 /=
    
    0 讨论(0)
  • 2020-12-12 22:51

    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.
    0 讨论(0)
  • 2020-12-12 22:53

    Install tabularize plugin and modify gist by tpope to something like this :

    inoremap <silent> :   :<Esc>:call <SID>align(':')<CR>a
    inoremap <silent> =   =<Esc>:call <SID>align('=')<CR>a
    
    function! s:align(aa)
      let p = '^.*\s'.a:aa.'\s.*$'
      if exists(':Tabularize') && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
        let column = strlen(substitute(getline('.')[0:col('.')],'[^'.a:aa.']','','g'))
        let position = strlen(matchstr(getline('.')[0:col('.')],'.*'.a:aa.':\s*\zs.*'))
        exec 'Tabularize/'.a:aa.'/l1'
        normal! 0
        call search(repeat('[^'.a:aa.']*'.a:aa,column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
      endif
    endfunction
    
    0 讨论(0)
  • 2020-12-12 22:54

    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.

    0 讨论(0)
提交回复
热议问题