Questions tagged [global-command]

Questions about the usage of the global command `:g` which executes an Ex command (default ":p") on the lines within a range a where a pattern matches.

Filter by
Sorted by
Tagged with
4 votes
3 answers
169 views

Equivalent of backreferences in a g command?

I'm formatting a multiple-choice quiz to be imported into an LMS. The questions are currently formatted like this, with the asterisk indicating the correct solution: 1. Question text a. First answer *...
Blair's user avatar
  • 43
0 votes
1 answer
25 views

Deleting multiple pattern simultaneously

I would like to execute multiple deletions. The problem when a pattern is not found all deletions are cancelled. I used the e flag to tell vim the not founding a pattern is not an error, but it still ...
Ayoub Falah's user avatar
2 votes
2 answers
132 views

Just the first line is deleted

Given the following file: Stand a b c d e f Stand a b c I would like to remove the lines that match the following pattern Stand\(.*\n\)\{4} To achieve that I executed the command :g/Stand\(....
Ayoub Falah's user avatar
1 vote
2 answers
77 views

Ex global command is silenced, why is execution still stopped on error (Neovim)?

I have a sample text: 123 456 If I delete the first line, why does :silent! execute 'g/123/d' | execute 'g/456/d' work while :silent! g/123/d | g/456/d doesn't work, unless I bring back the first ...
danko's user avatar
  • 11
0 votes
1 answer
33 views

Is there a way to apply syntax highlighting to the results of :g?

I do a lot of work with log files, and I use a lot of hi def foo and syn match foo in my .vimrc. Is there a way to apply that syntax formatting to the results of a :g command?
Brian J's user avatar
0 votes
1 answer
40 views

Apply normal command to all regex matches on the same line

This comment convinced me that I don't need multiple cursors, however, it's missing one feature that multi-cursor can do, which is to perform actions on multiple regex matches on the same line. :g is ...
Wong Jia Hau's user avatar
1 vote
1 answer
27 views

matching with single character > or lines that start with >\s

I want to delete any lines that either match ^>$ or >\s I have something like this, but this doesn't work:nnoremap dq :.,$g/^>[\s$]/d<CR> Here's a small sample where it should delete ...
fbence's user avatar
  • 603
0 votes
1 answer
34 views

using g to paste matching lines under cursor

I wanted to copy a series of lines to a specific place in a file, and these are the options I see, all with g, the first two from the power of g clear register, copy matching lines to register, paste ...
fbence's user avatar
  • 603
0 votes
2 answers
23 views

How can I strip footnotes using :global?

I have a very large table covering many pages in a document. The same footnote appears under the table on every page and I want to strip the entire document of these lines so I'm left only with the ...
neverdimed's user avatar
0 votes
1 answer
54 views

Appending text to lines maintaining context

I'm trying to write some script (a function) which I can use to add some additions to a BASH script I'm modifying. This BASH script automates the building of a series of packages. The part of the ...
Andrew Falanga's user avatar
0 votes
1 answer
33 views

Show "hit-enter message" with Syntax Highlighting?

To get a view of all function definitions in a file I type :g/function/# Vim displays each line with the word "function" in a small "message window". I'd like to have my defined ...
Zach_is_my_name's user avatar
0 votes
4 answers
3k views

How to "match until"?

I have a CSV filled with text messages, and I want to be able to easily filter by sender/receiver. The last field indicates "SMS", "iMessage" or "WhatsApp"; the previous ...
Harv's user avatar
  • 219
3 votes
1 answer
247 views

Understanding the use of ., in the global command

While doing some updates to my .vimrc file today, I stumbled across this for condensing blank lines (really empty lines) into a single empty line. :map ;b GoZ<Esc>:g/^$/.,/./-j<CR>Gdd ...
Andrew Falanga's user avatar
1 vote
0 answers
38 views

What is the empty space in this document and how do I. get rid of them

Here is my document: What is the thing that my cursor is on? It is definitely not a whitspace as all white space should be marked with ␣. And more importantly, how do I get rid of it? I have tried :g/...
jxhyc's user avatar
  • 135
0 votes
2 answers
124 views

g[lobal] with a s[ubstitution]

I would like to add a dot after the first number in a line. I tried using the command below. :g/\v^\d/exe "norm!:s/\v(\d)/\1\./^M" The command above does not work. I recorded the macro @q=':...
Root's user avatar
  • 1
3 votes
1 answer
794 views

Delete all lines not matching a multi-line selection

I'm aware that :g/<body/ .,/body>/d can be used to delete everything within the multi-line selection betweeh <body and body>. I'm also aware that :g!/<pattern>/d can be used to ...
Rufus's user avatar
  • 133
1 vote
0 answers
220 views

in vimscript how to write the code a CTRL-C to be executed

I run a sourced vimscript like this: exec 'g#' . WE_var_searchmode . ' ' . WE_var_pattertosearch . '#exec " A | B | C | make_CTRL-C | ... | LAST " ' The outer execute '...' is needed to ...
Anton Wessel's user avatar
8 votes
5 answers
813 views

Is there a simple equivalent of :g/PATTERN/m0 that doesn't reverse the matched lines?

Occasionally I want to move all lines in a file to the top of that file. :g/PATTERN/m0 almost does what I want, but because :g operates one line at a time in order, it will also reverse the affected ...
Pandu's user avatar
  • 183
1 vote
1 answer
396 views

Why does this vim command fail?

I'm trying to process large error logs and part of that is filtering out lines I don't need to see. Here's what I'm running in vim: :g/.*rsync: stat.*/d | %s/\(.*mkstemp "\)\(.* \/\)\(.*$\)/\2/ | ...
Harv's user avatar
  • 219
0 votes
1 answer
203 views

How to execute multiple global commands in Vim not sequentially?

Using following test input: *********** aaa bbb ccc *********** ddd eee fff ----------- ggg hhh iii ----------- jjj kkk lll ----------- *********** aaa bbb ccc *********** ddd eee fff ----------- ...
dinan5m3's user avatar
1 vote
2 answers
367 views

Something like :g/re/p - but only print the matches instead of whole lines?

Say we have the following code: Lorem ipsum. <input name="test1"> <input name="test2"> Lorem ipsum some long text possibly so long you lose your overview. <input ...
MiK's user avatar
  • 143
2 votes
3 answers
72 views

How to delete a range of lines not up to N lines under a lines matching a pattern?

Say I have a document cat dog xxx 1 2 3 cow xxx a b c horse I would like to be able to run a command that will end up with xxx 1 2 3 xxx a b c whereby the transformation is defined by matching all ...
bradgonesurfing's user avatar
8 votes
2 answers
796 views

Deleting lines matching a pattern and put them into the buffer

I am trying to do this: deleting lines matching a pattern and then copy these lines into some other place in the file. :g/PATTERN/d does delete the lines but when I use p or P, only the last line is ...
Gergely's user avatar
  • 253
5 votes
1 answer
746 views

Join multiple lines based on pattern

I have text bounded by quotes as follows "This is an example of some text" I want to join these into one line. e.g. "This is an example of some text" I have tried commands like ...
daviewales's user avatar
1 vote
2 answers
61 views

Why is my global command coupled with a range command matching more lines than expected?

I have this file over here. <ul class="social-icons"> <li><a href=""><span class="social_linkedin_square"></span></a></...
mkayyash's user avatar
1 vote
1 answer
34 views

Why does this pattern make :global work only on the first line of the highligted match?

\/\*.*\(\n\s\*.*\)*\n\s\*\/ is a pattern which I cooked up to match some multi-line C comments, i.e., in the format of: /* Comment Text * Comment Text * Comment Text */ Searching for the pattern ...
Monke's user avatar
  • 13
1 vote
1 answer
606 views

How do I delete matching lines in multiple files at once?

I want to delete lines containing a search term from multiple files. I used bufdo or argdo for similar tasks such performing a replacement with s over multiple files. However, bufdo g/SearchString/d | ...
l4mpi's user avatar
  • 805
0 votes
1 answer
98 views

:command to abort sequence?

To be honest upfront, I wanted to use global to find the first non-matching line, as in v,/usr/lib/, abort (having slashes in the search pattern is the reason for using g instead of / — I'm editing a ...
usretc's user avatar
  • 345
2 votes
2 answers
940 views

g/pattern/d but check first

I'd like to use Ex mode :g/pattern/d to delete most (but not all) of the rows containing pattern, but I want to see each one first and confirm before deleting, similar to :s/pattern/foo/gc. Is there a ...
Josh Friedlander's user avatar
2 votes
1 answer
568 views

How do I append to the unnamed register?

In particular, I'd like to append to the unnamed register with the :global command.
Jay's user avatar
  • 201
3 votes
2 answers
71 views

Syntax clarification on a global sort command

I was looking at a global command that I saved from long time ago. I did not understand how it was a valid syntax, but apparently the syntax was valid because the command worked. The command: :g/\v\{/...
Iggy's user avatar
  • 165
4 votes
2 answers
1k views

Delete blank lines within range

Suppose I have the file: begin asdf asdf end begin asdf end begin asdfasdf end How can I use something like the global command to delete every empty line in between 'begin' and 'end'? :/begin/,...
hwong557's user avatar
  • 143
3 votes
3 answers
496 views

Using :g to yank in quotes across multiple lines

I was reading through the vim wiki about the power of g https://vim.fandom.com/wiki/Power_of_g and would like to do something similar to :g/pattern/y A but a little bit different. I have an html file ...
instantaphex's user avatar
2 votes
1 answer
2k views

How to truncate all lines at a certain length?

Much like this question, I've failed to put this together successfully on my own. I want to truncate lines in my file so that no line is over 100 characters in length. I can go to position 100 on ...
Davo's user avatar
  • 176
2 votes
0 answers
504 views

neovim hlsearch not working

I'm testing neovim and for many thing it is working way faster than vim (I use many plugins ~80, and I can really tell the difference with neovim). All the same config files worked out of the box, but ...
Blasco's user avatar
  • 245
2 votes
2 answers
235 views

Is it possible to use a reverse range in global search

I have an error report where often the known errors are in the top few thousand lines, formatting issue rather than quantity unfortunately. I want to run :g/pattern/ from the end of the file. I know ...
Steve's user avatar
  • 276
0 votes
1 answer
310 views

function to search for a pattern

I just recently found a very nice command to search for a pattern in a document, and display all hits: :g/pattern/z#.1 Since the command is a bit cumbersome to type, I would like to make a shortcut, ...
pfnuesel's user avatar
  • 674
2 votes
2 answers
308 views

How can I delete lines fit a pattern to a register or a new file?

I can use the powerful :g command to delete lines that contain a certain pattern like this: :g/^\va pattern/d I thought I should paste the deleted content to a new place but I cannot find all ...
Lerner Zhang's user avatar
4 votes
1 answer
319 views

Unmapping the global mappings on a per-buffer basis

I have the following mapping in tnoremap <Esc> <C-\><C-n> init.conf to exit insert mode while in the terminal. In certain terminal buffers I would like to not have that global ...
TheMeaningfulEngineer's user avatar
0 votes
2 answers
489 views

What's the simplest way of inlining a variable?

I've recently refactored vim-colors-solarized and I've stumbled on a code like this let s:foo = s:bar " used in multiple places " ... many lines let s:goo = s:boo " used in multiple places How can I ...
user1685095's user avatar
  • 1,141
2 votes
2 answers
319 views

How to ignore a failed :g (global) command in a macro?

Part of a macro I'm writing deletes any blank lines, and then does some more work (joining the expected two remaining lines and then appending to them). Simplified version: let @m=':g/^$/d^MggJA ^[' ...
Owen's user avatar
  • 203
3 votes
1 answer
194 views

replace or operate within regex match

underlying question: Is there a way to operate on text within a regex match? I.e. is there an analog to the 'g' command that operates on match substrings instead of lines usecases: One example of ...
Catskul's user avatar
  • 132
0 votes
1 answer
280 views

Transform lines: prepend with incrementing number and string and append with string for each line in one step

I have the following lines: abc07 bca08 xyz17 And I would like to transform it to: [0]="abc07" \ [1]="bca08" \ [2]="xyz17" \ Based on this question, I can do the first part and the end ...
Aba's user avatar
  • 103
4 votes
4 answers
470 views

How to run a python command based on a matched group of a pattern?

Let say I want to run a command that uses a matched group of a pattern for each line matching the pattern. The natural choice is :g. With the command :s, there is no problem. If i would do :g/aaa\(....
eyal karni's user avatar
  • 1,026
4 votes
2 answers
2k views

Using global command on Quickfix entries [duplicate]

I find and replace a pattern in all the files in my project (which is a big one). After the search is done, quickfix window is opened with the matches. The matches are in different files in different ...
Salahuddin Ahmed's user avatar
2 votes
1 answer
218 views

Insert text including capture group from search using global command

I would like to search for a pattern in a range of lines and append additional text on another line that would include part of the search pattern. Something like this: [range]g/\(foo \)\(\w\{-}\)/...
stevensonmt's user avatar
1 vote
0 answers
124 views

surround plug-in within global command

I've been unable to use surround within a global command, For example: g/^/ norm! yss" seems to do nothing or replaces the entire line by the contents of the clipboard. This is consistent with ...
justerman's user avatar
  • 111
2 votes
1 answer
67 views

:global, how to use matched pattern in the cmd of :g/pat/cmd?

Give a buffer each line contain a filename. how to use r! cmd on the filename, within a :g command? eg, BEFORE PROCESSING: file1.txt file2.txt AFTER PROCESSING (with wc -l): file1.txt 12 ...
qeatzy's user avatar
  • 940
0 votes
2 answers
60 views

limiting global search&replace to matches that occur immediately to the right of the cursor

If I have this line: a{word} a{20] a{word} a{20] and type :g/a{.\{-}\]/s/{/\[/gc vi prompts me to change all four occurrences of { to [. I want it to prompt me to replace only the matches ...
Leo Simon's user avatar
  • 715
2 votes
2 answers
389 views

How to remove lines which have lower numbers than before, and keep only the sequential?

In order to parse certain log file and make a plot of sequential numbers, I need to filter out the lines with numbers which appearing lower than in the previous lines. For example, given the list of ...
kenorb's user avatar
  • 18k