在行尾切换分号(或其他字符)

编程入门 行业动态 更新时间:2024-10-26 05:18:24
本文介绍了在行尾切换分号(或其他字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在行尾附加(或删除)分号是一种常见的操作.然而像 A; 这样的命令会修改当前光标位置,这并不总是理想的.

Appending (or removing) a semicolon to the end of the line is a common operation. Yet commands like A; modify the current cursor position, which is not always ideal.

是否有一种直接的方法来映射命令(例如 ;;)来切换分号是否出现在行尾?

Is there a straightforward way to map a command (e.g. ;;) to toggle whether a semicolon appears at the end of a line?

我目前在我的 vimrc 中使用这个命令来追加:

I'm currently using this command in my vimrc to append:

map ;; A;<Esc>

推荐答案

这样的事情会起作用

nnoremap ;; :s/\v(.)$/\=submatch(1)==';' ? '' : submatch(1).';'<CR>

这使用了一个替代命令并检查最后一个字符是否是分号,如果是,则将其删除.如果它不是将其附加到匹配的字符.这在替换部分使用 \= 来执行 vim 表达式.

This uses a substitute command and checks to see if the last character is a semicolon and if it is it removes it. If it isn't append it to the character that was matched. This uses a \= in the replacement part to execute a vim expression.

如果您不想匹配任何任意字符,您可以将其包装在一个函数中并传入您想要匹配的字符.

If you wan't to match any arbitrary character you could wrap it in a function and pass in the character you wanted to match.

function! ToggleEndChar(charToMatch)
    s/\v(.)$/\=submatch(1)==a:charToMatch ? '' : submatch(1).a:charToMatch
endfunction

然后映射将切换分号.

nnoremap ;; :call ToggleEndChar(';')<CR>

这篇关于在行尾切换分号(或其他字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 18:09:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分号   或其他   字符   行尾

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!