Select text within terminal output using VIM - linux

Let's say I have the following output in my terminal window now:
1. vsud#vsud:~$ pwd
2. /home/vsud
3. vsud#vsud:~$ echo "Hello, world!"
4. Hello, world!
I want to copy to clipboard the 1st and 2nd lines.
vsud#vsud:~$ pwd
/home/vsud
With mouse - I can do that by selecting those 2 lines and pressing Ctrl + Shift + C.
But can I enter into VIM mode somehow and work with that whole output in my terminal as with pure text - so to copy 2 lines I will need to type just "H2yy" ?
Do I need to install some specific Terminal Emulator for that?
Now I'm using terminal emulator which is built in XFCE.

There are two ways to do that: (1) Using tmux, and using (2) ':term' inside vim
tmux
If you are using your terminal from within tmux, you can enter copy mode and select and copy lines from your terminal. Assuming tmux Prefix is Ctrl-b, the precise steps to do this are:
Enter copy mode: Ctrl-b [
Start selecting: Space
Extend selection: use arrow keys
Store selection in buffer: Enter
Transfer selection to system clipboard: Ctrl-b : run-shell "tmux save buffer - | xsel -i -b"
Paste the selection into a vim buffer(+ register): "+p
Additional Notes:
a. tmux offers a command called "capture-pane", with which you can copy an entire pane(terminal).
b. You need xsel to be installed for step 5 above to work. Step 5 can be shortened by using a keybinding. For that have the following command in your .tmux.conf (or, entering this command on your tmux command line Ctrl-b :)
bind C-c run-shell "tmux save-buffer - | xsel -i -b"
Now step 5 is equivalent to pressing Ctrl-c.
c. You can use vim keys for navigation in copy mode: Ctrl-b :setw -g mode-keys vi
:term
If you fire terminal from inside vim using the ex command ':term', you can enter "normal" mode using: Ctrl-W Shift-n. Then you can navigate (and copy in) the terminal with vim keys as if it were a regular vim buffer.

You can pipe the output to vim using view -. For example:
ls -la | view -
This opens a vim instance in read-only mode which can read from stdin

Related

If I have vim open in two terminals, how can I share the copy/paste buffer?

I want to yy a line from one instance of vim running in one terminal emulator, and p that line into another instance of vim running in another terminal emulator. Possible? Is there a special setting to use?
There is a system clipboard which you can use to share data not only between multiple terminals, but also using shift + insert in GUI application.
First of all, check if system clipboard is enabled for your vim installation
vim --version | grep clipboard
if you see +clipboard you are good to continue, otherwise, follow this page to enable the system clipboard.
Once the system clipboard is enabled, you can copy line via "+yy into + buffer which is a system clipboard, and paste it in another terminal with vim with "+p command.
You can instead:
Esc for canceling all modes
:r! cat file.txt all content of file.txt pasts in your file, but if you want paste for example from 5 to 11 lines of file then
:r! sed -n 5,11p file.txt

Copy from vim to python console in tmux

I have installed tmux.
tmux -V
tmux 2.3
Set my configure file.
cat ~/.tmux.conf
set -g mouse on
Enter tmux and open a two vertical windows in it,open python3 console in the left,open vim in the right.
Now move my cursor at the beginning of the first line in the right with mouse.
Enter into normal mode and input 2yy+, to copy two lines in my + register.
Move cursor at the left python3 console window ,how can i paste content in + register into the python console?
#Kent,do as you say:
1.Move cursor at the beginning of first line,and type "+2Y
2.Move cursor to the left window,and middle-click mouse,nothing happen.
3.press ctrl+b then press ] key.
first your vim should be compiled with +clipboard see vim --version | grep 'clipboard'
To copy ( or delete ) in any vim register you can use the following syntex
"<register name><oprator><motion> (see :h registers )e.g.
"ayy(copy current line in register a) or
"bdd(delete current line in register b) or
"*ce(delete to the end of the current work and place content in register * using c will also put you in insert mode
to copy whole line you can use yy
and system clipboard is mapped to either + or * ( depending on the os )
so to copy the whole line into system clipboard you can use
"*yy or "+yy (depending on the os)
or to copy 2 lines
"*2yy or "+2yy ( to copy current and the line after current line )
once the content is copied in the system clipboard you can paste in tmux using ( command + v or ctrl + shift + v )
or to map system clipboard with tmux paste buffer see https://unix.stackexchange.com/questions/67673/copy-paste-text-selections-between-tmux-and-the-clipboard#72340
2yy+ does NOT copy two lines into + reg, instead, it yanks two lines to " reg, then moves the cursor to first non-blank char in next line
You can on the vim side do: "*2Y then do a mouse middle-click on the python console.
or simply select the lines you want to copy in vim by mouse, then middle click in python console
I didn't quite like with the accepted solution that it depends on a graphical environment for the clipboard since this does not work when vim can not access the clipboard, which is almost always the case for ssh connections.
So I cam up with another solution:
Instead of using the external clipboard vim can pass the text directly into tmux' paste buffer by piping it into
tmux load-buffer -
After that you can paste the content of the buffer with prefix + ] into the active tmux pane.
There are various ways to pass the text from vim to tmux:
# to write the current line into the tmux buffer:
:.w !tmux load-buffer -
# to write all *lines* within the visual selection into the tmux buffer:
:'<,'>w !tmux load-buffer -
# to pipe the content of a register (e.g. from a previous selection) into the buffer:
# #" being the unnamed register, #0 - #9 the numbered registers, and so on
:call system('tmux load-buffer -', #")
Using tmux paste-buffer you can even trigger the pasting into the correct pane at the same time:
# assuming the python pane is at :0.0
:call system('tmux load-buffer -; tmux paste-buffer -t :0.0', #")
You can now also easily map the last line to a key to send the visually selected text to the python pane.

How to bind a key to open the highlighted file with vim?

If I highlighted a filename in the copy mode, how could I open that file with vim by a hotkey?
I'm imagining something like:
bind-key -T copy-mode-vi 'C-o' send-keys -X copy-pipe-and-cancel "vim $(tmux paste-buffer)"
but that doesn't work, plus piping is redundant in this case.
I know there is tmux-open which should do similar thing, but it doesn't work for me somehow.
Environment: OSX 10.13.5, iTerm2 3.1.7, Tmux stable 2.7 built by homebrew
I normally have these settings in my vimrc
set hidden
set path+=**
Then you can just press gf when you cursor is over a word. If you want to open it on a new window, press Ctrl-w gf. If you just use gf you can go back by pressing Ctrl-6.

New lines being inserted at the start of the lines when pasted in vim (tmux)

I use Tmux to open my terminals . When I copy and paste in Tmux, new lines are being attached at the start of the lines. For example if I copy :
Line1 to be copied.
Line2 to be copied.
Line3 to be copied.
And if I paste the above text. It would become
Line1 to be copied.
Line2 to be copied.
Line3 to be copied.
I use it from mac. I select the text to be copied while pressing "alt" key and then paste it. Any suggestions on how to avoid the spaces at the start?
Note: A few more details about the environment.
I have a custom tmux.conf file. Following are a few setting I use in my tmux conf.
# more settings to make copy-mode more vim-like
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
bind -t vi-copy 'z' copy-pipe "reattach-to-user-namespace pbcopy"
# Buffers to/from Mac clipboard, yay tmux book from pragprog
bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
bind C-v run "tmux set-buffer $(reattach-to-user-namespace pbpaste); tmux paste-buffer")
Question looks like it has been already asked.
Turning off auto indent when pasting text into vim
Sum up:
Try typing:
:set paste
before pasting your clipboard and then return it to
:set nopaste
after, in order to resume normal formatting when entering newlines.
To make this automatic, you can use vim-bracketed-paste

Double Quote not working in Vim in command mode

I want to copy text from vim buffer to the system clipboard by using the command "*y but when I press the double quote sign in command mode, it doesn't seem to get fired at all as I don't see anything in the status bar (nothing gets copied also even if I blindly continue with the command). I can see all other commands in the status bar in vim. I'm using Ubuntu 12.04 and I have clipboard support enabled in my vim version.
You should press "+y in NORMAL (or VISUAL) mode.
If you love to do it in command line, do this:
:y +
(same for "*y)

Resources