Search across multiple lines and copy them to clipboard - search

I have multiple python scripts in my menu.vim file.
I want to copy them to clipboard.
All python scripts are like this one:
python3 << endpython
.......code......
endpython
I use this search command:
^python3\_.\{-}\_^endpython
It seems to me that it is the correct command even if does not highlight all lines (maybe because the scripts are longer than my screen display).
But how do I copy a multiple lines search to the clipboard?
Is this the way to go?
g/pattern/y A

You're right; the solution involves :global (to iterate over all matched ranges), and an uppercase register (to accumulate the ranges):
:let #a = '' | global/^python3/,/^endpython/yank A
If you need the result in the system clipboard, you can do this via
:let #+ = #a
Plugin alternative
If these multiple commands are too much of a hassle, my PatternsOnText plugin provides (among others) this shortcut command:
:YankRanges/^python3/,/^endpython/ +

You can use:
:g/^python3/,/^endpython/y +
I think the "missing part" here is that you can use the special + and * registers to interact with the clipboard. See :help quoteplus and :help quotestar.
Also see How can I copy text to the system clipboard from Vim? for a more in-depth explanation of Vim and clipboard interaction.

Related

SLES12 vim - background changes scrolling + copy paste issues

I am using gnome-terminal with SLES12 and I encounter an issue where I am scrolling down during showing file in vim, the background color is changing.
Using some exploration during the internet I got the following solution :
if &term =~ '256color'
" Disable Background Color Erase (BCE) so that color schemes
" work properly when Vim is used inside tmux and GNU screen.
set t_ut=
endif
But using this solution, it creates a new one.
When I copy paste a line from vim and paste it on other vim , the copy consider also the blank lines in as characters , and creates really long lines.
To emphasize, lets say I have the following line which contain 11 char (including the space)
the copy paste consider also the rest of the line (the blank ones) as characters.
I would like that the copy paste would stop at char 'd '
hello world
Any idea how to combine a solution for these two issues?
The two issues are completely unrelated.
When you "copy paste a line from vim and paste it on other vim", you are presumably using your terminal emulator's or system's copy/paste feature which have no idea about where what you consider as a line starts and where it ends. The terminal emulator's window is n characters wide so a line is n characters and that's all they care about.
A much better approach would be to use Vim's built-in :help y and :help p, which have the same idea of what a line is as you.
But this creates a second problem: the default Vim is generally not built with clipboard support so you can't really use yy to yank a line in one Vim and p to put it in another one. You will have to install a proper Vim for that: use your package manager for that.
Once you are set, you can yank to clipboard with "+y and put from clipboard with "+p. See :help registers for "+ and :help 'clipboard' if you would like to synchronise Vim's default register with the system clipboard.
That said, why don't you simply open those two files in a single Vim instance?

How to execute a list of vim commands on a text file?

