3

I'm watching Gary Bernhardt's Destroy All Software screencasts, specifically A Compiler from Scratch. There, he runs shell commands without leaving Vim. I'm almost sure he's just using normal Vim syntax, for example on the 1-minute mark: :!chmod u+x compiler.rb. If I do that on my computer, the window will switch to execute the shell command and then I have to press Enter to go back to the editor. This is, I think, the normal behavior for Vim, and other people have asked this before, but I don't think any answers do things the way Gary does.

I looked at his .vimrc file, specifically the last version before the screencast was uploaded, but couldn't figure out what he might have changed to get the behavior I'm looking for. I downloaded it but I'm still not getting the right behavior, so I'm obviously missing something. None of his plug-ins seems to be related to this, so I've mostly ignored them.

I'm almost sure that I'm not looking for a solution that uses tmux, or things such as :split, :term, :below terminal, or :silent. Nor am I looking for an answer that uses something other than Vim, I believe Gary is just using Vim and a few plugins.

I watched Gary's screencasts where he specifically talks about Vim but he didn't address this question (or I didn't catch it), so if someone has figured out how to use :! <shell command> without leaving the text editor, I'd really appreciate it if you can help me work this out. I've tested the options I mentioned before both on Mac and Linux but nothing quite worked how I'd like it to, and I'd be happy with a solution that works on either platform.

7
  • Welcome to Vi and Vim! I think "Difference between :!{cmd} and :w !{cmd} regarding alternate screen" might cover at least part of it... Take a look at the answer and comments on that question, you might find that useful... Not sure if that will cover everything though, the video is quick, but I have the impression not to see the "Press ENTER prompt", not sure how to get that part of it...
    – filbranden
    Commented Dec 13, 2020 at 1:48
  • If you can find the timestamp or link to the part of the video that you saw this, that would help me. (I didn't find anything directly in the vimrc, but there are a lot of other files to configure vim! I didn't dig through all of them. It could also be the t_ti/t_te stuff to not clobber the screen having an unintended side-effect.)
    – D. Ben Knoble
    Commented Dec 13, 2020 at 2:22
  • @filbranden, thank you! I checked the link but I think the main goal here is exactly what you say about "Press ENTER". What I'd like to do is avoid having to go through pressing ENTER to go back to the editor.
    – David
    Commented Dec 13, 2020 at 2:23
  • 2
    I did see Press ENTER, just not in the alternate screen. I suspect the ti/te hack has something to do with this
    – D. Ben Knoble
    Commented Dec 13, 2020 at 3:04
  • 1
    @D.BenKnoble, and I think you're right! I tried :set t_ti= t_te= and does seem to do the trick
    – David
    Commented Dec 13, 2020 at 3:24

1 Answer 1

4

The video goes by quickly, but I paused it and I seem to have seen him type the following:

:!chmod u+x %

Which then produces this output:

:!chmod u+x compiler.rb
[No write since last change]
"compiler.rb" 5L, 42C
Press ENTER or type command to continue

So, actually, Vim does prompt to "press ENTER" after the command is complete, but it seems he pressed it so quickly (maybe he was expecting it already? or maybe he started pressing the following commands?) that it seems that message was skipped.

There are two settings that are relevant here, the first to prevent switching from the alternate screen while running the command and the second to automatically reload the file once it's changed by the external command (even though only permissions changed.)

Looking at his GitHub dotfiles repository, we can actually see both settings in the vimrc as of 2017-06-29 (which is when that video was published.)

  1. Configuring Vim not to switch from the alternate screen is configured with this snippet:
" Prevent Vim from clobbering the scrollback buffer. See
" http://www.shallowsky.com/linux/noaltscreen.html
set t_ti= t_te=
  1. Configuring Vim to automatically reload the file after an external operation is done with this setting:
set autoread

With these two settings on, I can almost exactly reproduce the same.effect, except that I don't get the [No write since last change] message. I'm not really sure where it's coming from, since as far as I can tell he had just written the file right before the chmod command. (As evidence, the status line doesn't show a [+] next to the name of the file, which would denote a buffer that's been modified.)

Perhaps it's something else from his vimrc... In theory, it should be possible to get it from GitHub and try the same. It seems that video is using Vim 7.4, so that might be part of it also.

Still looking at his GitHub dotfiles repo, it looks like he went back on allowing Vim to clobber the scrollback buffer, so he changed his mind on that part.

If that part still interests you, you can take a look at plug-in vim-altscreen, which implements that behavior, with some controls around it. See also the discussion on question "Difference between :!{cmd} and :w !{cmd} regarding alternate screen", in particular @user938271's exceptional answer.

1
  • 2
    Thank you for the thorough answer! I just tried :set t_ti= t_te= and :set autoread and I get the behavior I was looking for. I'm using Vim 8.2 and it seems to work fine as well. Thank you again! :)
    – David
    Commented Dec 13, 2020 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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