2

Is there a way to clear the shell screen everytime before running a terminal command within vim?

This would be most useful to me when running scripts while editing it as I have a binding for Python scripts (for now; looking for a general solution though).

Currently I resort to :!clear && !<command> however I would like this to be automatic.

How can I do this. Maybe remap ! to !clear && !?

Update: So it turned out that I already had a solution in my vimrc for my specific case of running python scripts. I already had a !clear as part of the autocmd.

However, the question remains open for a general solution in which the screen is cleared before every terminal command executed particularly basic utilities like grep & ls which I prefer to execute in the terminal as I have color highlighting which is not present in vims' version.

1

AFAIK you can't do this with an autocmd since the only events that deal with shell commands occur after the command executes. I can't think of a better way to do it than your solution. We can't easily overwrite it with command! since ! isn't a valid command name (It would also might not be wise to completely remap it), but we can use a cabbrev. Here's one way to do it:

cnoreabbrev <expr> ! (getcmdtype() == ':' && getcmdline() =~ '^!$')? '!clear && !' : '!'
  • getcmdtype() == ':': Only works in for an Ex command (not in searches for example).
  • getcmdline() =~ '^!$': Only works at the beginning of the command line.

See :help :cnoreabbrev, :help getcmdtype(), and :help getcmdline() for more info.

  • I'm not sure, but some creative use of 'shell', 'shellcmdflag', or t_ti/t_te might work. – Mass May 29 '18 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.