vim-easy-align icon indicating copy to clipboard operation
vim-easy-align copied to clipboard

[sharing]: auto align in insert mode.

Open ubaldot opened this issue 5 months ago • 0 comments

This is not really an issue, but rather a function that I wrote to mimic this but by using this plugin.

vim9script

export def Align()
  const p = '^\s*|\s*.*\s*|\s*$'
  if exists(':EasyAlign') != 0 && getline('.') =~# '^\s*|'

    # Save column and position
    const curpos = getcursorcharpos()

    # Search for first line
    var startline = line('.')
    if startline != 1
      while getline(startline - 1) =~ p
        startline = search(p, 'bW')
      endwhile
    endif
    setcursorcharpos(curpos[1], curpos[2])

    # Search for last line
    var endline = line('.')
    if endline != line('$')
      while getline(endline + 1) =~ p
        endline = search(p, 'W')
      endwhile
    endif
    setcursorcharpos(curpos[1], curpos[2])

    # Easy align
    execute $":{startline},{endline}EasyAlign *|"
    setcursorcharpos(curpos[1], strchars(getline(curpos[1])))

  endif
enddef

inoremap <silent> <Bar> <Bar><Esc><ScriptCmd>Align()<CR>a

Hope you find it useful!

ubaldot avatar Jul 26 '25 18:07 ubaldot