Questions tagged [substitute]

Any operation involving regular expression based substitution of text, such as the ex-mode `:s` command. Not to be confused with the [replace] tag.

Filter by
Sorted by
Tagged with
0 votes
1 answer
46 views

How to test in which mode of magicness Vim is?

Vim can be in four or more magic states. In vim help documentation there is plenty to read about, all presuming that the user knows the current state of magicness. When a complicated many lines long :...
0 votes
1 answer
33 views

exim incorrect substitution count?

I'm having strange bevahiour with Vim 9.0 $ ex --version | head -3 VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Sep 30 2022 03:09:54) macOS version - x86_64 Included patches: 1-270 $ VIM_POSIX='...
  • 222
0 votes
2 answers
47 views

Search and replace uppercase letters with lowercase letters

I have bunch of strings that starts with uppercase letters. I want to change uppercase letters with lowercase letters. Strings are not aligned so visual block mode is not an option. Is it possible to ...
  • 375
1 vote
2 answers
28 views

Vim fill command line with currently highlighted word in substitution

I have incsearch on, so when I type :s/thingS it will highlight the thingS part of thingSoFar in the below text see, this thing is the thingSoFar thing When I am typing that command line with my ...
  • 1,857
0 votes
1 answer
34 views

Replace inside matching parentheses

I want to replace all Box::new(x) with x, e.g. Box::new(A(B()) + C()) becomes A(B()) + C(). Does neovim (0.8.1) support that? I expect that there must be some feature for such a likely developer-...
  • 1,857
1 vote
2 answers
66 views

How to create a range command that substitutes characters with grouping and back references?

The use case is to copy citations from pdf files to markdown documents in vim. I'd create a dotted list of important copied text and then I'd like to replace the end of line hyphenation that appears ...
1 vote
1 answer
40 views

Replace all the backticks with tildas by using GUI

