如何在 vim 中选择文本?

在大多数文本编辑器中,我可以通过鼠标点击和拖动来选择文本,然后使用 Ctrl-C复制该文本,或者使用 Backspace删除该文本。

但是,由于 vim在控制台中运行,如果用鼠标突出显示某些文本,则 vim命令不会影响所选内容。

vim中选择文本的等效方法是什么?

64693 次浏览

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.

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: