Skip to content

04.Visual Mode

Is there a command to enter Visual Block mode in Vim?

  • On Windows, <C-q>
  • On MacOS, <C-v>

进入可视模式

Method 1

There is no built in command to start visual block mode in Vim, but you can define one yourself:

vim
command! Vb normal~ <C-v>

NOTE: User defined commands start with a capital letter.

Also, there may be a conflicting mapping which prevents you from using <C-v> to enter Visual Block mode. To check for any conflicting mappings for <C-v>, you can run in command-line :verbose map <C-v>, where I get "No mapping found".

Method 2

在 Windows 环境,<C-v> 是粘贴功能,为了在 Vim 中使用 Visual Block 模式,可以使用 <C-q> 来进入该模式。

Tip 20. 深入理解可视模式

Visual mode allows us to select a range of text and then operate upon it. However intuitive this might seem, Vim's perspective on selecting text is different from other text editors.

Many of the commands that you are familiar with from Normal mode work just the same in Visual mode. We can still use h, j, k, and l as cursor keys. We can use f{char} to jump to a character on the current line and then repeat or reverse the jump with the ; and , commands, respectively. We can even use the search command (and n/N) to jump to pattern matches. Each time we move our cursor in Visual mode, we change the bounds of the selection.

  • viw 选中当前单词,然后 c 删除单词并进入插入模式;ciw 删除选中单词并进入插入模式
  • 上述命令等效于直接 cw

We can toggle between Visual and Select modes by pressing <C-g>.

Tip 21. 选择高亮选区

  • v 选择字符
  • V 选择行
  • <C-q>选择列
  • gv 重选上次的高亮选区
  • o 对调光标在选中区块的对角线位置
  • <Esc> or <C-[> switch to Normal mode

Toggling the Free End of a Selection

  1. vbb Select backwards by two words
  2. o toggle the other end of the selection
  3. e select forward to the end of word

Tip 22. 重复执行面向行的可视命令

When we use the dot command to repeat a change made to a visual selection, it repeats the change on the same range of text. In this tip, we'll make a change to a line-wise selection and then repeat it with the dot command.

python
def fib(n):
    a, b = 0, 1
    while a < n:
    print(a)
    a, b = b, a+b
fib(42)
  • Vj 选一行、选二行
  • > 缩进一层
  • . 重复缩进
  1. 基于 $VIM\vimrc 的当前配置,已经可以完成上述操作:
vim
set autoindent
set noexpandtab
set tabstop=2
set shiftwidth=2
  1. 书中作者建议了另一种配置:
vim
:set shiftwidth=4 softtabstop=4 expandtab

经过测试,两者表现至少在操作上述代码缩进时的表现是一致的。那么问题来了,为什么在第一种配置的情况下,缩进仍然是 4 个空格,还不是 2 个空格呢?

Tip 23. Prefer Operators to Visual Commands Where Possible

Visual mode may be more intuitive than Vim's Normal mode of operation, but it has a weakness: it doesn't always play well with the dot command. We can route around this weakness by using Normal mode operators when appropriate.

html
<a href="#">one</a>
<a href="#">two</a>
<a href="#">three</a>

Method 1

  • vit visually select inside the tag
  • U converts to uppercase

When a Visual mode command is repeated, it affects the same range of text (see :h visual-repeat).

Method 2

  • gUit
  • j.
  • j.

The Visual mode U command has a Normal mode equivalent: gU{motion} (:h gU).

How to paste from Windows system clipboard in Vim?

Just p uses so-called unnamed register, which is basically an alias for the last register used. On the other hand, both "*p and "+p always put the contents of the system clipboard at the cursor position. See h: registers.

How to copy to Windows system clipboard in Vim?

The * register will do this. In Windows, + and * are equivalent.

For copying something to the clipboard register, you type "*y and then to put you type "*p.

How to add backticks in pair for selected text in Vim?

How to delete auto indented bullet points in Vim?

Tip 24. Edit Tabular Data with Visual-Block Mode

We can work with rows of text in any editor, but manipulating columns of text requires a more specialized tool. Vim provides this capacity in the form of its Visual-Block mode, which we'll use to transform a plain-text table.

md
| Chapter     | Page |
| ----------- | ---- |
| Normal mode | 15   |
| Insert mode | 31   |
| Visual mode | 44   |

Tip 25. Change Columns of Text

We can use Visual-Block mode to insert text into several lines of text simultaneously.

css
li.one a {
  background-image: url("/images/sprite.png");
}
li.two a {
  background-image: url("/images/sprite.png");
}
li.three a {
  background-image: url("/images/sprite.png");
}

Tip 26. Append After a Ragged Visual Block

Visual-Block mode is great for operating on rectangular chunks of code such as lines and columns, but it's not confined to rectangular regions of text.

js
var foo = 1;
var bar = "a";
var foobar = foo + bar;

Marking text (visual mode)

  • v - start visual mode, mark lines, then do a command (like y-yank)
  • V - start linewise visual mode
  • o - move to other end of marked area
  • Ctrl + v - start visual block mode
  • O - move to other corner of block
  • aw - mark a word
  • ab - a block with ()
  • aB - a block with {}
  • at - a block with <> tags
  • ib - inner block with ()
  • iB - inner block with {}
  • it - inner block with <> tags
  • Esc or Ctrl + c - exit visual mode

TIP

Instead of b or B one can also use ( or { respectively.

Visual commands

  • > - shift text right
  • < - shift text left
  • y - yank (copy) marked text
  • d - delete marked text
  • ~ - switch case
  • u - change marked text to lowercase
  • U - change marked text to uppercase