To replace ``` code blocks fenced with backticks ``` with ~~~ code blocks fenced with tildas ~~~ I can use :%s/```/\~\~\~/g and this works. However, when I try to replace them with GUI (Edit > ...
2 votes
1 answer
130 views

Break line after the n-th comma

Suppose I have the next line 1,0,10,10,9,7,7,5,41,3,57,7,1,9 I want to split it into several lines, each containing n commands for example, for n = 4 1,0,10,10, 9,7,7,5, 41,3,57,7, 1,9
  • 143
0 votes
3 answers
89 views

How can I select all non-lowercase multiline blocks which follow a keyword?

I'd like to copy these to a file, but in a speculative future case, I might want to delete them. I might need to find a regex to substitute intervening data with a single blank line. The highlighting ...
  • 1
2 votes
1 answer
298 views

How to disable the interactive preview for substitute in Neovim?

In Neovim, when I type :%s/foo then the current buffer changes to have all instances of foo removed, basically giving a preview of the change. Coming from standard Vim, I find this confusing and it's ...
  • 381
3 votes
1 answer
135 views

How to match the content in a pair of brackets, no matter how many pairs of brackets are contained in the content?

I want to replace GD[many[[brackets]]], GD[evenMore[[[[brackets]]]]] with GD[someStuff], GD[someOtherStuff] How can I match the correct pair of brackets? These patterns are scattered in a large ...
  • 31
1 vote
0 answers
31 views

After `:%s` substitution the syntax highlighting of my R code has changed

Preface: VIM beginner, and I suspect this question will evolve significantly as I understand more about it (and VIM). Documenting the issue here as I was unable to find any other references to "...
  • 111
1 vote
2 answers
37 views

Replace characters in relative linespan

I know that I can replace characters in a linespan via something to the effect of :300,305s/foo/bar/g But when I'm deep in the code, and I know that I just want to replace in the next five lines, I'm ...
  • 245
0 votes
2 answers
41 views

How do I copy and paste the content inside quotes from one place to another?

Lets say I have a buffer with the following content: <property name="sfgiusfgsnffgdsb" value="arbitraryvalue1"/> <property name="sfsergggsnfgdfgs" value="...
1 vote
1 answer
58 views

Vim: reformat CSS from one-line to multi-line rules

I wanted to reformat the W3.CSS file to make it more readable and study it. I looked for an easy way to do this but couldn't find it (in vim-only way, obviously, the web are plenty of solutions and ...
  • 187
2 votes
1 answer
217 views

Keep specific parts of a :s command in nvim

What I'm trying to do is convert between Textile and Markdown links, so it should be able to do something like :s/".*"(.*)/[.*](.*)/, which is to say, keep the stuff that is matched by the .*...
1 vote
2 answers
63 views

Vim find and replace with filename as variable

Is it possible to use vim find and replace to replace a variable with the current filename? for example, fancyFileName.txt contains this is the VARIABLE to replace in this fancyFileName.txt and I am ...
  • 147
0 votes
2 answers
56 views

How to avoid empty line and comment line when substitute string?

Want to convert //comment abc_def ... thank_you123 ... to //comment .abc_def ( abc_def ), .thank_you123 ( thank_you123 ), with single substitute command. Need to avoid both ...
  • 1,002
3 votes
0 answers
61 views

How do I capitalize first character after a special character?

I want to convert "abc_def_ghi ( abc_def_ghi )" to "abc_def_ghi ( abcDefGhi )". Note: If I visually select the string within (), the below command changes whole line instead of the ...
  • 1,002
0 votes
0 answers
13 views

autocommand variable is not substituded [duplicate]

let Dropbox = "/usr/Dropbox" ... autocmd BufNewFile *.php 0r Dropbox/vim/templates/php_start.txt The variable 'Dropbox' is not substituded by its value
0 votes
1 answer
89 views

Use tilde ~ in substitution inside neovim?

In vim 8.1 (tiny), I can use ~ in a substitution, but it won't work when using neovim 0.6.0. For example, if I have a file with the text: one two three And I enter vim with "vim -u NONE -N",...
0 votes
0 answers
37 views

combining multiple lines [duplicate]

I have a text file of X ips and X files that looks like: ip1 ip2 ip3 some_dir/file1 some_dir/file2 some_dir/file3 I'd like to combine the lines into a csv format, like: ip1,some_dir/file1 ip2,...
2 votes
1 answer
86 views

Add incrementing numbers to lines

How do I numbers starting from 1 to a range of lines (thereby creating an ordered list)? For example, the following foo bar baz would turn into 1 foo 2 bar 3 baz I know to use the visual block to ...
  • 179
1 vote
1 answer
211 views

Mapping repeat last command

Apologies I am probably expressing myself very badly, that's also why 1 hour searching hasn't yielded the result I wanted yet... Assume I want to replace true with false in a buffer. :s/true/false Now,...
2 votes
2 answers
820 views

Why does vim replace with confirmation only replace the first occurrence in the each line?

For example, for a line X X X with the command :s/X/Y/c vim will only replace the first occurrence, namely the first 'X' to 'Y', is there a way to let vim go through all the 'X' and replace it ...
2 votes
1 answer
98 views

Adding a new file:line format for gF for a given file type

I use gF a good amount when getting to the bottom of compilation errors in different tech stacks. With Dart/Flutter, I'm getting the error messages in the following format: lib/main.dart:58:34: Error: ...
  • 121
2 votes
1 answer
31 views

Stop replacing occurrence in subsequent lines when occurrence is found in one line

I understood that :%s/foo/bar replaces the first occurrence of 'foo' with 'bar' in each line and repeats the process for all lines. My requirement is to stop skimming through lines after the replace ...
1 vote
1 answer
51 views

Show more lines on substitute confirmation

Sometimes I use substitute with confirmation because I only want to replace some instances of a search, depending on the context. However, it's difficult to tell sometimes because as Vim is cycling ...
  • 123
3 votes
2 answers
450 views

How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?

I have textfiles with paths that have lost underscores between words inline in an OCR process. I want to keep the indentation and put back the underscores in the paths. I have a path like this: /...
5 votes
1 answer
142 views

Can I replace two distinct old strings with two distinct new strings using a single :s command?

Suppose I have the following line of text in vim: hello, it will be sunny today and I want to change it to: goodbye, it will be sunny tomorrow I could put my cursor on the line and type :s/hello/...
1 vote
3 answers
512 views

Netrw-R Renaming multiple Files with vim

After going through the renaming guides from the doc folder in vim I found a way to replace text with the input :%s/frompattern/topattern/g. Reaching the renaming files with the file browser tutorial/...
0 votes
1 answer
43 views

How can I connect separated lines without highlighting in visual mode?

I would like to connect broken lines, like: This is a line that continues. but instead of highlighting them with visual mode and connecting with Shift-J, I would like to run some kind of global ...
1 vote
1 answer
129 views

How do I enter N carriage returns during a substitution?

When using :s, how do I multiply the string being written by an integer, to write N copies of it? For example, :s/pattern/10*\r/ would write 10 carriage returns in each match.
1 vote
1 answer
220 views

argdo vs bufdo for project-wide search and replace in specific folders

I have the following structure: myproject ----src file1.cpp, ..., file10.cpp ----include incl1.h, ..., incl10.h ----data data1.txt, ..., data10.txt I would like to ...
  • 207
0 votes
1 answer
146 views

substitute() with ampersand in replacement

I’m doing something like this: let l:new = substitute(l:old, l:word, l:repl, ‘’) l:repl comes from an external source and often has an ampersand in it. Is there a way to prevent vim from interpreting ...
  • 103
-2 votes
1 answer
64 views

How to replace *_Apple to Apple using VI?

I need to replace multiple lines using vi. For example, any numbers_Apple to Apple 0001_Apple 0010_Apple 1021_Apple … 8383_Apple to Apple
1 vote
1 answer
343 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/ | ...
  • 209
0 votes
1 answer
59 views

Replacement of characters gives ^@ when replaced with newline but not newline [duplicate]

Why does replacement of something by using :%s/,/\n/gc in vim gives me ^@ instead of giving me newline. Sed replacement works perfectly sed s/,/'\n'/g. Any idea how to do sed thingy in Vim and save ...
  • 103
0 votes
0 answers
30 views

Custom indent minified file

At work I'm forced to work on custom mapping-files which map relations between objects of different systems. These mappings, which are basically Jinja templates, are unfortunately stored in single ...
0 votes
1 answer
57 views

Substitution alternating a pattern

I have a transcription file that looks like this: [09:46] question [10:22] answer [10:25] question [10:26] answer [10:27] question and so on... Now I would like to add an identifier right after ...
  • 3
2 votes
4 answers
885 views

How do I change the word under the cursor?

I've written a little function that changes a 'CamelCaseWord' to a space separated 'camel case word': " Convert a CamelCase word to space separated words. function! s:converFromCamelCase(arg1) ...
  • 31
1 vote
1 answer
96 views

Insert and remove a marker at the start of the current line

So, I'm trying to set up a sort of bookmark/marker kind of workflow, and I came to notice I'm used to put characters such as # and such for different meaning inside my own notes, etc. Now, I already ...
0 votes
3 answers
213 views

How can I perform a replacement a specified number of times per line?

Using vim, I know that I can replace all occurrences of a string across multiple lines by specifying a count or a range, as documented in :h subst. I have a markdown table which contains C function ...
  • 207
0 votes
1 answer
69 views

Can't replace div using regex

I have the following code from lines 6-20 <div> 1 </div> <div> 2 </div> <div> ...
2 votes
1 answer
145 views

Caret `^` and `\%V` compatibility in Vim regex

Can the caret ^ and the sequence \%V be used to match the beginning of a line in a visual selection? And if not, what is the rationale? The following example seems to indicate that ^ and \%V are not ...
  • 275
1 vote
1 answer
158 views

Replace two space indentation with tabs

I'd like to replace all two-space indents with tabs I can't do :%s/ /^I/g bc this replaces within lines too. I can't do :%s/^ /^I/g bc this replaces only the first two spaces of each line, not each ...
  • 1,857
1 vote
3 answers
229 views

Function That Substitutes Across Multiple Files

I'm trying to create a function that implements the functionality outlined here: Vim search replace all files in current (project) folder My current attempt: nnoremap <C-F> :call ...
1 vote
2 answers
184 views

Copy to clipboard or save to file substitution command

I'm trying to answer a vim question in U&L, which involves a very complex and long regex substitution. I would like to copy to clipboard the command, or save it in a file, so I don't have to make ...
1 vote
1 answer
65 views

Configure Vim 8 behavior regarding substitution in visual selection

When I select a section of text within a line, then indicate I want to substitute on that line with :s, the result is that the entire line is affected. In order to actually do what I intend, I have to ...
  • 134
3 votes
2 answers
77 views

Why \%V is not added in visual mode during substitution by default?

\%V can be used to restrict match within the visual selection. However, I can't think of a situation when one would not want to do just that in visual mode.
  • 391

1
2 3 4 5
7