Skip to content

Instantly share code, notes, and snippets.

@tweekmonster
Created May 18, 2015 17:53
Show Gist options
  • Save tweekmonster/86f043a07d68eaa062de to your computer and use it in GitHub Desktop.
Save tweekmonster/86f043a07d68eaa062de to your computer and use it in GitHub Desktop.
A script to truncate the_silver_searcher's output in Vim
#!/usr/bin/env python
'''Truncates matches from ag
Place this script somewhere in your $PATH, like ~/bin and pipe ag into it.
Vim could be setup like this:
if executable('ag')
set grepprg=ag\ --vimgrep\ -w\ $*
if executable('agtrunc')
set grepprg+=\ \\\|\ agtrunc
endif
set grepformat=%f:%l:%c:%m
command! -nargs=+ Ag exec 'silent! grep! <args>' | copen | exec 'silent /<args>' | redraw!
nnoremap <leader>a :Ag <c-r>=expand('<cword>')<CR><CR>
endif
'''
import sys
MAX_CHARS = 80
def truncline(line):
filename, lineno, col, text = line.split(':', 3)
if len(text) > MAX_CHARS:
cs = max(0, int(col) - 10)
ce = cs + MAX_CHARS
text = text[cs:ce]
print(':'.join((filename, lineno, col, text.strip('\r\n'))))
if __name__ == '__main__':
for line in sys.stdin.readlines():
truncline(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment