Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

2
votes
1answer
49 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 ...
0
votes
0answers
35 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 ...
0
votes
2answers
47 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 ^[' ...
3
votes
1answer
54 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 ...
0
votes
1answer
37 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 ...
3
votes
4answers
106 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\(....
2
votes
2answers
211 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 ...
1
vote
1answer
40 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\{-}\)/...
2
votes
1answer
43 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 ...
2
votes
2answers
83 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 ...
1
vote
0answers
47 views

indenting with ':g' command not working from the command line

When I need to work with some scrambled javascript/json code, one of my first steps is to introduce proper line breaks, followed by proper brace based indentation. For the later, I found the following ...
2
votes
1answer
427 views

Differences between local & global

From :h 'ar': ...If this option has a local value, use this command to switch back to using the global value: :set autoread< What do local & global value exactly mean? I ...
0
votes
2answers
56 views

Using -c command line option for Vim 7.4 on Windows produces errors

The following commands work fine when I issue them from the command mode in Vim: :%s/\(^\s\+\)\(\S\+.*)\s*\){$/\1\2\r\1{/g :g/^\s*\/\/$/d But when I run the above commands as a script as below: ...
1
vote
1answer
44 views

Converting from A to B using mapped key

I've posted a number of times to try and get closer to what I'm trying to do. My problem is I have got the commands I need to make the conversion happen, however I cannot successfully map it to a key. ...
1
vote
1answer
59 views

How to remove all words which doesn't match the pattern?

I've the following content: a1a foo bar buzz foo b2b bar buzz foo bar c3c buzz foo bar buzz d4d where I can match all words consisting only alpha characters by the following pattern: /\<[a-zA-Z]*...
2
votes
3answers
941 views

How to execute command on every matching pattern, not just lines?

I know in vim I can use :global to execute commands on the matching lines. But I'm wondering if it's possible to execute those commands on every matching pattern, not just lines, consider following ...
2
votes
1answer
898 views

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 ...
3
votes
1answer
444 views

Duplicate line and replace part of line multiple times

Considering this type of Json structure : [ { "id": "0000000000001", "en-US": "String 1" }, { "id": "0000000000002", "en-US": "String 2" } ... ] I ...
3
votes
2answers
88 views

Substitute text on one line with capture group from previous line?

Suppose I've copied a bunch of lines of boilerplate code that I need to copy multiple times over, and then change in two separate places, in the same way: it 'includes sessions#new' do expect(...
1
vote
2answers
739 views

vim comment out every line of text containing a pattern using the 'global' command

In my file containing latex source code, I wish to comment out all the lines matching a pattern (specifically, the pattern that I am interested in is \draw, including the \ character). I have magic ...
3
votes
2answers
430 views

How to filter text matched by global command?

I have a log file full of arbitrary lines, interspersed with some lines which are a proper xml sequence, with all the tags on a single line (for each sequence). I want to simply process this so that ...
0
votes
2answers
100 views

How to execute global/substitution command asynchronously?

With the release of vim 8, I was wondering if there is a way to execute :s or :g asynchronously.
0
votes
1answer
241 views

Run a command on every match of a search (not on the line)

I am trying to run a command on every occurrence of a search match. So, If i have some thing like this where a search match matches only these two anchor tags, .................. <a src="****" &...
1
vote
2answers
77 views

Joining consecutive commas using ex-mode

I have a large list of scores that looks likes this: Michelle ,46419 ,32413 Frank ,46412 ,33810 Steve ,28333 ,17830 ,37 Joe ,136 ,93 Alicia ,121300 ,84146 How can I move my list in Vim so it is re-...
4
votes
1answer
145 views

indent command does not indent matched lines

I have file where I would like to indent all the lines between curly braces: html { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } body { line-height: 1....
3
votes
1answer
304 views

Run global with range from within function

I have a function somewhat like this: function! MyFunction() g/\v.(\n\n|\n*%$)/execute "normal! v{:call AnotherFunction()\<cr>" endfunction Now I want to be able to pass a range to that ...
4
votes
3answers
456 views

Run a function once on every paragraph

I'm trying to write a plugin that highlights overused words. After much effort I managed to write a function (and a command, :Ditto) that will highlight the most used word in the file or in a ...
4
votes
1answer
505 views

How to write each line into separate file?

I'd like to write content of the file into separate files each for each line. For example: Lorem ipsum dolor sit amet consectetur adipiscing elit I'd like to write 1st line into line1.txt, 2nd ...
7
votes
4answers
248 views

Use pattern of global ex command found on a line to substitute in another line

In my fortran code I have a lot of blocks like the following subroutine name(arg1,arg2,arg3,...) : end subroutine and I want them to become subroutine name(arg1,arg2,arg3,...) : end subroutine name ...
3
votes
3answers
1k views

How to delete searched line and next

I have a file containing special comments in the following forms: // SPECIAL // Some stuff // Some other stuff // Last line of other stuff So I need to be able to delete the line containing SPECIAL ...
3
votes
3answers
182 views

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/...
5
votes
1answer
168 views

How to simulate Return and Escape for global command without using Control-V?

I've the following sample content which I want to edit: <table> <tr><td title="Title 1">col1</td><td>col2</td><td>col3</td></tr> <tr><td ...
9
votes
5answers
2k views

How to replace content between two patterns from the file?

I've the following format of the file: <common> fitnes=0 genetic=1 method=0 </common> <inputs> foo=bar bar=foo </inputs> <limits> balance=200.00 </limits> and I ...
1
vote
0answers
63 views

Substitute at begining and end of a pattern (not line by line)

Is there a way to make a substitution only at the beginning and end of a pattern and NOT line by line? E.g., searching for the pattern /^\*\{2,}.* in * Micro topic 1 ** Microbes are small ** You can'...
4
votes
1answer
375 views

Substitute all except the last asterisk in groups of two or more asterisks

How can I substitute all except the last asterisk in groups of two or more asterisks with two spaces? E.g. * Micro topic 1 ** Microbes are small ** You can't see them! *** Isn't that something? ...
2
votes
3answers
79 views

Why aren't :g/X/d changes saved in Ex?

I've the following file (which is a simplification of the hosts file): 126.0.0.1 127.0.0.1 128.0.0.1 and I'm trying to invoke the following command: ex +g/127/d -cwq file which is supposed to ...
10
votes
2answers
1k views

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 ...
3
votes
1answer
79 views

How to surround all headlines with empty new lines in a markdown file?

I have the markdown file that uses # for headlines, like this one. #H1 Text one one ##h2 more Text #more H1s Even more text No I have the problem that some interpreters of markdown don't accept a ...
4
votes
1answer
146 views

Indenting lines in each paragraph except the first one using global command

I want to indent lines in each paragraph except the first one. By paragraph, I mean a group of lines that are separated from other groups of lines by at least one blank line. For example, here we ...
1
vote
3answers
337 views

Reducing multiple blank lines into single blank line. Exceptional case

I want to join multiple consecutive blank lines into single blank line. To do this, I use the following global command: g/^\s*$/,/./-j But this doesn't work in some exceptional cases. For example ...
6
votes
2answers
1k views

Append lines of text after pattern using global command

I'm reworking a section of logic in some C code to avoid a memory leak that looks like this: switch (result) { case CASE_1: return report(...); case CASE_2: return report(...); /* * More ...
13
votes
6answers
2k views

Delete all consecutive duplicates

I have a file that looks like this. Move to 230.00 Hold Hold Hold Hold Hold Hold Move to 00.00 Hold Hold Hold Hold Hold FooBar Hold Spam Hold I would like it to look like this: Move to 230.00 ...
8
votes
1answer
103 views

How can I use consecutive numbers in an Ex-style substitute command?

For example, let's say I want to put a number before every line that starts with the word "Do". The command would look something like :%s/^(Do )/1. \1/, but what can I do so it will apply numbers ...