在Vim中,如何将多行转换为单行

zf2sa74q  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(106)

我有这个文本:

sudo apt-get install -y --no-install-recommends \
    culmus \
    fonts-beng \
    fonts-hosny-amiri \
    fonts-lklug-sinhala \
    fonts-lohit-guru \
    fonts-lohit-knda \

字符串
我希望它是:

sudo apt-get install -y --no-install-recommends culmus fonts-beng fonts-hosny-amiri fonts-lklug-sinhala fonts-lohit-guru fonts-lohit-knda


它可以包含在多个命令中,不一定是一个复杂的命令。

busg9geu

busg9geu1#

你要做的是去掉反斜杠、换行符和前导空格。在Vim中最简单的方法是使用正则表达式替换。

:%s@\\\n\s*@@

字符串
说明:

:%    " Over the whole file
s     " Perform a substitution (:help :s) s@search@replace@
@     " This is a delimiter: you can choose different symbols, but I 
      " picked @ here as I think it's clearer to see with all the backslashes
      " (:help pattern-delimiter)
\\    " Backslashes need to be escaped, so \\ means match a single backslash
\n    " Match a new-line (note that if this is in the replace bit of the
      " search-and-replace, you'd use \r)
\s*   " Match zero or more spaces (:help /character-classes and :help /star)
@     " Repeat of the delimiter to end the search part of search-and-replace
      " Nothing between the delimiters as we're replacing with nothing
@     " Another delimiter to end the replace part of search-and-replace

相关问题