---
title: "vim tips"
author: "standalone"
date: "2012-07-04T13:59:38Z"
tags: []
categories: ["编程语言"]
slug: "vim-tips"
draft: false
lastmod: "2012-07-04T13:59:38Z"
id: 1576431
url: "https://standalone.iteye.com/blog/1576431"
---

I read some articles about vim tips. Now I write down the ones I have tried successfully here.

1. 在insert模式下，C-R (register) 插入register里的内容，一个有趣的reg是"=".
假设你想输入123K的具体字节数，不用打开计算器，试试这个“<C-R>=1024*123<CR>”，
“125952”就出来了！
另外在命令行里C-R C-W和C-R C-A是必用的技巧，它们将光标下的<word>和<WORD>
考到命令行里，省了你无数的typing。

2. %用来匹配块，
如果你的光标在类似()
或者#ifdef #else #endif上
%将把光标跳转到相应的匹配符号上去，
%还可以用来指定命令范围，
如果你想把一个
{
 ..
 ...
}的块全部删除。
可以先把光标移到{ 再敲d%
类似的，
如果你想把一个块全部往里缩进一个tab
可以把光标移到 { 敲>%


3. 正则表达式大家都清楚，
我主要讲个一般人不太用，
但很有用的表达,

例如你想把所有的"..."形式的串替换成'...'的形式
但引号里的内容不变
你就可以用
%s/"\(.*\)"/'\1'/来做
上面这个正则表达式"\(.*\)"里 \用来表示()是元字符
第一个在括号里的被匹配的串就可以被\1来代表, 以后依次是\2 \3。


4. x 表示任何一个字符。
这是最快的在一行种移动的方法了。然后用
;  （分号）
继续移动。反向移动是F.

5.  任何一个操作命令在加一个移动命令。实现对某个范围的操作。
例如
dfx
表示删除到下一个出现 x 的地方， x 可以使任意字符。
操作命令有  d (cut), y(copy) , p(paste), v (select)
移动命令有  hjkl, f, /， gg, G

6. 任何命令组合都可以先按一些 数字健 表示重复操作。
如：
d123j
删除下面123行。

7. :r !ls
可以读取当前目录的文件列表。
如果你对 bash 很熟悉的话，这个功能非常好用
例如
输入
case 1
case 2
....
case 1000:
的方法是
:r !for ((i=0;i<100;i++));do echo "case $i" ; done

8. 大小写变换。
~     -     将光标下的字母改变大小写

3~    -     将下3行的字母改变其大小写

g~~   -     改变当前行字母的大小写

U     -     将可视模式下的字母全改成大写字母

gUU   -     将当前行的字母改成大写

u     -     将可视模式下的字母全改成小写

guu   -     将当前行的字母全改成小写

gUaw  -     将光标下的单词改成大写。

guaw  -     将光标下的单词改成小写。

9。 marks
To mark one or more positions in a file, use the m command. 

Examples: 

ma       -    set current cursor location as mark a 

'a       -    jump to beginning of line of mark a 

`a       -    jump to postition of mark a 

d'a      -    delete from current line to line of mark a 

d`a      -    delete from current cursor position to mark a 

c'a      -    change text from current line to line of mark a 

y`a      -    yank text to unnamed buffer from cursor to mark a 

:marks   -    list all the current marks 

NB: Lowercase marks (a-z) are valid within one file. Uppercase marks 
(A-Z), also called file marks, are valid between files. 

For a detailed description of the m command refer to 

:help mark 

See also: 

:help various-motions