I have a text that's around 1000 lines long, and I have a bunch of vim commands I want to apply to it. Lots of regex find-and-replace stuff. For example:
:%s/^[^\$\$]/def
:%s/\$\$/
%s/='/ = ['
I could copy and paste those commands one by one, but that's work I'll have to do again every time I receive a new version of this file. I'm wondering if there's a way to save a long list of commands that I can then apply to the file? Thanks!
registers
You can put them all into a register, either by typing those commands in a scratch buffer and then yanking into a register, or via explicit assignment:
:let #a = "%s///\n%s///"
Then, execute via :#a
Depending on your Vim configuration, the register contents may persist across sessions (cp. :help viminfo), but you must be careful to not override them.
scripts
A longer-lasting and more scalable solution is putting the commands into a separate script file, ideally with a .vim extension. You can put them anywhere (except ~/.vim/plugin, unless you want them executed automatically), and then run them via
:source /path/to/script.vim
custom commands
If you need a set of commands very often, you can give them a name and turn them into a custom command:
:command! MyCommands %s/// | %s///
Put this into your ~/.vimrc, or (if it's filetype-specific), make it a :help ftplugin.
Normally, you can run vim command from the shell using
vim -c YourCommandHere

vim: access to clipboard / pasting text from external clipboard

I've seen a guy (in a tutorial) pasting text, which he has copied (or yanked?) in a browser, in a file in vim. I was not able to reproduce this - I'm not very experienced in using vi or vim.
I can quote from this site:
Vim has extended vi to allow use of the * register as a reference to
the system clipboard. So we can use normal mode commands like: "*dd or
1G"*yG to copy things into the * register and "*p to paste text from
it.
So my question (slightly related is this one or this one, which I don't quite understand) is: how can I get access to any clipboard using vim in Fedora/Gnome?
:"*p
did not work for me,
:*p
neither.
Thanks!
First, check whether your Vim has support for this: :version must include +clipboard.
On Linux, there's a distinction between the primary selection (register *) and the system clipboard (register +). See :help quoteplus for details. Best select / copy something and check the output of :register *+ whether it's there.
Finally, the way to get and put things from / to the clipboard in Vim is through the register name. For normal mode commands, that means prefixing "+ to the p (put) or y (yank) command, e.g. typing " + P. There's also an Ex command for command-line mode (which you've attempted), but this takes the register as an argument (and without the "): :put +
VIM 8.1.2269 with +clipboard & +xterm_clipboard enabled.
I do / Ctrl+r +

how to copy/cut text from vim to system clipboard use line number

Be just like title,I know how to copy/cut to system clipboard "+yy,and it works! But my question is how can I use line number copy/cut section text,such as use :1,10y to yank 1 to 10 lines,can I use the same way to copy/cut to system clipboard,if can not ? Is there a simliar way for the same purpose ? (Actually,I have try to :1,10"+yy,but did not worked !)
You only need the "{reg} prefix for normal mode, to indicate that you want to specify a register. For Ex commands, you just append the register name.
:1,10yank +
Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor. :help :yank explains this:
:[range]y[ank] [x] Yank [range] lines [into register x]. Yanking to the
"* or "+ registers is possible only when the
|+clipboard| feature is included.

How do I repeatedly search & replace a long string of text in vim?

I'm aware of the vim replace command, which is of the form, eg:
:%s/old/new/gc
But what if either of these strings is long? How can I use something like visual selection mode, the clipboard or vim registers instead of having to type the old/new text in?
You can use q: to bring up a command-line window. This lets you use all the vim editing commands to edit the vim command line, including p to paste. So, you could copy the text into a register, paste it into the command line window, and execute it that way.
I recently discovered this feature via vimcasts.
According to the manual, you can use Ctrl+R to insert the contents of a register into the current position in the command line. The manual also claims that Ctrl+Y inserts the text highlighted with the mouse into the command line. Remember that in X11 and some other systems, you can also paste text into a program from the system clipboard using the middle mouse button or a menu command in your terminal emulator.
I think to avoid have your command line be huge you can use this to solve your issue
:%s/foo/\=#a/g
That replaces "foo" with whatever is in register a.
If you're trying to do a substitute with a long complicated search pattern, here's a good way of going about it:
Try out the search pattern using some test cases and refine it until you have the pattern you want. I find incsearch really helps, especially with complicated regular expressions.
You can then use :%s//new to replace all instances of the last searched for pattern.
If you've entered a pattern and want to copy it out of the search history, you can use q/ to bring up a command line window containing recent search patterns very similar to the q: one that contains recent command history.
On the other hand, if you're asking about how to copy and paste text into the substitute command:
I'd write the pattern out in insert mode and yank the search and replacement into two distinct registers using, say, "ay and "by and then use :%s/<C-R>a/<C-R>b/gc to do the substitute. There are lots of variations of the yank command, but this one should also work automatically when using a visual selection.
If you're copying in text from the clipboard, you can use <C-R>* to paste it's contents in insert mode.
I have the following mapping in my .vimrc
vnoremap <leader>r "ry:%s/^Rr/
So I visually select the thing I want to replace, and hit ,r, type the replacement and hit return. If I want to paste the replacement, I yank it before selecting the text to replace, and then use <C-r>" to paste it as the replacement before hitting return.
Note: to insert ^R in your .vimrc, you actually type <C-v><C-r>.

Resources