Skip to content

Instantly share code, notes, and snippets.

@tpope
Created January 26, 2010 19:51
Star You must be signed in to star a gist
Save tpope/287147 to your computer and use it in GitHub Desktop.
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
Tabularize/|/l1
normal! 0
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
@wildskyf
Copy link

This is really useful!
Thx a lot!

@timabell
Copy link

Thanks tpope for this and all your other amazing work. I ended up here after asking this question: http://vi.stackexchange.com/questions/6908/how-can-i-replace-a-word-in-a-column-without-changing-the-width

@eternalcyan
Copy link

this function really awesome! thanks

@zoliky
Copy link

zoliky commented Mar 26, 2020

Can I make this to work only on specific file formats such as AsciiDoc? Is it okay to add the code to ftplugin/asciidoc.vim? Thanks!

@tpope
Copy link
Author

tpope commented Mar 26, 2020

Sure, make it a <buffer> map and unmap it in b:undo_ftplugin.

@namjul
Copy link

namjul commented Feb 19, 2021

I have recreated it in lua for anyone that is interested.

local cmd = vim.cmd
local fn = vim.fn

function _G.alignMdTable()
  local pattern = '^%s*|%s.*%s|%s*$'
  local lineNumber = fn.line('.')
  local currentColumn = fn.col('.')
  local previousLine = fn.getline(lineNumber - 1)
  local currentLine = fn.getline('.')
  local nextLine = fn.getline(lineNumber + 1)

  if fn.exists(':Tabularize') and currentLine:match('^%s*|') and (previousLine:match(pattern) or nextLine:match(pattern)) then
    local column = #currentLine:sub(1, currentColumn):gsub('[^|]', '')
    local position = #fn.matchstr(currentLine:sub(1, currentColumn), ".*|\\s*\\zs.*")
    cmd('Tabularize/|/l1') -- `l` means left aligned and `1` means one space of cell padding
    cmd('normal! 0')
    fn.search(('[^|]*|'):rep(column) .. ('\\s\\{-\\}'):rep(position), 'ce', lineNumber)
  end
end

@rodhash
Copy link

rodhash commented May 23, 2022

local cmd = vim.cmd local fn = vim.fn function _G.alignMdTable() local pattern = '^%s*|%s.%s|%s$' local lineNumber = fn.line('.') local currentColumn = fn.col('.') local previousLine = fn.getline(lineNumber - 1) local currentLine = fn.getline('.') local nextLine = fn.getline(lineNumber + 1) if fn.exists(':Tabularize') and currentLine:match('^%s*|') and (previousLine:match(pattern) or nextLine:match(pattern)) then local column = #currentLine:sub(1, currentColumn):gsub('[^|]', '') local position = #fn.matchstr(currentLine:sub(1, currentColumn), ".|\s\zs.") cmd('Tabularize/|/l1') -- l means left aligned and 1 means one space of cell padding cmd('normal! 0') fn.search(('[^|]|'):rep(column) .. ('\s\{-\}'):rep(position), 'ce', lineNumber) end end

Thank you sir!

Recently moved all my config to lua, u know just playing around and getting to know it .. and this amazing piece of code was one of the missing parts on my new setup. Seems to work just fine.

@Harsha9554
Copy link

@namjul hey thank you so much, worked like a charm.

@rockyzhang24
Copy link

@namjul it works perfectly. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment