简体   繁体   English

function 在 vim 修复报价

[英]function in vim to fix quotations

I'm trying to create a function in vim to be activated by a shortcut and go through my document and re-quote the strings using Python-like syntax.我正在尝试通过我的文档在 vim 中创建一个 function 以通过快捷方式和 go 激活,并使用类似 Python 的语法重新引用字符串。 That is:那是:

  • "something" -> 'something' "something" -> 'something'
  • '''docstring''' -> """docstring""" '''docstring''' -> """docstring"""
  • "'" -> "'" (stays the same) "'" -> "'" (保持不变)
  • '"' -> '"' (stays the same) '"' -> '"' (保持不变)

Given my limited knowledge of python functions I came up with this in my .vimrc :鉴于我对 python 函数的了解有限,我在.vimrc中提出了这个:

function! Fixquotes()
   :silent! %s/"\([^"]*\)"/'\1'/g
   :silent! %s/'""/"""/
   :silent! %s/""'/"""/
   :silent! %s/'''/"""/g
endfunction

inoremap <C-f> <esc>mk:call Fixquotes()<CR>`kli
noremap <C-f> mk:call Fixquotes()<CR>`k

It kinda work, except for the case where I have "'" since the first substitution will turn it into ''' and the last will turn it into """ .它有点工作,除了我有"'"的情况,因为第一个替换会将它变成'''而最后一个会将它变成"""

Does anyone have any recommendation?有人有什么建议吗?

give this a try:试试这个:

function! Fixquotes()
   :silent! %s/\v([^'"]|\_^)\zs"([^"']*)"\ze([^'"]|\_$)/'\2'/g
   :silent! %s/'''/"""/g
endfunction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM