{"id":2575,"date":"2015-12-12T13:38:55","date_gmt":"2015-12-12T05:38:55","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2575"},"modified":"2015-12-12T13:38:55","modified_gmt":"2015-12-12T05:38:55","slug":"vim%e4%b8%ad%e7%9a%84%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2575.html","title":{"rendered":"Vim\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u5728Vim\u4e2d\u5220\u9664\u7a7a\u884c<\/h6>\n<pre class=\"lang:default decode:true\">:g\/^$\/d\n:g\/^\\s\\+\/d  #\u9700\u8981\u5bf9'+'\u8fdb\u884c\u8f6c\u4e49\n:g\/^\\s*$\/d  #'*'\u53ef\u4ee5\u76f4\u63a5\u7528\uff0c\u4e0d\u9700\u8981\u8f6c\u4e49\uff08\u5728Vim\u9ed8\u8ba4\u7684magic\u6a21\u5f0f\u4e0b\uff0c\u53ea\u6709\u300c. * ^ $\u300d\u4e0d\u9700\u8981\u8f6c\u4e49\uff0c\u5176\u5b83\u7684\u90fd\u9700\u8981\uff09<\/pre>\n<h6>\u5c06\u8fde\u7eed\u7684\u7a7a\u884c\u5220\u9664\u81f3\u5355\u7a7a\u884c<\/h6>\n<pre class=\"lang:default decode:true \">:g\/^$\/,\/.\/-j\t#Reduce multiple blank lines into a single blank line<\/pre>\n<h6>\u5220\u9664\u884c\u5c3e\u7684\u591a\u4f59\u7a7a\u683c<\/h6>\n<pre class=\"lang:default decode:true \">:%s\/\\s\\+$\/\/g<\/pre>\n<h6>\u67e5\u627e\u5339\u914d\u5b57\u7b26\u4e32\u51fa\u73b0\u7684\u6b21\u6570<\/h6>\n<pre class=\"lang:default decode:true \">\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u67e5\u627e\u5339\u914d\u5b57\u7b26\u4e32\u51fa\u73b0\u7684\u6b21\u6570\uff1a\n:%s\/\u5b57\u7b26\u4e32\/&amp;\/gn\n\u6700\u540e\u7684\u6807\u5fd7\u7b26n\uff0c\u4ee3\u8868\u53ea\u62a5\u544a\u5339\u914d\u7684\u4e2a\u6570\uff0c\u800c\u4e0d\u8fdb\u884c\u5b9e\u9645\u7684\u66ff\u6362\u3002\n\n\u5982\u4f55\u67e5\u627e\u5728\u5f53\u524d\u884c\u4e2d\u8be5\u5b57\u7b26\u4e32\u51fa\u73b0\u7684\u6b21\u6570\u5462\uff1f\n:s\/\u5b57\u7b26\u4e32\/&amp;\/gn\n\n\u5982\u4f55\u5728m\u548cn\u884c\u4e4b\u95f4\u67e5\u627e \u5b57\u7b26\u4e32 \u51fa\u73b0\u7684\u6b21\u6570\u5462\uff1f\n:m,ns\/\u5b57\u7b26\u4e32\/&amp;\/gn<\/pre>\n<h6>Vim\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5143\u5b57\u7b26<\/h6>\n<pre class=\"lang:default decode:true \">^\n$\n.\t#Matches any single character, but not an end-of-line\n*\t#Matches 0 or more of the preceding characters, ranges or metacharacters .* matches everything including empty line\n#\u5728Vim\u9ed8\u8ba4\u7684magic\u6a21\u5f0f\u4e0b\uff0c\u53ea\u6709\u4ee5\u4e0a\u7684\u300c. * ^ $\u300d4\u4e2a\u5143\u5b57\u7b26\u4e0d\u9700\u8981\u8f6c\u4e49\uff0c\u5176\u5b83\u7684\u90fd\u9700\u8981\n\n\\+\t#Matches 1 or more of the preceding characters...\uff08\u9700\u8981\u8fdb\u884c\u8f6c\u4e49\uff09<\/pre>\n<h6>Vim\u4e2d\u7684magic\u8bbe\u5b9a<\/h6>\n<p>\u8bbe\u5b9a\u65b9\u6cd5\u4e3a\uff1a<\/p>\n<p>:set magic &#8221; \u8bbe\u7f6emagic<br \/>\n:set nomagic &#8221; \u53d6\u6d88magic<br \/>\n:h magic &#8221; \u67e5\u770b\u5e2e\u52a9<\/p>\n<p><span style=\"color: #0000ff;\">\u7b80\u5355\u5730\u8bf4\uff0cmagic\u5c31\u662f\u8bbe\u7f6e\u54ea\u4e9b\u5143\u5b57\u7b26\u8981\u52a0\u53cd\u659c\u6760\u54ea\u4e9b\u4e0d\u7528\u52a0\u7684\u3002<\/span>\u4f8b\u5982\uff1a<\/p>\n<p><strong><span style=\"color: #ff0000;\">magic(\\m)\uff1a\u9664\u4e86 $ . * ^ \u4e4b\u5916\u5176\u4ed6\u5143\u5b57\u7b26\u90fd\u8981\u52a0\u53cd\u659c\u6760\u3002<\/span><\/strong><br \/>\n<strong>nomagic(\\M)\uff1a\u9664\u4e86 $ ^ \u4e4b\u5916\u5176\u4ed6\u5143\u5b57\u7b26\u90fd\u8981\u52a0\u53cd\u659c\u6760\u3002<\/strong><\/p>\n<p>\u8fd9\u4e2a\u8bbe\u7f6e\u4e5f\u53ef\u4ee5\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u901a\u8fc7 \\m \\M \u5f00\u5173\u4e34\u65f6\u5207\u6362\u3002 \\m \u540e\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u6309\u7167 magic \u5904\u7406\uff0c\\M \u540e\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6309\u7167 nomagic \u5904\u7406\uff0c \u800c\u5ffd\u7565\u5b9e\u9645\u7684magic\u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<p>\/\\m.* # \u67e5\u627e\u4efb\u610f\u5b57\u7b26\u4e32<br \/>\n\/\\M.* # \u67e5\u627e\u5b57\u7b26\u4e32 .* \uff08\u70b9\u53f7\u540e\u9762\u8ddf\u4e2a\u661f\u53f7\uff09<\/p>\n<p>\u53e6\u5916\u8fd8\u6709\u66f4\u5f3a\u5927\u7684 \\v \u548c \\V\u3002\u00a0<strong><span style=\"color: #ff0000;\">\\v\uff08\u5373 very magic \u4e4b\u610f\uff09<\/span><\/strong>\uff1a\u4efb\u4f55\u5143\u5b57\u7b26\u90fd\u4e0d\u7528\u52a0\u53cd\u659c\u6760\u00a0<strong><span style=\"color: #ff0000;\">\\V\uff08\u5373 very nomagic \u4e4b\u610f\uff09<\/span><\/strong>\uff1a\u4efb\u4f55\u5143\u5b57\u7b26\u90fd\u5fc5\u987b\u52a0\u53cd\u659c\u6760\u3002\u4f8b\u5982\uff1a<\/p>\n<p>\/\\v(a.c){3}$ \u00a0 # \u67e5\u627e\u884c\u5c3e\u7684abcaccadc<br \/>\n\/\\m(a.c){3}$ \u00a0# \u67e5\u627e\u884c\u5c3e\u7684(abc){3}<br \/>\n\/\\M(a.c){3}$ \u00a0# \u67e5\u627e\u884c\u5c3e\u7684(a.c){3}<br \/>\n\/\\V(a.c){3}$ \u00a0# \u67e5\u627e\u4efb\u610f\u4f4d\u7f6e\u7684(a.c){3}$<\/p>\n<p><span style=\"color: #0000ff;\"><strong>\u9ed8\u8ba4\u8bbe\u7f6e\u662f magic\uff0cvim\u4e5f\u63a8\u8350\u5927\u5bb6\u90fd\u4f7f\u7528magic\u7684\u8bbe\u7f6e\uff0c\u5728\u6709\u7279\u6b8a\u9700\u8981\u65f6\uff0c\u76f4\u63a5\u901a\u8fc7 \\v\\m\\M\\V \u4e34\u65f6\u4fee\u6539\u8bbe\u7f6e\u5373\u53ef\u3002<\/strong><\/span>Vim\u7684\u5e2e\u52a9\u6587\u4ef6\u975e\u5e38\u6709\u7528\uff0c\u5173\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<p>:h pattern<br \/>\n:h magic<br \/>\n:h perl-patterns<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/vimregex.com\/\" target=\"_blank\">http:\/\/vimregex.com\/<\/a><\/li>\n<li><a href=\"http:\/\/www.linuxdiyf.com\/linux\/1369.html\" target=\"_blank\">http:\/\/www.linuxdiyf.com\/linux\/1369.html<\/a><\/li>\n<li><a href=\"http:\/\/vimdoc.sourceforge.net\/htmldoc\/motion.html#object-select\" target=\"_blank\">http:\/\/vimdoc.sourceforge.net\/htmldoc\/motion.html#object-select<\/a><\/li>\n<li><a href=\"http:\/\/liuzhijun.iteye.com\/category\/270228\" target=\"_blank\">http:\/\/liuzhijun.iteye.com\/category\/270228<\/a><\/li>\n<li><a href=\"http:\/\/vimdoc.sourceforge.net\/htmldoc\/pattern.html#regexp\" target=\"_blank\">http:\/\/vimdoc.sourceforge.net\/htmldoc\/pattern.html#regexp<\/a> #Vim\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<li><a href=\"http:\/\/vimdoc.sourceforge.net\/htmldoc\/cmdline.html\" target=\"_blank\">http:\/\/vimdoc.sourceforge.net\/htmldoc\/cmdline.html<\/a> #Vim\u4e2d\u7684\u547d\u4ee4<\/li>\n<\/ul>\n<h6>\u66f4\u591a\u7684\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/linux.cn\/article-4669-1.html\" target=\"_blank\">http:\/\/linux.cn\/article-4669-1.html<\/a><\/li>\n<li><a href=\"http:\/\/xmodulo.com\/useful-vim-commands.html\" target=\"_blank\">http:\/\/xmodulo.com\/useful-vim-commands.html<\/a><\/li>\n<li><a href=\"http:\/\/vim.wikia.com\/wiki\/Vim_Tips_Wiki\" target=\"_blank\">http:\/\/vim.wikia.com\/wiki\/Vim_Tips_Wiki<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3032030\/how-does-g-j-reduce-multiple-blank-lines-to-a-single-blank-work-in-vi\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3032030\/how-does-g-j-reduce-multiple-blank-lines-to-a-single-blank-work-in-vi<\/a><\/li>\n<li><a href=\"http:\/\/vim.wikia.com\/wiki\/Remove_unwanted_empty_lines\" target=\"_blank\">http:\/\/vim.wikia.com\/wiki\/Remove_unwanted_empty_lines<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/72739\/how-to-remove-multiple-blank-lines-from-a-file\" target=\"_blank\">http:\/\/unix.stackexchange.com\/questions\/72739\/how-to-remove-multiple-blank-lines-from-a-file<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10760326\/merge-multiple-lines-two-blocks-in-vim\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/10760326\/merge-multiple-lines-two-blocks-in-vim<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u5728Vim\u4e2d\u5220\u9664\u7a7a\u884c :g\/^$\/d :g\/^\\s\\+\/d #\u9700\u8981\u5bf9&#8217;+&#8217;\u8fdb\u884c\u8f6c\u4e49 :g\/^\\s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,12],"tags":[377,19,43],"class_list":["post-2575","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-tools","tag-regex","tag-tips","tag-vim"],"views":3038,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2575"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2575\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}