If I've selected some text in visual mode, how do I insert the selected text into the command line?
For example, if I want to search for the currently selected text, I could use y/<c-r>"… But is there some way of doing it without first yanking the selected text?
Edit: A better example, as searching while in visual mode would expand the selection: if I've got part of a file name selected, and I want append to that name then open it in a new editor.
I have remapped : in visual mode to solve this problem. With following mapping I can paste selected text to the command line and b - onus - move the cursor to the leftmost position:
vnoremap : y:<C-r>"<C-b>
One small drawback I can see - pressing : now cancels the selection.
After activating visual mode and selecting some text, if I hit / to search forward and then press <C-R>*, I get the selected text inserted into the command line.
Another way (at least in Linux) is to press <S-Insert>, which will insert the contents of the current selection in X11 (e.g. select something in firefox and you can <S-Insert> in any other application to copy that selection; again, at least in Linux).
Keep in mind that with your specific example, after you insert some text after the / command, your visual selection will expand to include the next occurrence of whatever it is searching.
Edit: I was assuming GVim, which copies the visual selection to the X11 selection buffer (see :help x11-selection for more info).
If you are using Vim inside a console, then I think (but could be wrong) that your best option is to select the text you want with the mouse while pressing Shift down, and then use <S-Insert> to copy that text into the command line. This works in Linux and probably in OSX too; I don't know about Windows, sorry.
Put copy and paste code into vimrc config file.
Reopen vim editor.
Select text (e.g. using v)
Enter command mode using :
Selected text can now be pasted to command line using Ctrl-p
The selected text will have special characters escaped ready for use in regex.
Credits:
Based on idea by ierton (on this page)
Most of code taken from bryan kennedy
Vim search and replace selected text
" Escape special characters in a string for exact matching.
" This is useful to copying strings from the file to the search tool
" Based on this - http://peterodding.com/code/vim/profile/autoload/xolox/escape.vim
function! EscapeString (string)
let string=a:string
" Escape regex characters
let string = escape(string, '^$.*\/~[]')
" Escape the line endings
let string = substitute(string, '\n', '\\n', 'g')
return string
endfunction
" Get the current visual block for search and replaces
" This function passed the visual block through a string escape function
" Based on this - https://stackoverflow.com/questions/676600/vim-replace-selected-text/677918#677918
function! GetVisual() range
" Save the current register and clipboard
let reg_save = getreg('"')
let regtype_save = getregtype('"')
let cb_save = &clipboard
set clipboard&
" Put the current visual selection in the " register
normal! ""gvy
let selection = getreg('"')
" Put the saved registers and clipboards back
call setreg('"', reg_save, regtype_save)
let &clipboard = cb_save
"Escape any special characters in the selection
let escaped_selection = EscapeString(selection)
return escaped_selection
endfunction
" Start the find and replace command across the entire file
vmap <leader>z <Esc>:%s/<c-r>=GetVisual()<cr>/
" When in visual mode,
" before command mode is entered by using :
" Put the contents of any selected text into
" the default register
" leave the text highlighted.
" Based on ierton - https://stackoverflow.com/questions/4878980/vim-insert-selected-text-into-command-line
vnoremap : "pygv:
"Map ctrl-P to paste escaped contents of default register in command mode
" Based on bryan kennedy - https://stackoverflow.com/questions/676600/vim-search-and-replace-selected-text
cnoremap <C-P> <c-r>=GetVisual()<cr>
I know this may or may not help you, but type !command from within vim.. It's not copy and paste, but if you're using ssh - it will at least let you view your buffer(s) while you're typing out your command.
ex: !make
It really all depends on what environment you're in..
If you're using putty from windows, you can actually highlight and copy text from it(configurable as well within putty) and paste into either another putty session or another window(if using screen or tmux).
If you're actually on the box and have a graphical environment, copy & paste should be fairly easy(so I assume you're not in this position).
Hope it helps, sorry if it doesn't.
For pasting on the vim command line:
first copy the selected text
open command line :
use Ctrl-r " for pasting
I found the solution. When you press : in visual mode instead of normal ":" you have there ":'<,'>". So you need to erase this to write command as usual.
For expanding to selected text <C-r>* works pretty well.
vnoremap <M-8> :<BS><BS><BS><BS><BS>NGrep <C-r>*<CR>
If you want to edit command before launching it just remove trailing <CR>.
Related
I am using Neovim 0.5 running on the Windows 10 command line terminal.
When I copy a value from a cell in Microsoft Excel or Libreoffice Calc e.g. "WORD", it is displayed in the "+ register as "WORD^J". The problem is that when I paste this value into Neovim using "+p or "+P, the inserted text always jumps to the line above where my cursor is.
I think that the ^J character/symbol is some kind of new line symbol that spread-sheets or Windows programs use for some reason but I can't be sure as there seems to be little info out there about it based off my time spent Googling.
I just want the text to be pasted clean at the exact point where the cursor is. I have tried adding the following to my vimrc (and various other lines of code) but can't succeed in stripping out the unwanted "^J" symbol. This substitution works for regular letters but not for "^J".
Does anyone know what this symbol is and how I can strip it from the "+ register before being pasted into vim?
Once I remove the symbol, I can update the mapping below to simply paste in the new contents of the "+ register.
nnoremap <C-v> :let #+ = substitute(#+, '^J', '', 'g')
Thanks.
You are right ^J symbol is for new line.
You can use the following to solve your issue:
:noremap <C-v> :let #+ = substitute(#+, "\n", "", "g")<CR>"+p
This modifies the + register to remove new line symbol and paste it.
I want to paste some word into command area of gvim. (in escape mode)
Say for example i have copied some search text from somewhere else
and now i want to search a huge string.
/howToPasteSomeHugeWord
If your pattern was copied from your browser it is placed on register +, so you can start searching by typing / then Ctrl-r +
If you copied some vim selection with y you can use: /Ctrl-r 0
You can also use any register in any mode, for example, you find a cool function and want to test it on vim you can do
:#+
The above command will execute wherever you have in your clipboard
If you want to copy your last command to clipboard you can do
:let #+ = #:
I modified some code I found here to work for LaTeX. I demonstrate the desired output for two cases. Please have a look at
Dealing with LaTeX Commands
Dealing with LaTeX Environments
http://vim.wikia.com/wiki/Wrap_a_visual_selection_in_an_HTML_tag
Dealing with LaTeX Commands
Given:
elephant
visually select "elephant"
press F7
enter "test"
Output:
\test{elephant}
Example Code Code for VisualcommandTagWrap
This works for singleline selections. I'd like to make it work for multiline selections.
Simply select text to wrap using your visual selector, then press F7.
" Wrap visual selection in a latex command tag.
map <F7> : call VisualcommandTagWrap()
function! VisualcommandTagWrap()
let tag = input("Tag to wrap block: ")
if len(tag) > 0
normal `>
if &selection == 'exclusive' "set selection=exclusive means position before the cursor marks the end of the selection vs. inclusive
exe "normal i\\".tag."}"
else
exe "normal a\}"
endif
normal `<
exe "normal i\\".tag."{"
normal `<
endif
endfunction
Dealing with LaTeX Environments
Given:
elephant
visually select "elephant"
press F7
enter "test"
Output:
\begin{test}
elephant
\end{test}
Example Code for VisualenvironmentTagWrap
I do not know how to implement functions that cover multiple lines.
Additional Examples
For those unfamiliar with LaTeX. There are two situations to satisfy requirements for LaTeX commands and environments.
Here is some visual mode selected text.
And another line of visual mode selected text.
Command Situation:
type function key defined in .vimrc to call function
vim prompts for user input (e.g. "Please enter name of command:")
vim surrounds selection with command.
Result:
\inputstep2{Here is some visual mode selected text.
And another line of visual mode selected text.}
Environment Situation
type function key defined in .vimrc to call function
vim prompts for user input (e.g. "Please enter name of environment:")
vim surrounds selection with environment.
Result:
\begin{inputstep2}
Here is some visual mode selected text.
And another line of visual mode selected text.
\end{inputstep2}
Step 1:
You can store the text you want (like test) in register a.
:let #a='test'
(Until you change it, it will remain the same and can be reused. If you want it to be changed, you have to follow the step again.)
Step 2:
Then, select the the text using visual selection (v, V or ctrl V) and then press escape to cancel the selection.
Step 3;
Keep the cursor on anywhere in the screen, (preferably in the line where you want to surround with) and press \s in normal mode.
Put the following mapping in ~/.vimrc file.
Let's create a mapping to wrap around.
For first example: (begin and end type)
:nmap \s '<O\begin{<C-R>a<ESC>}'>o\end{<C-R>a}
For second example: (begin type)
:nmap \s '<O\{<C-R>a<ESC>'>a}
Let's say I have a word selected in visual mode. I would like to perform a substitution on that word and all other instances of that word in a file by using s//. Is there a way to use the highlighted text in the s/<here>/stuff/ part without having to retype it?
Sure. If you selected the word, just "y"ank it, and then type:
:%s/<ctrl-r>"/something else/g
Where is pressing ctrl key with r key, and " is just " character.
All keypresses:
y:%s/<ctrl-r>"/what to put/g<enter>
If you searched for your text before you can use
CTRL-R /
to insert the last search item in your search and replace string.
You can check this page for other similar tricks:
http://www.vim.org/htmldoc/insert.html
You don't have to yank the word, place your cursor on the word and then:
:%s/<C-r><C-w>/bar/g
Another way to access register contents from the command line is via # variables. So if you yank text into the default register, it'll be in a variable called #".
:exe '%s/' . #" . '/stuff/'
Here's a mapping to make this easy to type:
vmap <Leader>s y:exe '%s/' . #" . '//g'<Left><Left><Left>
Now you can highlight something in visual mode, type \s, type your replacement and hit Enter. depesz's version also makes a good mapping (almost exactly as he typed it):
vmap <Leader>s y:%s/<c-r>"//g<Left><Left>
I want to search for $maximumTotalAllowedAfterFinish and replace it with $minimumTotalAllowedAfterFinish. Instead of typing the long text:
:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g
Is there a way to COPY these long variable names down into the search line, since, on the command line I can't type "p" to paste?
You can insert the contents of a numbered or named register by typing CTRLR {0-9a-z"%#:-=.}. By typing CTRL-R CTRL-W you can paste the current word under the cursor. See:
:he cmdline-editing
for more information.
Copy it as normal, then do CtrlR" to paste. There are lots of other CtrlR shortcuts (e.g, a calculator, current filename, clipboard contents). Type :help c_<C-R> to see the full list.
Copy:
1) v (or highlight with mouse, in visual mode)
2) y (yank)
Paste:
1) / (search mode)
2) Ctrl + R + 0 (paste from yanked register)
Type q: to get into history editing mode in a new buffer. Then edit the last line of the buffer and press Enter to execute it.
Or create the command in a vim buffer , e.g. type it in the buffer:
s/foo/bar/gci
And copy it to a named register, with "ayy (if the cursor is on that line!).
Now you can execute the contents of the "a" register from Vim's Ex command line with:
:[OPTIONAL_RANGE]#a
I use it all the time.
You can place the cursor on the word that you want to add to your pattern and then press / or : to enter either the search or the command mode, and then press CtrlRCtrlW to copy the word. Source
Typically, you would do that with mouse selecting (perhaps CtrlIns or CtrlC after selecting) and then, when in the command/search line, middle-clicking (or ShiftIns or CtrlV).
Another way, is to write your command/search line in the text buffer with all the editing available in text buffers, starting with : and all, then, on the line, do:
"add#a
which will store the whole command line in buffer a, and then execute it. It won't be stored in the command history, though.
Try creating the following line in the text buffer as an example for the key presses above:
:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g
Finally, you can enter q: to enter history editing in a text buffer.
add a line: cnoremap <c-v> <c-r>+ in your vimrc, then you can use ctrl-v to paste.