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.

The syntax of the command is :

:[range]g[lobal]/{pattern}/[cmd]

See :h :global.

86 questions
15
votes
2 answers

How to join all lines together which matching pattern?

I would like to join lines together only for lines which have certain pattern (such as ;), however when using g/;/j it doesn't work as expected unless called couple of times. For example the following content: a 1; 2; 3; 4; 5; b 6; 7; 8; 9; c when…
kenorb
  • 18,433
  • 18
  • 72
  • 134
8
votes
5 answers

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 lines. Example: Consider this file. Say I want to…
Pandu
  • 183
  • 3
4
votes
3 answers

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 *b. Second answer c. Third answer d. Fourth…
Blair
  • 43
  • 2
4
votes
1 answer

How to use :g to insert?

Let's say I have these lines: a # c b ## d and i want to insert # on a line starting with a # so that I get this: a ## c b ### d I was not able to find anything in the vim help, but I was able to get this far: g/^#/ - what should come after the…
Dylanthepiguy
  • 395
  • 5
  • 13
2
votes
3 answers

Replace current line with filtered lines

For example, to replace current line with all lines starting with '# ' , I use :.! grep '^\# ' % I know about the :g command that can filter lines to perform operations like :g/call/d :g/cat/ s/animal/mammal/g :.,+20 g/^# / normal >> But I am not…
Sundeep
  • 1,086
  • 7
  • 18
2
votes
1 answer

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 each line and execute a d$ (or just a D), but what…
Davo
  • 176
  • 3
  • 12
2
votes
2 answers

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 :g/pattern/m0 reverses the file which is mainly…
Steve
  • 276
  • 1
  • 13
2
votes
1 answer

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\{-}\)/normal! 2jibar \2 but the callback to the capture…
stevensonmt
  • 123
  • 4
0
votes
1 answer

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 from register copy to end of file, jump to end…
fbence
  • 677
  • 4
  • 12
0
votes
2 answers

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 table. Imagine there are 3 footnotes like…
neverdimed
  • 97
  • 6
0
votes
1 answer

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 syntax hightlighting applied to this output. Possible?
0
votes
1 answer

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 ----------- Following method is close to what i'm looking…
0
votes
2 answers

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 inline all usages of such variables automatically…
user1685095
  • 1,141
  • 11
  • 24