在Vim中用正则表达式重新格式化被干扰的表

wbrvyc0a  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(154)

如果我从流行的www.example.com站点复制一个表tutorialspoint.com并将其粘贴到文本文件中,格式会受到严重干扰。例如,https://www.tutorialspoint.com/lua/lua_operating_system_facilities.htm处的表将变为:

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重新格式化以便于阅读?我尝试了以下方法:

%s/\n\n/\t/g | %s/ \t\t/\t/g

但它在一行中给出了所有信息。我如何才能获得更好的显示效果,例如,在以下格式中:

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

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

谢谢你的帮助。

uidvcgyl

uidvcgyl1#

这些替换看起来做得相当不错:

:%s/^\(\d\+\)\s*\n\n/\r\1\t
:%s/^\D/\t&

第一次替换

  • 搜索部件:
^           anchor the search to the beginning of the line
\(\d\+\)    capture group #1 consisting of 1 or more digits
\s*         0 or more whitespace characters
\n\n        two newline characters
  • 更换零件:
\r          a newline character (\n in search, \r in replace)
\1          capture group #1
\t          a tab character

第二次替换

  • 搜索部件:
^           anchor the search to the beginning of the line
\D          a non-digit character
  • 更换零件:
\t          a tab character
&           the matched text

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

:command! COMMANDNAME %s/^\(\d\+\)\s*\n\n/\r\1\t/ | %s/^\D/\t&/ | nohlsearch

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

s<separator>search<separator>replace<separator>[flags]

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

:s/foo/bar
:s/foo/
:s/foo

如果要在替换后使用另一个命令,则最后一个分隔符是必需。
适用于当前所选内容或整个缓冲区的变量:

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

有关-range=%:help '[的信息,请参见:help command-range

相关问题