Exit Vim without clearing the terminal before - vim

Every time after I exit Vim, it looks like the "clear" command is called automatically in the shell. Everything left on the screen is a new command line for input.
More exactly, it seems that Vim shares the same terminal with shell. After exiting Vim, the shell commands and their output are pushed pages up, and a "clear" must be called, because otherwise the content in Vim will remain on the screen.
With man and less, there is no such problem. After exiting, the screen returns what it is before running man and less. What can I do so that Vim could act like this?

You can edit ~/.vimrc and add into it set t_ti= t_te=.

Related

How to scroll within vim's shell command output?

When I enter a shell command via vim -- e.g., :!rake routes -- I'll get some output and then:
Press ENTER or type command to continue
If I press anything at that point, I am always taken back to the main vim UI. How do I stay in that shell mode for a bit longer, specifically I can scroll back to see all the output of the command?
For terminal vim scrolling is provided by terminal emulator (or terminal multiplexer like tmux/screen if you use it). I.e. if your terminal scrolls when using <S-PageUp> it must also scroll with Vim’s !. Same for mouse wheel (it works for me even if vim was configured to handle mouse on its own).
If have terminal vim and you need to view output after you pressed <CR> then you can use <C-z>/:susp. Both these capabilities will be spoiled with incorrect configuration of either vim or terminal (terminal multiplexer) (i.e. terminal configured not to support alternate screens (which is normally issue for terminal multiplexers rather then terminal emulators: screen requires altscreen on in .screenrc) or vim setting telling vim what to output to the terminal on startup/resume and shutdown/suspend was spoiled).
If you have GUI vim your options are very limited. It is better to follow link provided by #glts or #EricAndres advice in this case as I do not use GUI vim.
:r! [shell command] will read the output from the command into the buffer. So if you open a new buffer then use that command, you can search and scroll through it all you like. See Vimcasts episode 57 for more information.

Interactive terminal in VIM

I'm using Vim for editing source code, but I would also like to have a terminal embedded in vim's window (just like in Kate, you know).
Now I have seen the vimsh plugin that turns a vim buffer into an interactive terminal, but I don't like 2 things about it:
It opens automatically at startup. Can I disable it and invoke the terminal with a special command whenever I wish?
It splits the window in two and occupies the top window, but I would like it to occupy the bottom window. Can this be arranged?
PS: I'm not exactly a vim guru :)
Maybe this is what you want: Conque Shell - VIM Plugin
There's also an older patch that you can apply. It requires recompilation of the VIM source code though.
http://www.wana.at/vimshell/
Maybe I am not going to reply exactly to your question but I'll propose anyway a different approach on working with Vim and the terminal.
The first approach is to run shell commands directly from vim in command mode prepending them with a "!":
:!ls
will run the shell ls command and display you the output in a temporary window. This is useful if you just want to run a single or few commands.
If you want to mess around longer I suggest to suspend your vim session with Ctrl-z, work in the shell and issue fg as your last command to get back to vim.
Hope it helps you.
Another option you could try is using tmux/screen to split your terminal, so that you can then run vim in one pane and have your shell in another. I also liberally use ControlZ to drop into a shell from vim and then fg to get back to vim after finishing with the shell.

after quitting VIM editor I am not able to see the original screen contents that were present before entering vim

I want to see the original contents of screen after quitting vim
as they were before opening a file , as of not my file quits but the original display is not there
THanks
The feature of returning screen contents after running a full screen application vs, leaving the contents there, is not specific to vi, but to your terminal emulator. The feature you want to turn on to return to the previous text is often known as 'altscreen'. If you are using xterm as your terminal emulator, this behaviour is default. However if you are running GNU Screen inside of an xterm (or other terminal), you need to add the line
altscreen on
to your ~/.screenrc file. Other terminals that support this feature will have other mechanisms to turn it on and off.
Instead of quitting, you can put vim into the background by typing control-z. This restores the previous screen, but leaves you the editor running 'stopped' with the current file. To get vim back, enter the command
fg %1 at the shell prompt. This brings vim back to the foreground again - at least assuming you only have one stopped job. The command jobs will give you a list of stopped jobs, which you can access by number.
So the work sequence becomes edit, save, control-z, compile, test, fg...
This works on linux, and Mac OS X - YMMV on other Unix variants.
If you are using xterm, then see :help xterm-screens, or ... read that anyway as it describes that your problem should be related to some terminfo setting - probably.
HTH

