David Wolever
David Wolever

Reputation: 154664

vim: insert selected text into command line?

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.

Upvotes: 18

Views: 11550

Answers (6)

javier_domenech
javier_domenech

Reputation: 6273

For pasting on the vim command line:

  • first copy the selected text
  • open command line :
  • use Ctrl-r " for pasting

Upvotes: 5

Vitalik Verhovodov
Vitalik Verhovodov

Reputation: 674

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>.

Upvotes: 0

roger
roger

Reputation: 21

  1. Put copy and paste code into vimrc config file.
  2. Reopen vim editor.
  3. Select text (e.g. using v)
  4. Enter command mode using :
  5. 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:

  1. Based on idea by ierton (on this page)
  2. 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>

Upvotes: 2

Grwlf
Grwlf

Reputation: 956

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.

Upvotes: 9

syllogism
syllogism

Reputation: 655

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.

Upvotes: 1

Jorge Gajon
Jorge Gajon

Reputation: 1769

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.

Upvotes: 2

Related Questions