[英]vim: copy via regex and paste
I'm looking to write a vim macro (not that I know how yet) eventually, as I'm sure I can do this in vim, but I don't know the basics yet. 我希望最终编写一个vim宏(不是我还不知道),因为我确定我可以在vim中做到这一点,但是我还不了解基本知识。
I want to grab the branch name (or part of it anyway - that part of the regex should be straightforward) from a git commit message, and paste it, with a colon, to the top of the file, and hopefully leave the cursor at the end of that. 我想从git commit消息中获取分支名称(或者仍然是它的一部分-正则表达式的这一部分应该很简单),然后用冒号将其粘贴到文件顶部,并希望将光标放在到此为止。
So the file looks a bit like this when I start: 因此,当我启动时文件看起来像这样:
$ <-- cursor here
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch JIRA-1234-some-description-here
# Your branch is up to date with ...
I want to capture the /On branch \\([AZ]*-[0-9]*\\)/
, move back up to the top, paste it, and a colon, leaving the cursor after the colon, eg: 我想捕获
/On branch \\([AZ]*-[0-9]*\\)/
,移回顶部,粘贴并添加冒号,将光标留在冒号之后,例如:
JIRA-1234: $ <-- cursor here
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch JIRA-1234-some-description-here
# Your branch is up to date with ...
Ideally, also leave me in insert mode there. 理想情况下,也让我保持插入模式。
Assuming you are using fugitive.vim you can use fugitive#head()
to get the current branch name. 假设您正在使用fugitive.vim ,则可以使用
fugitive#head()
获取当前的分支名称。 You can probably do something like this (put in vimrc
file): 您可能可以执行以下操作(放入
vimrc
文件中):
augroup MyCommit
autocmd!
autocmd FileType gitcommit call setline(1, substitute(fugitive#head(), '^\([A-Z]\+-[0-9]\+\).*', '\1: ', ''))
augroup END
Note: I have not tested this in anyway. 注意:无论如何我都没有测试过。
You could use this function: 您可以使用以下功能:
function! MyFunction()
g/On branch \([A-Z]*-[0-9]*\)/exe "co0 | 1s/.* \\(.*-\\d\\+\\).*/\\1:/g"
normal $
endfunction
and then map it: 然后将其映射:
nmap <leader>f :call MyFunction()<cr>
Explanation of function: the global command g/{pat}/[cmd]
executes the command [cmd]
on lines where pattern {pat}
matches. 功能说明:全局命令
g/{pat}/[cmd]
在模式{pat}
匹配的行上执行命令[cmd]
。 You've already provided {pat}
in your question. 您已经在问题中提供了
{pat}
。 Then, to execute more than one command I recommend to use exe
. 然后,要执行多个命令,我建议使用
exe
。 Here exe
copies the matched line to the first line ( co0
) and performs the replacement ( 1s/.../\\\\1:/g
). 此处
exe
将匹配的行复制到第一行( co0
)并执行替换( 1s/.../\\\\1:/g
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.