How do you select text in vim?
Asked Answered
C

2

107

In most text editors, I can select text by clicking and dragging with my mouse, and then using Ctrl-C to copy that text, or Backspace to delete it.

However, since vim runs in the console, if I highlight some text with the mouse, my vim commands don't affect what I have selected.

What is the equivalent way to select text in vim?

Catherine answered 26/7, 2013 at 20:55 Comment(1)
A discussion on Meta has been opened in regards to the validity of this question. Please reference that post and discuss before voting to close this question.Catherine
C
172

In vim, text is selected by entering Visual mode. This can be done in multiple ways.

  • v (lower case v) begins regular Visual mode, and works similar to selecting text with a mouse. Use h and l to expand the selection left and right to include more words, and use j and k to expand the selection to the lines below and above.
  • V (upper case v) begins linewise visual mode. This selects entire lines of text at a time. Use j and k to expand the selection up and down.
  • Ctrl+v(lower case v) enters block visual mode. This selects text in a block format, allowing you to select parts of multiple lines without including the entire line. Use hjkl as usual.
  • As @FDinoff suggested, if your terminal emulator supports it, you can even specify visual selections with the mouse by enabling mouse input with :set mouse=a.

Once you have selected the text you want, you can use all sorts of commands on them. Some of the more useful ones are:

  • Escape visual mode
  • delete the text
  • yank (copy) the text
  • paste your clipboard onto the text, replacing it
  • change the text, which deletes it and sets your cursor for typing
  • replace the text with the next character you type
  • yq/p search for the text elsewhere in your document

You can learn more about Visual mode by typing :help v while inside vim.

Catherine answered 26/7, 2013 at 20:55 Comment(4)
Depending on the terminal emulator you could get mouse support in vim if you use set mouse=a.Bradstreet
Thanks @FDinoff, I have added that information to the answer and cited you.Catherine
In the terminal I am using, Ctrl+v actually pastes text. Is there another way of entering block visual mode?Ampliate
@Ampliate I’m guessing you’re using Windows? They have documentation about this exact case, just search “vim”. The basic idea is to tell your terminal to behave itself and to stop stealing your ctrl+v.Catherine
A
1

Hightlight yanked text

First of all I would like to recommend highlight yanked text: https://github.com/machakann/vim-highlightedyank (vim and neovim)

This is useful because it will give you a visual hint of what you have just copied.

For neovim:

augroup highlight_yank
    autocmd!
    au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END

The vim philosophy goes way beond selecting, copying etc.

Start spending more time reading about vim/neovim and you will not going back to any other editor.

Nice to meet you dear "text-objects"

  • Read more about them here

Copy a whole paragraph to the clipboard:

"+yip

"+ .................... clipboard register
y ..................... copy
ip .................... inner paragraph

Copy the whole file to the clipboard

:%y+

Test some vim commands from the clipboard

:@+

The above command allows you to run functions and vim commands even if did not pasted them into your vimrc, there are some exceptions but in general it will work.

You can define your own text-objects

" vim line text-objects
xnoremap al :<C-u>norm! 0v$<cr>
xnoremap il :<C-u>norm! _vg_<cr>
onoremap al :norm! val<cr>
onoremap il :norm! vil<cr>

So you can use vil or dil

Sometimes you don't need to select to copy If you wan to copy the second line to the end of the file you can do:

:2t$

If you want to move lines 4-7 to the beggining of the file you can do:

:4,7m0

copy from mark a to mark b:

ma .................. mark current line as mark a

Jump to a second place in your file and then

mb .................. mark current line as mark b

finally:

:'a,'by+

from mark a to mark b copy to the clipboard

Diving into advanced vim:

Aniseed answered 1/8, 2022 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.