将 URL 地址替换成 URL 格式化文本
Author: Leal
Last Change: 3 17/05/2006 12:24:07 leal@raint:16.cal
示例网址:
--------------------------------------------------
testhttp://anotherone.com
http://test.test.asp?so http://test.com/index.htm#contents
http://126.html Hi, I am here.
http://127.html 中文
http://128.html中文
http://%%xx.te.o.
http://www.test.com/articles/2006/05/15/news/libya.php
http://anotherone.htm
生成结果:
--------------------------------------------------
testhttp://anotherone.com
〔url=http://test.test.asp?so) http://test.test.asp?so 〔/url) 〔url=http://test.com/index.htm#contents) http://test.com/index.htm#contents 〔/url)
〔url=http://126.html) http://126.html 〔/url) Hi, I am here.
〔url=http://127.html) http://127.html 〔/url) 中文
〔url=http://128.html) http://128.html 〔/url)中文
〔url=http://%%xx.te.o.) http://%%xx.te.o. 〔/url)
〔url=http://www.test.com/articles/2006/05/15/news/libya.php) http://www.test.com/articles/2006/05/15/news/libya.php 〔/url)
〔url=http://anotherone.htm) http://anotherone.htm 〔/url)
命令说明:
--------------------------------------------------
1. 只适用于网址中无空格 / 制表符 / 多字节字符等的情况;
2. URL 和非 URL 部分的英文文本之间必须以空格或非 URL 组成字符进行分隔;
3. URL 和多字节字符如中文之间可以连写,不用空格或非 URL 组成字符进行分隔;
4. 如果使用 Ex-mode 命令,则将下列命令拷贝到 : 之后;其中 /c 会提示你进行
确认,你也可以将它换成 /g ;将所有 URL 的合法字符添加到 [ 和 ] 中即可.
%s/\<http\:\/\/[a-zA-Z0-9_%?&:./]*/\ & \[\/url\]/c
改进版本:
--------------------
%s;\<http://[-a-zA-Z0-9_%?&:./#]*;[url=&]&;gc
5. 如果想快捷的执行替换操作,可以将如下命令拷贝到 : Ex-mode 命令中;或者
将该命令添加到 .vimrc 里,此后只要在 Ex-mode 里执行 :URL 即可.
sil! com -nargs=0 URL exe '%s/\<http\:\/\/[a-zA-Z0-9_%?&:./]*/\ & \[\/url\]/c'
改进版本:
--------------------
sil! com -nargs=0 URL exe '%s;\<http://[-a-zA-Z0-9_%?&:./#]*;[url=&]&;gc'
NOTE: 6-10 摘编自 <学习 vi 编辑器> 第六版 (< Learning the vi Editor >)
6. 除 / 之外,我们可以采用除 \, " 和 | 之外的任何非字母 / 非空白字符作为
搜索串,替换串等之间的分隔符. 当我们要查找替换一个路径或其他含有大量 /
的串时,这点非常有用.
7. 如果把 - 放在 [ ] 模式块的最前面, 则不必进行转义(即 \-). [ ] 里大多数
元字符已失去了其原有作用, 不过必要时 \, - 和 ] 三者仍需进行转义.
8. 搜索串中 \n, \, &, ~, \u, \l, \U, \L, \e 和 \E 都是元字符.
9. \< 表示一个字的开始.
10.在遇到复杂, 重复的编辑任务时, 都要花时间对问题进行分析, 是否可以使用
模式匹配工具来处理相关内容.
Besides /, we can use any non-alpha, non-blank character except \, " and | to separate patter-to-search
and string-to-replace. This is especially useful when we modify a path or things including bundles of /.
If we put - in the front of [ ] pattern, we need not escape it (e.g. \-). Most of atom character in [ ]
have lost its original meanings, but \, - and ] still have to be escaped.
\n, \, &, ~, \u, \l, \U, \L, \e and \E in string-to-replace still have to be escaped.
\< means that blank or other non-alpha should exist before http://......
[分享] Vim 将 URL 地址替换成 [url=URL] URL [/url] 格式化文本
- leal
- 帖子: 1119
- 注册时间: 2005-08-29 14:49
- 来自: 杭州
- 联系:
- leal
- 帖子: 1119
- 注册时间: 2005-08-29 14:49
- 来自: 杭州
- 联系: