vim - 如何通过在文件开头插入日期和一些文本来启动 vim?
问题描述
我正在编写一个 bash 脚本来创建类似这样的日志文件,其中最新的条目首先出现在文件中:
2019-07-26 Looks like SSD needs replacing
2019-07-25 db backup failed
如果命令行中没有包含文本,只需启动 vim。如果命令行中包含文本,请插入日期和该文本,例如
edlog db backup failed
从概念上讲,它在我的脑海中看起来像这样,但只有第一个 -c 命令按预期工作:
!/bin/bash
if [ $# -eq 0 ]
then
nvim ~/log.txt
else
# :r will Insert date at top of file.
# $\t moves to end of line and inserts a tab
# $* appends contents of command line
nvim -c ":r! date +\%F" -c "$\t" -c "$*" ~/log.txt
fi
解决方案
这是我的解决方案。我非常愿意接受批评和纠正。
#!/bin/bash
# Appends to a log file if you type a message, or
# just brings up vim in case you want to log several
# lines' worth
# Nothing added to command line, so just bring up vim:
if [ $# -eq 0 ]
then
nvim ~/Dropbox/log.txt
else
# User entered something like this:
# edlog Extra backup last night due to OS update
LINE=" $*"
# Format date like 07/25/2019
# Start inserting text after the date.
# Insert all arguments on command line, like "Re-test db"
# Then write out file, and quit back to shell
nvim -c ":pu=strftime('%m/%d/%Y')" -c ":startinsert!" -c "norm a$LINE" -c "wq" ~/Dropbox/log.txt
fi
推荐阅读
- user-interface - Unity:画布从场景中生成
- wordpress - 在 Wordpress 文件中的位置
- linux - Linux中命令行选项的正负形式有哪些
- sql - 如何在oracle中优先考虑特定记录?
- java - 在 mono.compose() 中添加 doOnSuccess() 与简单的 mono.doOnSuccess() 有什么好处吗
- php - 根据 Woocommerce 中的父产品类别更改标题
- google-apps-script - 谷歌表格根据今天的日期隐藏行
- waze - 历史报表数据
- php - 将表单数据插入 SQL 表
- python - 避免被识别为垃圾邮件