首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Regex在Vim中重新格式化受干扰的表

Regex在Vim中重新格式化受干扰的表
EN

Stack Overflow用户
提问于 2017-10-29 18:09:02
回答 1查看 47关注 0票数 0

如果我从流行的tutorialspoint.com站点复制表并将其粘贴到文本文件中,则格式会受到严重干扰。例如,facilities.htm的表如下:

代码语言:javascript
复制
Sr.No.  Library / Method & Purpose
1   

os.clock ()

Returns an approximation of the amount in seconds of CPU time used by the program.
2   

os.date ([format [, time]])

Returns a string or a table containing date and time, formatted according to the given string format.
3   

os.difftime (t2, t1)

Returns the number of seconds from time t1 to time t2. In POSIX, Windows, and some other systems, this value is exactly t2-t1.
4   

os.execute ([command])

This function is equivalent to the ANSI C function system. It passes command to be executed by an operating system shell. Its first result is true if the command terminated successfully, or nil otherwise.
5   

os.exit ([code [, close])

Calls the ANSI C function exit to terminate the host program. If code is true, the returned status is EXIT_SUCCESS; if code is false, the returned status is EXIT_FAILURE; if code is a number, the returned status is this number.
6   

os.getenv (varname)

Returns the value of the process environment variable varname, or nil if the variable is not defined.
7   

os.remove (filename)

Deletes the file (or empty directory, on POSIX systems) with the given name. If this function fails, it returns nil, plus a string describing the error and the error code.
8   

os.rename (oldname, newname)

Renames file or directory named oldname to newname. If this function fails, it returns nil, plus a string describing the error and the error code.
9   

os.setlocale (locale [, category])

Sets the current locale of the program. locale is a system-dependent string specifying a locale; category is an optional string describing which category to change: "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category is "all". The function returns the name of the new locale, or nil if the request cannot be honored.
10  

os.time ([table])

Returns the current time when called without arguments, or a time representing the date and time specified by the given table. This table must have fields year, month, and day, and may have fields hour (default is 12), min (default is 0), sec (default is 0), and isdst (default is nil). For a description of these fields, see the os.date function.
11  

os.tmpname ()

Returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed.

如何使用Vim中的Regex重新格式化以方便阅读?我试过以下几点:

代码语言:javascript
复制
%s/\n\n/\t/g | %s/ \t\t/\t/g

但它给出了所有的一条龙。如何获得更好的显示,例如以下列格式显示:

代码语言:javascript
复制
1<tab>function_name(arguments)
<tab>function description

2<tab>function_name(arguments)
<tab>function description

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-29 20:15:39

这些替换似乎做得相当好:

代码语言:javascript
复制
:%s/^\(\d\+\)\s*\n\n/\r\1\t
:%s/^\D/\t&

第一替代

  • 搜索部分: ^将搜索定位到行(\d+)捕获组#1的开头,该组#1由1个或多个数字\s* 0或更多空格字符\n\n两个换行符组成
  • 更换零件: \r换行符(\n在搜索,\r在替换) \1捕获组#1 \t一个制表符字符

二次替代

  • 搜索部分: ^将搜索定位到行首\D非数字字符
  • 更换零件: 不使用制表符&匹配的文本

编辑

在整个缓冲区上执行这些替换的工作命令(并禁用hlsearch):

代码语言:javascript
复制
:command! COMMANDNAME %s/^\(\d\+\)\s*\n\n/\r\1\t/ | %s/^\D/\t&/ | nohlsearch

请注意,替换的规范语法是:

代码语言:javascript
复制
s<separator>search<separator>replace<separator>[flags]

虽然可以在一次性命令中省略其中的一些元素:

代码语言:javascript
复制
:s/foo/bar
:s/foo/
:s/foo

如果要使用另一个命令进行替换,最后一个分隔符是必要的。

适用于当前所选内容或整个缓冲区的变体:

代码语言:javascript
复制
:command! -range=% COMMANDNAME <line1>,<line2>s/^\(\d\+\)\s*\n\n/\r\1\t/ | '[,']s/^\D/\t&/ | nohlsearch

参见:help command-range中的-range=%:help '[

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47003543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档