How to tabedit a remote file in vim - vim

I have two files A and B. A is on my local machine and B is on a remote machine. I want to certain text from A to certain locations in B. I'm using vim. What is the best way to do this?
If both files were local, I would just do
vim A
:tabedit B
then I'd be able to easily switch between the two. Is there anything similar for remote files?

What you are looking for is included with netrw. Take a look at :h netrw and :h netrw-read. So you could just type the following to edit thats on the remote server.
tabedit scp://[user#]machine[[:#]port]/path
(A copy of the file is pulled to the local machine and then when you save it is uploaded to the remote)

Related

vim paste in byobu fails if origin file is not closed

I connect my server(centos 7) with putty, and run vim in byobu.
Now I have 2 windows in byobu, and two files, a.py in window-1 and b.py in window-2.
I tried copy some text from a.py to b.py, but ran into some problems.
If I follow steps below, it works fine:
STEP1: In window-1, open a.py, press yy, then quit with :wq
STEP2: In window-2, open b.py, press p.
But if I keep a.py, and b.py open at first , or doest quit a.py after yy, paste fails.
It seems quite odd, did missed anything?
I have set clipboard=unnamedplus in .vimrc, and have +clipboard in vim --version, my vim version is 8.2.1438.
You are able to paste across Vim sessions because of the [viminfo] file. By default, Vim stores the contents of non-empty registers (including the default register) into the viminfo file. That's why when you put (with p) right after you start Vim, you get the contents you last yanked (with yy) before you quit Vim last time.
The viminfo file is written right before Vim quits, and it's read during Vim initialization. That's why this won't work automatically if you start the second instance of Vim before you quit the first instance. Then the viminfo file will be read too early by the second instance (or written too late by the first instance) to allow you to put the yanked text across instances.
You can explicitly write the viminfo file with the :wviminfo or wv command. And you can explicitly read it with the :rviminfo or :rv command. So you could use these two commands in the two separate instances to transfer the register information through the viminfo file without having to quit them.
I have set clipboard=unnamedplus in .vimrc, and have +clipboard in vim --version.
If you're using the X11 clipboard integration, you could use that to share copied contents between Vim instances. Note that setting the 'clipboard' option only affects yanks, so to put from the clipboard register you'd have to use "+p.
But this assumes you have a valid X11 server to connect to, which is quite unlikely in your situation. First, since you're connecting through SSH using PuTTY, you would need to use SSH forwarding to export an X11 server from your client. Since you're using PuTTY, I'm assuming you're on a Windows box, which doesn't really run X11 by default, so you'd have to install one and run it (easier said than done.) Also, since you're running Byobu, you will also have trouble reconnecting to an existing Byobu session, which will quite likely be pointing at an invalid X11 server, using the connection information at the time the Byobu session was created, which may no longer match the new X11 forwarding connection information of the new SSH session... It'spossible to make this all work, but if you want my opinion, it's probably too much trouble, especially for this particular use case of two Vim instances in the same remote box.

Copy and paste selected text from vim file

I use the following command to copy the content of a text file (located in remote server) opened with VIM (Linux OS):
gg v shift+g
The text is selected and greyd out as seen in the screenshot
But I don't know how to copy it in clipboard in order to paste it later in another vim file (in my host machine)
If I understand you right, you are going in the wrong way.
I guess you opened the remote file via ssh. You cannot copy to your local clipboard directly over a remote ssh.
I would suggest that, you open the file from your local vim, via :e scp://server//path/file or open it in a split window in your local vim. Then you are free to yank/copy content from your remote file to your local vim file(s).

how to yank text from one instance of gvim to another using ex commands

So I have a lot of lines to copy from one file (open in one instance of gvim) to another file (open in another instance of gvim) and I'm using ex commands. So I tried doing the usual
:0,164ya
and then going to the other file and doing
:pu
and I also tried
:+0,164ya
and then
:+pu
but neither worked. I also tried using p instead of pu. Anyone have any idea on how to do this?
Use the "+ or "* registers:
:0,164y +
:put +
Vim doesn't sync its register contents across Vim instances. Therefore, you have to use a shared data store like the system's clipboard (register "+), or a separate file (:[range]w /tmp/file, followed by :r /tmp/file).
If you really need to transfer multiple register contents at once (where the approaches above become tedious), you have to explicitly sync the register contents via the viminfo file that persists them (among all other history).
In Vim 1:
:[range]yank a
:wviminfo
In Vim 2:
:rviminfo!
:put a

VIM: Overwriting system vimrc

I work on multiple MAC OS X systems, which do not save changes after log out. As you know VIM is on every new mac, just type in vim in the terminal. I always bring my vimrc file with me, and the problem is that every single time I start vim i have to load it with :so command.
I want to use the fact that vim is available on every unix, but I also want to take advantage of the nifty integration with the terminal for doing quick tests, I just switch back to the terminal, but for that I need to close vim. When I reopen it, I again have to load the vimrc. And I am a newb, I don't even have plugins yet...
I want to keep the integration with the terminal and only develop a super quick way of introducing my changes to vim. Think of the conditions as if though you are sitting on newly installed operating system.
Thanks !
Not directly to your question, but you can always invoke vim with -u, which will let you specify your vimrc file instead of launching vim and then running :so.
The default location for your .vimrc file is ~/.vimrc (on the mac, ~ is /Users/_you_, eg /Users/matt). If you can write your file there, it will be loaded when vim starts up every time.
The system vimrc file on the mac is at /usr/share/vim/vimrc, but it sounds like these systems are not under your control, so you won't be able to write that file. Have a look at: How can I override ~/.vim and ~/.vimrc paths (but no others) in vim?, which uses the -u option to change the path vim looks for plugins under. But, in all cases, you must either be able to write your .vimrc into your home directory (which it sounds like your system does not permit) or specify the path to it at runtime (as with the command-line option I mention above, or with the normal command :so which you're currently using).
Depending on the kind of testing you need to do, you can always run shell commands within vim, by using the ! in normal mode. For instance, I frequently make changes to a python file in a buffer, and then (in normal mode) run !nosetests within vim—that writes terminal output into a temporary buffer at the bottom, and doesn't require me to leave or suspend vim. I can review the output, and any key-press takes me back to my buffer.
I don't have any experience with Mac-Os terminal. However I think if you could cp your .vimrc file to your home directory. everytime you start vim, vim will load the .vimrc file from your home directory.
If you want to swtich back to terminal from vim to do some testing/execute some commands and back to vim. you could consider to:
open multiple terminal windows
try something like screen or tmux. personaly I am using tmux, and it's very nice.
try Conque Shell plugin: http://www.vim.org/scripts/script.php?script_id=2771 I have this plugin installed too.
type Ctrl-z in vim to back to terminal
If you want to sync your .vimrc on different machines, you could put your .vimrc file in
a scm repository like gitHub, bitbucket... (I perfer this option, since you could have different branches for different settings)
dropbox
I hope this helps.
How is it possible that your changes are not saved after you log out? What would be the point of such a machine? An internet kiosk in an airport? Do you log as a user without a "home" directory?
If you have a "home" directory, just create a blank ~/.vimrc and put your settings there.
If you don't have a "home" directory but you are able to write somewhere else, create a blank vimrc file where you can, write your settings there and learn this command by heart:
$ vim -u /path/to/your/vimrc
If you don't have a "home" directory and you are really sure that you can't save anything on these machines, put your settings in a file somewhere online, preferably a place under your control, and learn this command by heart:
$ vim -u http://domain.name/yourvimrc
If you are lucky, the command you use will be remembered by your shell for you and it will be easy to issue it again without much typing.
For running your tests, you can either:
Hit <C-z> to suspend Vim. You are back at the prompt from where you started Vim and you can do your thing. Type $ fg to go back to Vim.
Type :sh to launch a new shell from the current directory. To go back to Vim, type $ exit.

GVim - How to handle multiple files

Sorry to ask such a novice question but I am looking for a way to handle multiple files. I dont want to type huge file paths to open every file using :tabnew and :e commands
Fuzzy Finder is a handy plugin to quickly find and open files.
Basically you have to only type a few letters like test and you'll get a pop-up menu to open in your current path :
footest.c
bartest.h
footest.h
...
It is a bit slow when used on NFS but it is useful if you don't want to type long path and file names.
Alternatively if you don't want to use any plugin, by default gvim/vim includes a file browser called netrw.
To start it, just type :e . you'll get the content of your current directory, you can then navigate through the directory structure quite easily. (There is even commands to delete, rename, etc like a standard file explorer)
:help netrwfor more information.
A couple of tips that you might be interested in:
You can configure Vim so that the
current directory "follows" the
directory of the file you are
currently editing. That way you can
edit another file from the same
directory without having to type the
full path. This can be achieved by
putting either set autochdir or
autocmd BufEnter * lcd %:p:h in
your .vimrc
You can use wildcards with tab
completion. e.g. to edit
a_file_with_a_long_name.txt you could
do :e a*long and then press
Tab followed by
Return.
Usually, vim supports buffers for that. Use :badd to add buffer, :bdelete to remove it and :ls (or :buffers) to list all opened buffers. I believe, GVim supports these features too.
For example, if you wanna edit all .rb files in your app/controllers/pages dir (in the case of Rails project), you type vim app/controllers/pages/*.rb in your terminal and then edit the first file (buffer) in the vim window. When you've done with all changes, save changes as usual with :w (note: do not use q! option - this will close all your buffers you've opened) and then use :bn<tab> (or fully, :bnext) or :bprevious to switch to the next file (buffer). When you run :bnext on the last buffer, you'll be dropped to the first one.
You can open a directory in Vim, search for the file o directory you are looking for with '/' and type [enter] to open it.

Resources