The Global Command

Both Ex commands and command-line commands are the same. They are the commands that start with a colon (:).

:g/pattern/command

The global command works by executing command against each line that matches the pattern

:g/console/d
remove all lines containing "console"
在運行g命令時,Vim對文件進行兩次掃描。在第一次運行時,它掃描每一行并標記與/console/模式匹配的行。一旦標記了所有匹配的行,它將第二次執(zhí)行,并在標記的行上執(zhí)行d命令。

run the global command on non-matching lines:
:g!/pattern/command
or
:v/pattern/command

:g/one|two/d
delete the lines containing either "one" or "two

:g/[0-9]/d
or
:g/\d/d
delete the lines containing any single digits

:g/0{3,6}/d
match the lines containing between three to six zeroes

.,3
between the current line and line 3

3,$
between line 3 and the last line

+n
n lines after the current line.

:g/./normal A;
add a ";" to the end of each line
/./ 是 “非空行”的模式。它匹配至少有一個字符的行,所以它匹配帶有"const"和"console"的行,而不匹配空行。


  • qa0A;<Esc>q
    在寄存器a中創(chuàng)建一個宏,這些行末尾添加一個逗號
    :g/const/normal @a
    使用

  • Recursive Global Command
    :g/console/g/two/d
    delete the second console.log
    首先,g將查找包含模式“console”的行,并將找到3個匹配項。然后第二個g將從這三個匹配中尋找包含模式“2”的行。最后,它將刪除該匹配。

  • 改變分隔符

:g@console@d
delete the lines containing "console"

g@one@s+const+let+g
using the substitute command with the global command, you can have two different delimiters

  • 默認的命令
    如果未指定命令,會執(zhí)行輸出命令
    :g/console
    print at the bottom of the screen all the lines containing "console".

  • Reversing The Entire Buffer
    :g/^/m 0
    reverse the entire file

  • Aggregating All TODOs

編寫代碼時,有時我會在正在編輯的文件中編寫TODOs
const one = 1;
console.log("one: ", one);
// TODO: feed the puppy

const two = 2;
// TODO: feed the puppy automatically
console.log("two: ", two);

const three = 3;
console.log("three: ", three);
// TODO: create a startup selling an automatic puppy feeder
跟蹤所有創(chuàng)建的todo可能很難。
要將所有TODOs復制到文件末尾,以便更容易地進行內(nèi)省,運行:
:g/TODO/t $

結(jié)果:
const one = 1;
console.log("one: ", one);

const two = 2;
console.log("two: ", two);

const three = 3;
console.log("three: ", three);

// TODO: feed the puppy
// TODO: feed the puppy automatically
// TODO: create a startup selling an automatic puppy feeder

:g/console/d _
刪除并不存儲到寄存器中

減少多個空行為一個空行

:g/^$/,/./-1j

通常,全局命令接受以下格式::g / pattern / command。 但是,您也可以使用以下格式運行全局命令::g / pattern1 /,/ pattern2 / command。 這樣,Vim將在pattern1和pattern2中應用命令:
/pattern1/是/^$/
它表示空行(帶有零字符的行)。
/pattern2/ is /./ with -1 line modifier
表示非空行(至少有一個字符的行)。-1表示上面的一行。
command is j, the join command (:j).
在這個上下文中,這個全局命令連接所有給定的行。

:g/^$/,/./j
將多個空行減少為無行

  • Advanced Sort
    const arrayB = [
    "i",
    "g",
    "h",
    "b",
    "f",
    "d",
    "e",
    "c",
    "a",
    ]

const arrayA = [
"h",
"b",
"f",
"d",
"e",
"a",
"c",
]
you need to sort the elements inside the arrays, but not the arrays themselves

:g/[/+1,/]/-1sort

結(jié)果:
const arrayB = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
]

const arrayA = [
"a"
"b",
"c",
"d",
"e",
"f",
"h",
]

  • :g/[/
    is the global command pattern.
  • /[/+1
    第一個模式。它匹配左方括號“[”。+1指的是它下面的一行 。
  • /]/-1
    第二種模式。它匹配右方括號“]”。-1指的是它上面的一行。
  • /[/+1,/]/-1
    then refers to any lines between "[" and "]".
  • sort
    is a command-line command to sort.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容