Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select text in vim?

Tags:

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?

like image 328
Cory Klein Avatar asked Jul 26 '13 20:07

Cory Klein


People also ask

How do I select and copy text in Vim?

Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement. Press d (delete) to cut, or y (yank) to copy.

How do I select text in Linux vi?

5.1. We can select and delete specific texts using the Shift + V keys and then using the dd command. Let's delete the line Welcome to Linux! – Line 4. First, we'll need to open a vi session and press v to be in the VISUAL LINE mode.

How do I select a word in vi editor?

You can use * and/or # to search for the word under the cursor or viw to visually select the word under the cursor.


1 Answers

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.

like image 173
Cory Klein Avatar answered Oct 12 '22 12:10

Cory Klein