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

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.

Related

What happens when I execute vim remotely over ssh? [duplicate]

I have a remote file that I edit regularly. I would like to edit it with a quick, simple command that would work likely via SSH. At present, my workflow is to connect to the remote computer via SSH, open the file using an editor (say vim or nano), edit, save and then close the connection.
I am aware that I can mount the remote computer filesystem using SSHFS or Nautilus capabilities, but I'm really looking for a single command to run in the terminal which shall open the file in an editor, allow me to save and then exit, closing all connections to the remote computer.
Currently, I am trying to do this by passing a command to the remote computer via SSH, but I am running into difficulties. For VIM, the command is something like the following:
ssh user1#computer1 "vim /path/laboratory_notebook_1.md"
Using this procedure, VIM does not run correctly and presents the following error:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
For nano, the command is something like the following:
ssh user1#computer1 "nano /path/laboratory_notebook_1.md"
Using this procedure, nano does not run and the following error is presented:
Error opening terminal: unknown.
I'm not sure how to proceed on this line of thought. I would appreciate assistance on this method and suggestions on other ways to edit remote files briskly with a minimum amount of interaction.
Force Pseudo-TTY Allocation
You can force pseudo-tty allocation with one or more -t flags. The SSH(1) man page says:
-t Force pseudo-tty allocation. This can be used to execute arbi-
trary screen-based programs on a remote machine, which can be
very useful, e.g. when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.
Example
Using your own example, slightly modified, the following would work if you have a local TTY or PTY:
ssh -t user1#computer1 'vim /path/laboratory_notebook_1.md'
It works fine for me with OpenSSH_6.2p2. Your mileage (and operating environment) may vary.
If you are using vim. Vim comes with a plugin called netrw which will allow you to do this.
vim scp://hostname/path/to/file
Will copy the file to you local machine and on save reupload it.
Take a look at netrw's documentation :h netrw

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

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]

What is a quick way to edit a remote file on Linux?

I have a remote file that I edit regularly. I would like to edit it with a quick, simple command that would work likely via SSH. At present, my workflow is to connect to the remote computer via SSH, open the file using an editor (say vim or nano), edit, save and then close the connection.
I am aware that I can mount the remote computer filesystem using SSHFS or Nautilus capabilities, but I'm really looking for a single command to run in the terminal which shall open the file in an editor, allow me to save and then exit, closing all connections to the remote computer.
Currently, I am trying to do this by passing a command to the remote computer via SSH, but I am running into difficulties. For VIM, the command is something like the following:
ssh user1#computer1 "vim /path/laboratory_notebook_1.md"
Using this procedure, VIM does not run correctly and presents the following error:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
For nano, the command is something like the following:
ssh user1#computer1 "nano /path/laboratory_notebook_1.md"
Using this procedure, nano does not run and the following error is presented:
Error opening terminal: unknown.
I'm not sure how to proceed on this line of thought. I would appreciate assistance on this method and suggestions on other ways to edit remote files briskly with a minimum amount of interaction.
Force Pseudo-TTY Allocation
You can force pseudo-tty allocation with one or more -t flags. The SSH(1) man page says:
-t Force pseudo-tty allocation. This can be used to execute arbi-
trary screen-based programs on a remote machine, which can be
very useful, e.g. when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.
Example
Using your own example, slightly modified, the following would work if you have a local TTY or PTY:
ssh -t user1#computer1 'vim /path/laboratory_notebook_1.md'
It works fine for me with OpenSSH_6.2p2. Your mileage (and operating environment) may vary.
If you are using vim. Vim comes with a plugin called netrw which will allow you to do this.
vim scp://hostname/path/to/file
Will copy the file to you local machine and on save reupload it.
Take a look at netrw's documentation :h netrw

How to activate an existing GVim window

What I'm searching for is a command similar to:
gvim --remote-silent FILE
But without the FILE part. So basically: launch a new GVim instance if none exists, and otherwise activate/focus the existing window. I prefer not to use a dummy file (eg. gvim --remote-silent ~/.vimrc) as that would mess up my bufferlist/MRU.
The reason I'm looking for something like this, is that I'd like to configure a shortcut for GVim (Mod+8) to use in the Linux XFCE window manager.
You can re-activate an existing GVIM instance with
$ gvim --remote-send ":call foreground()<CR>"
That fails when there's no existing server. In that case, just spawn a fresh instance, or check beforehand with gvim --serverlist.
In Ubuntu gvim --remote-silent works but doesn't activate editor window and just waves its icon in sidebar. To change this behavior look here.
This behaviour can be changed from the command line, open a terminal
and type:
dconf write
/org/compiz/profiles/unity/plugins/core/focus-prevention-level 0
To restore the default value:
dconf write
/org/compiz/profiles/unity/plugins/core/focus-prevention-level 1

Resources