How do you select text in vim?

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?


Solution 1:

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:

  • 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
  • Escape visual mode

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