Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip escape sequences in diff output #131

Open
zharmany opened this issue Apr 30, 2020 · 3 comments
Open

Strip escape sequences in diff output #131

zharmany opened this issue Apr 30, 2020 · 3 comments
Labels

Comments

@zharmany
Copy link

Hi there,

I've been recently getting odd errors when stripping whitespace upon save. I eventually diagnosed this as being related to setting my Vim shell to be a bash login shell, via let &shell='bash -l'.

What happens is that during the course of sourcing my dotfiles for my fancy prompt and such, the tput sgr0 command is issued, which causes the printing of escape characters that reset any color / styling effect for text.

This messes up the output of the diff command in vim-better-whitespace, the line being

silent! echo system(s:diff_cmd.' '.shellescape(expand('%')).' -', join(getline(1, line('$')), "\n") . "\n")

For now, I'm just going to comment out the tput sgr0 line in my .bash_profile and hope for the best, but I'm wondering if there's a more robust way to get the diff output (avoiding the shell altogether, or strip the escape sequences). I do realize that this more of a me problem than a you problem.

@Cimbali
Copy link
Collaborator

Cimbali commented May 3, 2020

Hi @zharmany, I think that’s a tricky problem. Do you get it only with better whitespace or with all system()-based commands?

The g:diff_binary option can maybe help you here, it’s what’s used to build the diff command. Maybe setting the path to diff directly helps?

let g:diff_binary='/usr/bin/diff --color=never'

Otherwise you can maybe use it to insert something before the diff command that clears up any garbage thrown out by your login script? Not sure how to go about that though.

Also note that the diff command is only used to strip whitespace on modified lines only. You can disable that option and better-whitespace will strip all lines of the file.

@zharmany
Copy link
Author

zharmany commented May 4, 2020

Thanks for the reply.

In my situation, it was with all system() commands -- as it was capturing the output of something in my .bash_profile prior to capturing the output of the actual command. E.g., even echo system('command pwd') would wind up capturing extra escape characters.

With that said, I don't think changing the diff_binary would help, because it's the output prior to any diff command.

There's probably a few different ways to solve this, but I'm not versed enough in Vimscript to know what would be the appropriate way to go.

Thanks for the suggestion about modified lines, but that's precisely one of the awesome features I like :). I work on a shared codebase with folks that are not as persnickety as me, so I'd be making all sorts of whitespace changes in the codebase.

@Cimbali
Copy link
Collaborator

Cimbali commented Jul 17, 2020

I think the only way to fix this is to make your .bash_profile entirely silent.

You can probably make it silent only for non-interactive shells and let it print as usual for interactive sessions.
So for example, replacing tput sgr0 with [[ $- == *i* ]] && tput sgr0'. Also you maybe don’t need to set your fancy prompt at all in a non-interactive shell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants