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

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

Related

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]

How to make Vim open in a terminal instead of XQuartz when logged onto a remote server?

When I'm on my own computer's terminal, when I enter the command vim filename and vim opens the file in the terminal.
When I log into my school account using SSH, and I enter vim filename, nothing happens.
I assume it's trying to open it using XQuartz, which I recently uninstalled.
How do I make it open in the terminal window, like on my own computer?
I have tried not using -Y when logging on, but it doesn't make a difference.
Actually, there is a workaround. You can try to edit it using the scp option in Vim.
Just run vim scp://user#schoolserver.edu/path/to/your/file from your terminal and you'll start editing the file using Vim from your local machine.
If you want to learn more about scp in Vim type :help scp in Vim.

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).

exit or close the vim through shell script

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`

Resources