Skip to content

[sharing]: auto align in insert mode. #168

@ubaldot

Description

@ubaldot

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions