Move section to new file in vim - vim

I have a section in file1 which should be moved to file2. Normally I'd do this by visually-selecting the block, deleting it, :wq from file1, open file2, then paste.
Is there an easy way to move a block of text from file to file2 without closing vim?

Alternatively:
write selection to new file:'<,'>w file2
then reselect and delete with gvd
The first step is covered in vimtutor Lesson 5.3: SELECTING TEXT TO WRITE.

You could open your new file in a split using :sp newfile and then delete the block as normal from your first file. Then, change to the other split with ctrl w and w. Then use p to put the deleted content into the other file.

You could open the file in a new buffer.
just open the file via :e file2 and paste the text. To move quickly between the buffers use either :e # or :b #
see :help buffers for more information

Since no one mentioned that: you can use tabs instead.
Select your block.
Delete it with d
Create a new buffer in new tab with :tabnew newfile.name
Paste it and save it with p and :w
You can go back with gT or close current tab with :q
I personally newer use buffers -- only tabs. Read more about them in :help tabpage

Related

Vim's :open does not tab complete, what does?

Instead of tab completed file or directory names, I see ^I
:open ./lib/^I^I^I
Is there another command that would offer tab completion, or perhaps another solution?
I'm considering replacing the following mapping with something that would use buffers, but want to keep tab completion.
map <C-O> :tabnew ./
That might be because :open is not the command you're looking for. Try :edit or :e for short. Also try :help :open and :help :e to see, what the commands do. :e has tab completion.
Use :enew if you want to create a new empty buffer.
Use :edit filename if you want to edit a specific file in place of the current one.
Use :new if you want to create a new empty buffer in a new horizontal split window.
Use :split filename if you want to edit a specific file in a new horizontal split window.
Use :vnew if you want to create a new empty buffer in a new vertical split window.
Use :vsplit filename if you want to edit a specific file in a new vertical split window.
Use :tabnew if you want to create a new empty tab.
Use :tabedit filename if you want to edit a specific file in a new tab.
Use :help :command if you are unsure about its usage.
:set wildcharm=<tab> for tab completion.

Open last closed file in a new tab in Vim

I know I can open the last closed (:q) file in Vim by using :e#. This will open the file in the current tab. How would I perform the same task but open the file in a new Vim tab. Also I am interested in how to open the file in a new split instead of the current tab.
# is simply an Ex special character that will be replaced with the name of the alternate file. Do an :ls, and the alternate file will be marked with a # there also.
# can similarly be used with :tabnew and split. In the examples below I'll use :tabe in place of :tabnew as :tabe is a shorter alias for :tabnew (search for either in the help docs):
To open the alternate file in a new tab: :tabe#
To open the file in a new split: :split#; this can be abbreviated to :sp#, and :vsp# for a vertical split.
Using a buffer number from :ls, e.g. buffer number 5 you can also:
open the buffer in a split with :sp#5; alternately :sb5 if the switchbuf option contains the newtab specifier - see :help switchbuf
open the buffer in a vertical split with :vsp #5 (there is no :vsb)
open the buffer in a new tab with :tabe #5
You don't necessarily have to leave normal mode to open the alternate buffer in a new window:
CTRL-W ^ opens the alternate buffer in a horizontal split.
CTRL-W T opens the current buffer in a new tab (Shift-T, that is).
So, one solution to your title question is the following combo.
CTRL-W ^, CTRL-W T: opens the alternate buffer in a new tab.
Note that for the caret "^" in the first command you don't have to release the Control key and you don't have to press Shift, just hold down CTRL then strike W and 6 (where the caret is located on many English keyboard layouts).

Command T to open file in previously opened buffer

I'm using Janus MacVim by Carlhuda, and I wonder if there's a way to tweak Command-T to open a file (buffer) only once, instead of into multiple splits of the same buffer.
Eg: Assumming your directory/project has two files: A.txt and B.txt.
1) Cmd T, then select A.txt.
2) Work on A.txt, then Cmd T, split B.txt with Ctrl V.
3) Work on B.txt, then need to switch back to A: Cmd T, A.txt. Currently Command T would either open A buffer to current split, or create a new split of A. What I want is that the previously opened A buffer would be active again (the cursor would jump back to A) instead of a new split A got created.
So essentially if a buffer has already been opened, resume to that split buffer. Is there a tweak or shortcuts for this?
You probably want :drop or :tab drop instead of the default :tabe for opening files in the Command-T search buffer. This is configurable in your .gvimrc file:
function! CommandTAcceptSelectionTab()
ruby $command_t.accept_selection :command => 'tab drop'
endfunction
This one bothered the heck out of me, too!
There is a 'switchbuf' option but that works only for :sbuffer and few more commands but not for :split, :new and others.
As far as I know it needs some vimscript woodoo, which I used some time ago but do not use anymore and just use :sb with completion.

how to copy from other file in vi editor

how to copy from other file with ctrl-c and in vi editor with p yank (paste), i have no idea!!
thinks
You can also do: :r filename
This will pull in the file. It can also be used for output from a command:
:r! grep some_text file
:r! which perl
The other way is to use buffers.
:e other_file.txt
Once you yank, you can :bn to switch to the other buffer and paste
If you are just concerned about pasting, ctrl-v or shift+insert also work to paste the contents of the clipboard.
Once you have yanked (copied) text in vi, you can type <ESC>:e filename to open another file for editing. Your yank buffer will still be the same, letting you paste into the other file. You must be copying text from one file in vi into another file in vi.
If you want to paste text from outside of vi, you need to setup your terminal and vi specially to allow that.
You can't. The yank buffer is private to vim, not shared with the system clipboard.

Diff two tabs in Vim

Scenario: I have opened Vim and pasted some text. I open a second tab with :tabe and paste some other text in there.
Goal: I would like a third tab with a output equivalent to writing both texts to files and opening them with vimdiff.
The closest I can find is "diff the current buffer against a file", but not diffing two open but unsaved buffers.
I suggest opening the second file in the same tab instead of a new one.
Here's what I usually do:
:edit file1
:diffthis
:vnew
:edit file2
:diffthis
The :vnew command splits the current view vertically so you can open the second file there. The :diffthis (or short: :difft) command is then applied to each view.
I would suggest trying :diffthis or :diffsplit
When you have two files opened in vertical splitt, run
:windo diffthis
The content of all tabs are inside the buffers.
Look at the buffers:
:buffers
Find the right number for the content which should be diffed with your current tab content.
Open the buffer inside your current tab (f.e. buffer number 4)
:sb 4
Or do for vertical view:
:vertical sb 4
Then you can simple diff the content with
:windo diffthis
If you finished diff analysis you can input:
:windo diffoff

Resources