vim 中的宏

N 天前偶然在 zhihu.com 上看到这样一道问题:vim 怎样替换每行第二个出现的指定的字符?

比如:
aaa,bbb,ccc,ddd
eee,fff,ggg,hhh
iii,jjj,kkk,lll

怎样把b, f, j后面的逗号改成引号?

然后了,下面有两位同学回答了,得票最高的那位使用的是正则,通过 vim 中的命令模式实现:

:%s/^\([^,]*,[^,]*\),/\1"/g

这个不是今天的主角,然后下面还有个同学回答了:

最快速方便的方法是使用宏
命令模式下按qa进入录制状态,按照以下顺序操作就可以了,“#”字符之后为注释,宏将保存在寄存器a中
0    #定位到行首
2f,  #定位到第二个,字符
r'   #将光标下的字符替换为'
j    #进入下一行
q    #退出宏录制状态

针对剩余的行调用宏就可以了,比如在命令行模式下键入“[email protected]”,就是重复执行100次

更加具体的细节,在命令模式下键入:help q及:help @

vim 中的宏,大致就是可以批量处理相同要求的一个功能。

恰好晚上某女复习到软工,其中有 12 套选择题的卷子,大致的样子像下面这个样子(截取了一部分):

Test1^M
1.  What is Configuration management? ^M
  A:  is the discipline of managing and controlling change in the evolution of software systems. ^M
2.  Most errors found by users in software are the result of ^M
  A:  difficulties understanding the problem statement. ^M
3.  Mutable objects cannot be interned. ^M
  A:  True ^M
4.  Decentralized organization permits meetings to proceed effectively without a leader.  ^M
  A:  False ^M
5.  A good design does not rule out any future extension to a project.  ^M
  A:  False ^M
6.  In a well-managed large project typically the least time is spent on ^M
  A:  coding ^M
7.  Which phase of software projects is usually the most costly? ^M
  A:  maintenance^M
8.  During which decade was the term "software engineering" coined? ^M
  A:  The 1960's ^M
9.  Which of the following activities are part of the requirements phase?^M
  A:  Clarification of the users' needs. / Development of a project plan, including the process to be followed. /Development of a verification & validation plan. /Writing a system test plan.^M
10. You are a software manager at a large software company. You are asked to bid on a project that would automate the air traffic control system for a large airport. The current (largely manual) system works reasonably well, but your potential client hopes to save money by automating the system. However, the client is very flexible about the terms of the contract. You realize that there are extremely high safety and reliability requirements and the corporate lawyers advise you that the company could be liable for damages if the system fails to operate properly. In addition, there are many unknowns about the feasibility of the system that may not be resolved until much later in development. Which lifecycle model would you choose? ^M
  A:  The spiral model ^M
Test2^M
1.  Which question no longer concerns the modern software engineer? ^M
  A:  Why does computer hardware cost so much? ^M
2.  Today the increased power of the personal computer has brought about an abandonment of the practice of team development of software. ^M
  A:  False ^M
3.  Software is a product and can be manufactured using the same technologies used for other engineering artifacts.^M
  A:  False ^M
4.  Software deteriorates rather than wears out because ^M
  A:  Multiple change requests introduce errors in component interactions ^M
5.  Most software continues to be custom built because ^M
  A:  Off-the-shelf software components are unavailable in many application domains. ^M
6.  The nature of software applications can be characterized by their information ^M
  A:  content/    determinacy ^M
7.  Modern software applications are so complex that it is hard to develop mutually exclusive category names.^M
  A:  True ^M
8.  The so called "new economy" that gripped commerce and finance during the 1990s died and no longer influences decisions made by businesses and software engineers.^M
  A:  False ^M
9.  The functionality of most computer systems does not need to be enhanced the lifetime of the system. ^M
  A:  False ^M
10. Change cannot be easily accommodated in most software systems, unless the system was designed with change in mind. ^M
  A:  True ^M
^M
^M
Test3^M
1.  The linear sequential model of software development is^M
  A:  A reasonable approach when requirements are well defined.^M
2.  The linear sequential model of software development is also known as the^M
  A:  Classical life cycle model/Waterfall model  ^M
3.  The incremental model of software development is ^M
  A:  A good approach when a working core product is required quickly ^M
4.  The rapid application development model is^M
  A:  A high speed adaptation of the linear sequential model ^M
5.  Evolutionary software process models^M
  A:  Are iterative in nature/Can easily accommodate product requirements changes/Do not generally produce throwaway systems ^M
6.  The prototyping model of software development is^M
 A:  A useful approach when a customer cannot define requirements clearly. ^M
7.  The spiral model of software development^M
  A:  Includes project risks evaluation during each iteration ^M
8.  The concurrent development model is^M
  A:  Often used for the development of client/server applications ^M
9.  The component-based development model is  ^M
  A:  Works best when object technologies are available for support ^M
10. The formal methods model of software development makes use of mathematical methods to^M
  A:  Define the specification for computer-based systems/    Develop defect free computer-based systems / Verify the correctness of computer-based systems ^M
^M
Test4^M
1.  The essence of software engineering practice might be described as understand the problem, plan a solution, carry out the plan, and examine the result for accuracy. ^M
  A:  True ^M
2.  Which of the following is not one of Hooker's core principles of software engineering practice? ^M
  A:  Pareto principle (20% of any product requires 80% of the effort) ^M
