Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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