How to temporarily exit Vim and go back

How could I exit Vim, not :q, and then go back to continue editing?
Assuming terminal Vim on a flavor of *nix:
To suspend your running Vim
Ctrl + Z
will suspend the process and get back to your shell
fg
will resume (bring to foreground) your suspended Vim.
To start a new shell
Start a subshell using:
:sh
(as configured by)
:set shell?
or
:!bash
followed by:
Ctrl+D (or exit, but why type so much?)
to kill the shell and return to Vim.
You can use :sh to exit to your default shell then typing $ exit at the shell prompt will return you to Vim.
You can switch to shell mode temporarily by:
:! <command>
such as
:! ls
You can also do that by :sus to fall into shell and back by fg.
If you frequently need to go back and forth between shell and vim, probably what you really want is have only one vim instance in the shell, and use it to open any file in the workspace. If so, check this question. Once you set it up correctly, you can :sus or C-z to return to the shell, then just v or v <newfile> to get back to vim.
And my answer is almost my daily routine.
If you are on a Unix system, Ctrl + Z will suspend Vim and give you a shell.
Type fg to go back. Note that Vim creates a swap file while editing, and suspending Vim wouldn't delete that file (you aren't exiting Vim after all). On dumb terminals, this method was pretty standard for edit-compile-edit cycles using vi. I just found out that for me, gVim minimizes on typing Z.
If you're using Neovim, you can do the following:
:terminal command to bring up a terminal window.
Do your terminal stuff
Type exit to kill the terminal process
Press any key to return to Neovim
Just put in fg and go back to your most recently suspended program.
There are several ways to exit Vim and have everything the same when you return. There is very good documentation within Vim itself explaining the various ways this can be done. You can use the following command within vim to access the relevant help page: :help usr_21
To give you a brief summary, here are the different methods of quitting and returning with your session intact:
Suspend and resume - You don't actually quit Vim with this; you simply hide your session in the background until you need it. If you reset your computer or issue a kill command to Vim, you will lose your session. This is good for when you want to switch to another task temporarily, but if this is the case, then you might want to look into using the GNU Screen utility instead.
Sessions - This is the true way of saving your session between instances of Vim. Even if you truly quit Vim, your session will be there for you when you return. This is probably what you are looking for.
To extend user Zen's answer, you could add the following line in your ~/.vimrc file to allow quick toggling between Bash and Vim:
noremap <C-d> :sh<cr>
If you don't mind using your mouse a little bit:
Start your terminal,
select a file,
select Open Tab.
This creates a new tab on the terminal which you can run Vim on. Now use your mouse to shift to/from the terminal. I prefer this instead of always having to type (:shell and exit).

how can I make vim send command to gnu screen session

I am trying to figure out how to script vim to send a message to a gnu screen "window". i.e. I have a screen session open and in one window I have a vim session and in the other a scheme interpreter. When I save my vim session I would like it to restart the interpreter in the other window loading in the new environment. I can figure out everything other than how to have an "on save" hook in vim send a shell command to another "screen window" causing the script to terminate and restart. if I could figure out how to send commands I could kill the process and then start a new one - I just need to make sure it starts in the right "window".
Have vim issue shell commands, and use screen -X to issue commands to screen. Some permutation of :at <other-window> stuff <restart-command>. See man screen's customization section for more commands.
For example, if I was in screen window 1, using vim, and I had an irb session in window 0, to restart the irb session, I would do
:!screen -X at 0 stuff exit^Mirb^M
(^M entered via CTRL-V Enter).
If it is sufficient for you to have the scheme interpreter run every so many seconds, you could just run watch /path/to/scheme/interpreter /path/to/scheme/file in the second screen window. Adjusting the time intervals in which watch runs commands can be adjusted using command line parameters. The watch man page contains the details.

Resources