exit or close the vim through shell script - linux

I'm writing a script file in which I have to close the vim files that are opened. So, how can I close or exit from the file through the shell script? I've tried this solution, but had no luck. I've tried it this way:
vim path_to_file/abc +qa

In order to close an existing, running Vim from an external shell script, you either have to tell that Vim instance to quit, or let the operating system do the job.
communication with Vim
Vim has client-server functionality built in, see :help remote.txt. If you know the server name (you can get the list with vim --serverlist), you can send it commands. E.g. this tells the Vim instance named GVIM to quit:
$ vim --servername GVIM --remote-send '<C-\><C-N>:quitall!<CR>'
through operating system
Brute force: kill all running Vim instances :-)
$ killall vim
To find the Vim instance which has a particular file open, the lsof tool can be used. Because Vim only opens the file itself on :writes, we have to search for its (permanently open) swap file (i.e. .file.swp instead of file):
$ kill `lsof -t /path/to/.file.swp`

Related

Access VIM session started by another process - Script/SU/Programme

I have a challenge and was wondering how to complete it.
A process in the background would start VIM using "su - user1 -c vim file.txt" whilst logged into root. I from the user1 need to access that VIM session. I can't read the file.txt directly but the contents I need is in the VIM session.
Is there anyway to switch to a VIM process that is already running? I have tried killing the process to force it to dump to a swap file but that's not working well.
If your vim is compiled with the +clientserver option (check by calling :version from within vim) then you can open vim with a server instance using vim --servername FOO and then connect to that server in a separate terminal using vim --servername FOO --remote file.txt. You can even send a valid Ex command to the server using vim --servername FOO--remote-send ':g/Foo/d'
For your use case I imagine you'd want to send a command like :w !anyTerminalCommand > anotherFile.txt
For more information, checkout :h clientserver

Cygwin terminal input disappearing after quitting vim

Using Cygwin, I tried creating and editing a file in Vim:
touch test | vim
This is obviously a mistake; something like vim "$(touch test)" has a better chance of actually working. Nevertheless, this command throws the error:
Vim: Warning: Input is not from a terminal.
And after this, Vim opens and I exit the program with :q. Any subsequent commands I enter into the terminal are hidden from view until I restart Cygwin.
Why is this?
You don't understand what does a pipe | do in shell.
Pipe will take the pervious command's stdout as stdin to next command, in a subshell.
Your touch foo doesn't generate any output, what do you expect to happen? same for vim "$(touch test)".
If you want to create a file and open it in vim in one shot, you can try:
touch foo && vim foo
If you want to edit it with vim anyway, actually, you can simply just:
vim foo
then save the buffer after your editing.

open vim file in new unix terminal

How to open existing vim file from unix shell (bash) in new terminal (not in same/new tab of existing terminal) on local machine ?
Also is there any way to split file on new terminal (not in same/new tab of existing terminal) from inside vim ?
How to open a new terminal is platform dependent; This doesn't really have a whole lot to do with vim itself.
For example, if you're using GNOME you could do this by running gnome-terminal -e "vim $filename" & disown. Look up the documentation for the terminal emulator you're using to find out how to launch a new terminal and execute commands in it.
Another (IMHO much better) solution is to simply use GVim for situations like these, unless you have a very good reason to run vim in the terminal (if you're running this over ssh this won't work anyway, in that case you're better off using a terminal multiplexer like screen or tmux).
PS: bash isn't a terminal (emulator); bash is a shell. If you just run a new instance of bash it'll run in the same terminal, which is not what you want here.
Try this:
vim [your file]
If this isn't working for you, make sure you have it installed with:
sudo apt-get install vim
If you're already IN vim do
:edit [your file]

Freeing Terminal While Using gVim

Is there a way that I can free my terminal from running the gVim process w/o quitting gVim so that I can continue to use the terminal? I'd like to be able to
do something similar to what I do with emacs. With emacs I can either use the emacs [file] command to have the process run through the terminal, or I can use the runemacs [file] command to keep the terminal free.
I start gVim with the command:
gvim [file]
and then the terminal hangs until I :quit gVim. When I searched for an answer to this question on the web, people advised that the best thing to do was to use ctrl-z to suspend the vim process and then use fg to return. However, this fails to work for me in both command line and gVim mode. I'm using Git Bash for my terminal on Windows 7.
You could run gvim in background as any other process:
gvim [file] &
After executing this command you receive a message indicating the pid of the new process. When you end it you should receive a similar message on that shell.
Edit:
The ctrl-z/fg problem is probably related to windows. This question states that GitBash would create a new shell instead of returning to the current one, so it probably doesn't work as in Linux. A possible solution would be to run your commands from gVim, either calling the shell through :! on mappings, or plugins/commands (fugitive for git, :py or some plugin for python interpreter, etc).

How to check if it's still in Vim shell mode

In vim, type :sh will switch to shell, and exit can exit the shell and get back to vim. Is there any command to check if it's in vim shell mode? So that I won't accidentally vim to open the same file again. I want to avoid below scenario:
vim myfile > :sh > exit > vim myfile // get warning of another vim instance is editing the same file
These are the normal scenario:
vim myfile > :sh > exit // keep editing
vim myfile > :wq > vim myfile // keep editing
In addition to #a3nm answer, what you can do is
use pstree -h: it will output process tree with current branch highligted and all you need to check is whether there is vim in the highlight.
Another possibility is ps t: it will show all processes using the current terminal and should show vim in a list when you are inside :sh. ps -oargs t may be more useful in case you want to know arguments you ran vim with.
These methods are more reliable because VIMRUNTIME, VIM and MYVIMRC environment variables may be overrided by you in order to do some customizations (BTW, they are defined by vim for use in vimscripts, not by :sh). They also work for other processes that allow you to run a subshell, but do not define any environment variables.
I would also suggest to consider using <C-z> in normal mode or :suspend/:stop in Ex because these use shell vim was launched from instead of creating new. This behavior gives you access to history of commands you typed before running vim and also makes you able to write more complex and time-consuming shell configuration without needing to wait every time.
In case you use <C-z> both methods still work, but first method won’t highlight vim because it will be at the same level (has the same parent) as pstree itself, likely just below or above pstree in graph. This also enables third method: jobs shell builtin.
In order to restore from <C-z> you should use fg (a single % in zsh and bash also works) which is less to type then exit (but more then <C-d>).
The :sh command in vim seems to define the VIMRUNTIME, VIM and MYVIMRC environment variables, so you can just check if they are defined. To do so, you can run echo $VIM, for instance, which should return an empty line in a normal shell and something like /usr/share/vim in a shell run from vim.

Resources