3

I find it annoying to constantly press Ctrl-Z and then fg to go between these screens (where command terminal is the command line that you used to invoke vim from). It also generates unwanted fg lines on my console. I'm pretty sure this can be done as I remember doing it when I was in university, but I can't remember how.

Information on Terminals and its Relation to vim

vt100 terminal and others have a way to save the screen prior to changing it and then it could restore it when it wants to. vim does this and can be seen if you go into vim and then shell or Ctrl-Z out. Most of the time it will show you what you had on the screen prior to entering vim by saving the screen prior to painting the vim screen. If your terminal doesn't support this functionality, it will just put the command line directly under the vim screen, scrolling it up. This can be seen by exporting to the TERM variable a less functional terminal emulation or clearing it (though by clearing it, it may put vim into line mode or may use the most basic of terminal codes depending on its implementation). To see more on saving/restoring a screen, see Terminal codes (ANSI/VT100) introduction, under the heading Save/restore screen.

  • 1
    Would setting HISTIGNORE to fg:bg be enough? (It would prevent these two commands from ending in your history, assuming bash.) Or is there something more that you find irritating? – dhag Mar 24 '15 at 16:06
  • I don't want it displayed on the screen. If I switch back and forth, the console screen will start to scroll and the thing that I'm looking at will move off page. – Adrian Mar 24 '15 at 16:48
  • Some terminal apps have a "second screen" that some programs, for example less, use. I have no idea, though, how to flip between them by keyboard command. Since I normally work with the gnome desktop, I use evim (because I'm a vim newbie but a GUI person since the late 80's), which, as another answer points out, works around the problem. – Joe Sewell Mar 24 '15 at 16:57
  • Its a workaround, but doesn't answer my question. :/ – Adrian Mar 24 '15 at 16:58
  • 1
    You could type (or create a macro that does) :silent !sleep 999999 . This will show your saved screen until you type Control-C, and (because of the :silent command) it won't print any Press ENTER to continue messages on the saved screen. A ^C will be printed, though. – Mark Plotnick Mar 24 '15 at 21:07
2

Starting gvim opens a new window, so the terminal window stays available for commands. For intermediate "shell escapes" I do :sh to get access to a shell session, and type <Ctrl>-D to exit it; no fg is needed, but a prompt is added in this case to the console shell. And with no X running I start two consoles, one where vim is running, and the other where the shell is running, and I switch consoles by (in my case) e.g. <Ctrl>-<Alt>-<F1>. (Depending on the actual needs, one of those options should fit; I hope.)

  • I don't use a graphical vim app. Just a terminal version. I've used this in uni 15-20 years ago, and though that was a long time ago, I don't think I'm going senile. ;) – Adrian Mar 24 '15 at 16:55
  • The graphical vim is actually just a normal vim (with only a few more convenient features). Myself being a IT-dinosaur, a vi enthusiast, and a terminal junkie, I think it's worth (if possible) to use gvim. You can, of course, also just start an ordinary vim in another window if you like. – Janis Mar 24 '15 at 17:01
  • Yeah, I'd rather not use a 2nd terminal for reasons I don't care to explain. I guess I could go back to screen, but that seems overkill. – Adrian Mar 24 '15 at 17:08
  • How do you think should vim accomplish the task of switching to the parent shell process and resuming? You need some controlling process to do so. Otherwise the options are as shown by exemple; controlling process is the window session or console manager, or the application spawns a new shell, or the shell's job-control interrupts the running editor and you resume. – Janis Mar 24 '15 at 17:16
  • IIRC, it switches by using terminal codes to switch between saved pages. See Terminal codes (ANSI/VT100) introduction under Save/restore screen. Most terminals have this functionality, and I think that is how vim does it so that you have access to the console view when you shell to it. I just want it to flip between these without having to cause the console screen to scroll. – Adrian Mar 24 '15 at 17:24
0

Does the Vim command :shell work as you want? :shell<return> causes Vim to run your $SHELL. When you exit the shell, you're back in Vim, right where you left off.

  • 1
    no, as I said, I want to toggle between the two screens without unwanted lines being generated in the console. – Adrian Mar 24 '15 at 15:57
  • 2
    @Adrian Most shells don't have separate "screens" unless you use something like tmux or a terminal emulator with tab support. Maybe your question is really "how can I run a command without its name being echoed"? – Anko Mar 24 '15 at 16:09
  • @Adrian - I don't understand what you mean by "Console" in that case. You'll have to use more standard terminology, as what you asked just plain doesn't make sense. "Console" has a somewhat specific meaning in the context of X11 interfaces, but as far as I know, "Console" doesn't mean anything specific with respect to vim. – Bruce Ediger Mar 24 '15 at 18:22
  • Sorry, I mean the terminal shell prompt. What do you mean by "running a command without its name being echoed?" – Adrian Mar 24 '15 at 19:06
0

Ok, so I've figured out how I can do this with Gary Johnson's help on the Google Groups vim_use group. He helped by stating how to run the tput commands from vim. Here is my final solution:

  1. Have the following function script in vim memory:

    function! ShowTerm()
        call system(">/dev/tty tput rmcup")
        call input("")
        call system(">/dev/tty tput smcup")
        redraw!
    endfunction
    
  2. Map the script to some command key sequence like this:

    map [= :call ShowTerm()<CR>
    

I used the sequence [=, but you can use whatever you want.

Now type the key sequence and you get the other screen. Press Enter and you're back to the vim screen. The Enter doesn't add a line feed to the terminal screen either, so no marching lines.

Nice! :) Note that this probably isn't the way it was done 15-20 years ago, but it works.

EDIT

In order not to be dependent on tput being available, I've found that the following that @MarkPlotnick inferred works just as well. Just replace the function ShowTerm() above with:

function! ShowTerm()
    silent !read -sN 1
    redraw!
endfunction

This depends on bash being used as the shell (or others that have a similar read call).

2nd Edit

Further, you can have this automated by putting a .vim file into your ~/.vim/plugins folder that contains the following:

" Function that allows viewing command line display last time it was seen.
" Note: Will only work on terminals that allow saving/restoring the display.
function! ShowTerm()
    " This will invoke the command shell and call the read function.
    " Will exit when a key is pressed.  This is specific to bash and
    " and may have to be changed if using a different shell.
    silent !read -sN 1
    redraw!
endfunction

" Maps [= sequence to view command line display last time it was seen.
map [= :call ShowTerm()<CR>

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.