-
概述
- vim 运行 ex 脚本
-
背景
- 最近在整理一些文本资料
- 发现有某个网站上资料质量还行
- 就是妈的禁用了各种操作
- 鼠标左键选取
- 鼠标右键菜单
- 键盘快捷键
- 就是妈的禁用了各种操作
- 尝试从 js 的角度改变, 结果失败
- 这个时候知道前端知识的重要性了?
- 无奈只能尝试通过 文本处理 的方式来处理
- 暂时使用 vim
- 其实 sed 也可以
- 需要连续的几个 ex 命令
- 暂时使用 vim
-
环境
- OS
- win10
- shell
- mintty-2.9.4
- OS
-
准备
-
原始文本
# demo.html <p>TUTOR: Right, Jason and Karin, now I asked you to look at the case study for Box Telecom as part of your exam assessment. It’s interesting because they are in the middle of problems at the moment and I want you to track how they deal with them. Let’s start with you, Karin. Having read through the case study, can you just summarise what the problems were that Box Telecom had to take on board?</p> <p>KARIN: Um, yeah… Well of course what first came to their attention was that, despite a new advertising campaign, they were suffering from falling <span style="color: #ff0000;"><strong>sales </strong></span>– and this is something that had many causes. On top of that immediate problem, what had also happened over the last two years was that, although they had invested in an expansion plan, they had to face up to increased <span style="color: #ff0000;"><strong>competition</strong></span>. And, before they had a chance to get to grips with the effects of that, they were stalled by a strike and it was just when they were thinking about making a colossal investment in new machinery for their plants. So they were really in trouble. <span style="color: #ff0000;"><strong>Q21, Q22</strong></span></p> <p>TUTOR: Yes, I think that’s fair. And Jason, you contacted the company, didn’t you?What did the company define as the reasons for these problems?</p> <p>JASON: Well, I think they’ve hit on the right things – it would be easy to say they had invested too heavily, or at the wrong time, but in fact the signs were good and what they were set back by was high <span style="color: #ff0000;"><strong>interest rates</strong></span>. At the same time, their longer-term problems, which were affecting their market share, were eventually credited to poor <span style="color: #ff0000;"><strong>training </strong></span>– and having looked at the details in their last report I think that’s right. <span style="color: #ff0000;"><strong> Q23, Q24</strong></span></p> <p>TUTOR: So, onto the larger issues then. Karin, what do you think the company will do?</p> <p>KARIN: Hmm… Well, obviously they have the choice of accepting the very favourable terms that another company – KMG Plc- have given them to buy them out. That would mean creating a new company with a new image. Or they could decide on a bolder move and offer some new shares if they wanted. But I think they’re much more cautious than that and expect they will start <span style="color: #ff0000;"><strong>trying to find individuals who’d be prepared to back them with some of the capital they need</strong></span>. <span style="color: #ff0000;"><strong>Q25</strong></span></p> <p>TUTOR: Well, you mustn’t always assume that dramatic problems require dramatic solutions. Sometimes there’s a simple fix such as changing the guy at the top. If they truly are cautious, then I suspect they will seek to shut down some of their shops. But a more ambitious approach, and one which I think would have more chance of success, would be to <span style="color: #ff0000;"><strong>alter how they’re running things</strong></span> – the management layers and the processes. So in your analysis try to think of all the options. Jason? <span style="color: #ff0000;"><strong>Q26</strong></span></p> <p>JASON: Yes, it’s interesting because I’ve found it a really useful company to study. Its problems cross all types of industries and it’s lucky it’s so big – a smaller or even medium-sized company would have gone under by now.</p> <p>TUTOR: Ah well in fact, what I want you two to do is to go away when we’ve finished our discussion today and write a report. We’ve looked in general at the telecommunications market in the UK over the last few sessions and I want you to take Box Telecom as an example and <span style="color: #ff0000;"><strong>suggest some ways in which they might overcome their problems</strong></span>. And outline the reasons why you think as you do – but try and keep it intrinsic to the company rather than dragging in other examples. Is that OK, Karin? <span style="color: #ff0000;"><strong>Q27</strong></span></p> <p>KARIN: Yes, I think I can do that. Personally I’ve got great hopes for it.<span style="color: #ff0000;"><strong> I think it will recover</strong></span>. That advertising campaign they did was very strong and they’re very innovative with their products – they set new trends. The company’s got to recover, don’t you think, Jason? <span style="color: #ff0000;"><strong> Q28</strong></span></p> <p>JASON: Hmmm – I’m not sure. I think it can but it’s not a foregone conclusion unless they manage <span style="color: #ff0000;"><strong>to attract the right level of investment</strong><strong>. The company definitely needs a boost</strong></span> and to attract more highly skilled workers if their recovery is to be long-lasting. When I was talking to the marketing manager he said to me that he thinks the company had got a great management team – but he would say that, wouldn’t he? – but they are suffering from having to work with outdated production machinery and that could cost a lot to put right. <span style="color: #ff0000;"><strong>Q29</strong></span></p> <p>TUTOR: Well, personally I think the stock market is to blame. I think they were expecting too much of the company and then inevitably it looked bad when it didn’t perform. The market should have had more realistic expectations. <span style="color: #ff0000;"><strong>And I disagree with you about the advertising campaign Karin. That’s where they could do with some innovation</strong></span> – to get sales kick-started. Anyway, let’s see what you come up with… [fade] <span style="color: #ff0000;"><strong> Q30</strong></span></p>
-
整理后需要
- 去掉
- 去掉 Q2 之类的符号
- 去掉所有的 html 标签
-
命令
# 执行前, 需要 : 唤起 ex命令模式 % s/ //g % s/<strong>s?Q.*</strong>//g % s/<.{-}>//g
-
1. 思路1: 直接执行
-
概述
- 直接执行
-
操作
- 进入文本
- 逐条执行命令
- 得出结果
-
问题
- 逐条执行命令
- 手打烦
- 复制粘贴也烦
- 有没有什么办法, 可以像执行脚本那么执行呢
- 当然有啦
- 逐条执行命令
2. 思路2: vim 内脚本执行
-
概述
- vim 内脚本执行
-
操作
-
整理脚本文件
# ex.vim, 拓展名其实无所谓 % s/ //g % s/<strong>s?Q.*</strong>//g % s/<.{-}>//g
-
用 vim 打开 demo.html
-
执行命令
# 执行前, 需要 : 唤起 ex命令模式 # 执行前有 确认 # 执行后如果不满意, 可以直接回退... :source ex.vim
-
-
结果
- 执行成功
- 调试方便
- 就算执行结果有问题, 可以立即使用 u 来回退
-
问题
- 如果可以对多个文本执行, 效率会比较高一点
3. 思路3: vim 内多文件脚本执行
-
概述
- vim 内多文件脚本执行
-
操作
-
整理脚本文件
-
用 vim 打开多个文档
> vim demo01.html demo02.html
-
执行命令
# 执行前, 需要 : 唤起 ex命令模式 source ex.vim | w
-
-
解释
-
|
- 相当于 ;
- 一行执行两条命令
-
|w
- 执行 ex 脚本后, 立即 w
- 否则的话, 会因为没有保存, 无法切换窗口
-
4. 思路4: 命令行多文件脚本执行
-
概述
- 命令行多文件执行
-
操作
-
整理脚本文件
-
命令
> vim -c 'argdo source ex.vim|w' demo01.html demo02.html
-
-
结果
- 类似 思路3
- 而且无法回退
ps
-
ref
- VIM学习笔记 Ex脚本 (Ex Script)
- 这个专栏写的还不错
- VIM学习笔记 Ex脚本 (Ex Script)
-
后续
-
ex 脚本
- 其实还有很多东西可以讲
- 用到了再慢慢学吧
-
流编辑器
- 编辑相关的 ex 脚本, 其实都可以转化为 sed 脚本
- 这个以后一定要做
- 编辑相关的 ex 脚本, 其实都可以转化为 sed 脚本
-