简体   繁体   中英

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. That is:

  • "something" -> 'something'
  • '''docstring''' -> """docstring"""
  • "'" -> "'" (stays the same)
  • '"' -> '"' (stays the same)

Given my limited knowledge of python functions I came up with this in my .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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