3.  Every communication activity should have a facilitator to make sure that the customer is not allowed to dominate the proceedings. ^M
  A:  False ^M
4.  The agile view of iterative customer communication and collaboration is applicable to all software engineering practice. ^M
  A:  True ^M
5.  Software engineers collaborate with customers to define which of the following? ^M
  A:  Customer visible usage scenarios /Important software features/  System inputs and outputs ^M
6.  Everyone on the software team should be involved in the planning activity so that we can ^M
  A:  get all team members to "sign up" to the plan ^M
7.  What role(s) do user stories play in agile planning? ^M
  A:  Used to estimate the effort required build the current increment ^M
8.  Which of the following activities is not one of the four things that need to be accomplished by the generic planning task set? ^M
  A:  Create a detailed schedule for the complete software project . ^M
9.  Analysis models depict software in which three representations? ^M
  A:  information, function, behavior ^M
10. The customer can directly observe both the difference between the internal quality of a design and its external quality? ^M
  A:  False ^M
^M
Test5^M
1.  Software engineers do not need to consider hardware when designing a computer-based system . ^M
  A:  False ^M
2.  Which of the following can be elements of computer-based systems? ^M
  A:  documentation/software/people/hardware ^M
3.  The system engineering process usually begins with the ^M
  A:  world view ^M
4.  To construct a system model the engineer should consider which of the following restraining factors?^M
  A:  assumptions /   constraints ^M
5.  By following modern system engineering practices simulation of reactive systems is no longer necessary ^M
  A:  False ^M
6.  During business process engineering, three different architectures are examined.  ^M
  A:  applications, data, technology infrastructure ^M
7.  Which elements of business processing engineering are the responsibilities of the software engineer?^M
  A:  business system design /    construction and integration ^M
8.  The goal of product engineering is to translate the customer's desire for a set of defined capabilities into a working product.^M
  A:  True ^M
9.  The architecture components for product engineering are^M
  A:  data, hardware, software, people ^M
10. The top level of the hierarchical model of a system is known as the ^M
  A:  SCD ^M

要求是按首字母顺序排序,首字母相同就按接下来的一个,如此向下进行,我当时想到的第一个方法就是使用了 vim 中的宏了!下面就是他的过程了。要处理的文件名为 software.sh

$ cat -A software.sh

可以发现该文件是 MS 文件。 处理掉 MS 中的 ^M,在 MS 下的换行符为 \r\n,而 Linux 下的为 \n,要将其转换成 Linux 下的换行符:

$ cat software.sh | tr -d "^M" > software-1.sh

1.处理以 Test 开头的行并且删除空行:

$ sed -i '/^Test/d'  software-1.sh
$ sed -i '/^$/' software-1.sh
删除空行还可以使用下面这些方式:

$ grep -v '^$' file
$ sed '/^$/d'  file
$ sed -n '/./p' file
$ awk '/./ {print}' file

2.处理每行的序号:

Ctrl + v 选中,d

现在的文件内容应该像这样:

What is Configuration management?
A:  is the discipline of managing and controlling change in the evolution of software systems.
Most errors found by users in software are the result of
A:  difficulties understanding the problem statement.
Mutable objects cannot be interned.
A:  True
Decentralized organization permits meetings to proceed effectively without a leader.
A:  False
A good design does not rule out any future extension to a project.
A:  False
In a well-managed large project typically the least time is spent on
A:  coding
Which phase of software projects is usually the most costly?
A:  maintenance
During which decade was the term "software engineering" coined?
A:  The 1960's
Which of the following activities are part of the requirements phase?
A:  Clarification of the users' needs. / Development of a project plan, including the process to be followed. /Development of a verification & validation plan. /Writing a system test plan.
You are a software manager at a large software company. You are asked to bid on a project that would automate the air traffic control system for a large airport. The current (largely manual) system works reasonably well, but your potential client hopes to save money by automating the system. However, the client is very flexible about the terms of the contract. You realize that there are extremely high safety and reliability requirements and the corporate lawyers advise you that the company could be liable for damages if the system fails to operate properly. In addition, there are many unknowns about the feasibility of the system that may not be resolved until much later in development. Which lifecycle model would you choose?
A:  The spiral model
You work for a small start-up company trying to secure venture capital funding. The product you intend to develop is mass-market consumer software for an unprecedented application; thus there are many questions about the user interface. Which lifecycle model would you choose?
A:  The rapid prototyping model
Which of the following are examples of nonexecution-based testing?
A:  Walkthroughs/Inspections/Correctness proofs
Which of the following statements about software process is not true?
A:  Having a good process guarantees that you will have a good product.
Which of the following principles does not directly help manage complexity?
A:  Rigor and formality
Which of the following software qualities is an internal software quality?
A:  Evolvability
Which of the following would not be appropriate to include in a requirements document?

3.将偶数行的数据合并到奇数行,也就是说每个答案都要紧贴在题目的后面,此时发挥 vim 宏的强大作用:

gg      #定位到行首
qa      #开始录制宏
Shift-j #合并下一行
j       #进入下一行
q       #退出宏录制状态
[email protected]    #批量完成

4.至此,最主要的工作已经完成,接下来就是按首字母排序:

$ sort software-2.sh > final.sh

  • http://ssssp.net xufan6

    学vim最早学会的高级点的东西就是宏了。。像我这种命令都记不全的用宏处理了好多事儿~