Skip to main content

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
2 votes
2 answers
268 views

How can I insert new arguments to this block of function calls that follow a pattern?

I have a number of lines as seen below: arrowBuilder("right_front_wheel", inputMessage.front_right_lateral, Colour.Green, 4, "y"), arrowBuilder("right_front_wheel", ...
Khalid Owl Walid's user avatar
1 vote
3 answers
193 views

Search and replace multiple characters simultaneously

Suppose I have a buffer containing a b c How can I use a single search and replace command to obtain the following (cyclic) permutation? b c a In this question, I found one can use the "bar&...
907456's user avatar
  • 13
1 vote
2 answers
54 views

Substitute a word in which cursor in the current line

How can I :substitute, in the current line, a word on which cursor is, but only after the cursor position. e.g.: when doing (illustratively, may not exact correct command): :s/<C-r><C-w> ...
user17227456's user avatar
1 vote
1 answer
50 views

Have the cursor in the beginning of the replacement string post find-substitute

The commnad: :s/foo/&bar when applied to a line containing foo, catches it and append bar on it, then user hits Enter, the cursor immediately gets into very beginning of the line. How to let it ...
user17227456's user avatar
0 votes
2 answers
40 views

How to convert octal in ascii characters by using substitute() in Vim9?

My attempt: vim9script var text = 'Hello \\123world\\045!' echom text->substitute('\\\o\o\o', => printf("%c", str2nr(submatch(0), 8)), 'g') result in E15 and E116.
Barzi2001's user avatar
  • 650
0 votes
2 answers
77 views

Delete the last space in multiple lines

I need to remove the last 1 space for multiple lines if there is a 1 space. I found the reference but it does not work for last. I did :%s/ ^ / https://stackoverflow.com/a/6962474/1917217 for ...
Carter's user avatar
  • 121
0 votes
3 answers
48 views

Swap a 0 under the cursor with a 1 and vice versa

Assume that, in normal mode, the character under the cursor is either 0 or 1. I want that, upon pressing a key (say F6), it is swapped to the other one.
brad's user avatar
  • 131
0 votes
1 answer
51 views

Generate tag links of search pattern in Vimwiki

Question How to use :VimwikiGenerateTagLink with a search pattern? Explanation The Vimwiki command :VimwikiGenerateTagLinks produces the list of tags and links of your wiki. Without arguments, it ...
Ludenticus's user avatar
2 votes
3 answers
161 views

Best way to keybind subsequent substitutions in visual mode

I need a keybinding that, given some text selected in visual mode (line visual mode, block visual mode, or whatever): Add > _ before the text Add _ after the text In case of empty lines, > ...
robertspierre's user avatar
2 votes
2 answers
122 views

Can't stop the global command using subcommand :substitute (:s) with the check flag (c)

I'm trying to use the global command :g with :substitute to find and replace some string and also with check. So the command like: g/\d/s/.*//gc but the check behavior is not same as in :s I can't ...
M_Sea's user avatar
  • 157
0 votes
1 answer
56 views

Add newline after every paragraph in log file in vi editor

I want to add new line after every paragraph in vi editor. How do I do it? The new line is located by the date and time in this format 2023-11-26 14:14:14
achhainsan's user avatar
1 vote
1 answer
56 views

Search and replace text in visual range but not entire line [duplicate]

I want to search and replace within a visual range just as was answered in this SO post, which uses :'<,'>. But I don't want this to take place on an entire line, only within the selected subset ...
N. Syiemlieh's user avatar
3 votes
2 answers
146 views

How to remove trailing whitespace and carriage return characters?

My goal is to get rid of "bad" CRs and trailing spaces at once, so I am executing the following command: :%s/^M\|\s\+$//g The problem is that if at the end of the line there is a ^M and ...
John's user avatar
  • 33
1 vote
1 answer
46 views

How do I use parameters within a custom command?

I want to define a global search and replace with confirmation command, something like: command Gfr :s/<parameter1>/<parameter2>/gc So I can type: :Gfr foo bar and Vim will find all ...
David Shaw's user avatar
1 vote
4 answers
85 views

How to encapsulate a visual selection with preprocessor logic?

I have to preprocessor-comment out a lot of C-code. E.g. I want this: if (abcd == true) do_smt(); To become this: #if 0 if (abcd == true) do_smt(); #endif This is tedious work, so I was hoping ...
Opifex's user avatar
  • 227
1 vote
1 answer
53 views

Is there a way to substitute a text that has regex formatted number ranges and replace with all combinations?

Let's say I have a file that looks like: host[1-2]lx host[1-4]win what I want is an easy way to get host1lx host2lx host1win host2win host3win host4win Is there a neat way to automate that process? ...
deedar huq's user avatar
0 votes
1 answer
184 views

Command to separate nested brackets over multiple lines

I am looking for a language-agnostic way to convert something like this: this(is, a, line(of, things)) to this: this( is, a, line( of, things ) ) I can handle the non-...
pvelayudhan's user avatar
1 vote
1 answer
71 views

How can I replace duplicate line prefixes with blanks?

Is it possible to transform: John Apple Red 3 John Apple Green 5 John Radish White 2 John Radish Red 7 Tom Apple Red 3 Tom Apple Green 5 Tom Radish White 2 Tom Radish Red 7 to: John Apple Red 3 ...
athos's user avatar
  • 207
0 votes
1 answer
38 views

What is the syntax to use character classes inside Vim regex bracket expression?

I did the following tests: code output notes :echo substitute("s s", '\v[^s]', "<&>", "g") s< >s space replaced. All fine :echo substitute("s s&...
KamilCuk's user avatar
  • 265
1 vote
2 answers
468 views

Substitution command in neovim remaps not working

I simply want to map < leader> pu to erase all occurrences of the carriage return character (represented as ^M) in a file. I can execute the command: :%s/\r//g and it works. Any attempts to use ...
fireq1's user avatar
  • 13
1 vote
1 answer
145 views

How to evaluate Lua in substitution field instead of vim script?

As simple example, take this: :'<,'>s/\vs([A|B])\.size\(\)/\={"A":"a","B":"b"}[submatch(1)]/g This substitution will replace sA.size() with a and sB.size(...
Vladyslav Rehan's user avatar
1 vote
1 answer
91 views

How to evaluate vim script in the substitution field and put text with it?

I have to replace a lot of variables s1.size() with wa, and replace s2.size() with wb accordingly. I have made next substitution request: :'<,'>s/\vs([1|2])\.size\(\)/w\={"1":"a&...
Vladyslav Rehan's user avatar
0 votes
4 answers
55 views

How to repeat substitution on the consequent lines?

Say I want to fix a mistake in the following php code: $line1 = intval($line[$1]); $line2 = intval($line[$2]); $line3 = intval($line[$3]); and do substitution on the first line with: :s/\$\(\d\)/\1/ #...
l00p's user avatar
  • 263
2 votes
1 answer
45 views

:.w !bash a temporarily modified line and executing it in the shell

#!/usr/bin/env bash # scp /tmp/abc.sh SERVER1:~/bin && ssh SERVER1 "~/bin/abc.sh" echo "Hello Stack Exchange" echo "thanks for the help I would like to be able to use ...
Aaron Burda's user avatar
0 votes
2 answers
56 views

Trying to replace pattern _1_ with _01_ but its not working

I am using Vim version 9.0.1280. The search and replace command I am using is - :%s/\(_\)\([1-9]\{1\}\)\(_\)/_0\1_/gc When I run this vim offers to replace _1_ with _0\1_. What am I doing wrong?
gautam's user avatar
  • 3
0 votes
2 answers
231 views

Vim substitution and keep cursor in place

I have a mapping in my .vimrc. It is the following: nnoremap q; :call Add_comment_css()<CR> And then I can run, say 10q; And then it will run 10 times. But my problem is that the cursor always ...
john-jones's user avatar
0 votes
1 answer
135 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 :...
Anton Wessel's user avatar
0 votes
1 answer
41 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='...
gildux's user avatar
  • 274
0 votes
2 answers
1k 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 ...
s1n7ax's user avatar
  • 605
1 vote
2 answers
291 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 ...
minseong's user avatar
  • 2,383
1 vote
1 answer
117 views

Neovim E276 - Command works in Vim but not in Neovim

Thanks to the comment from @martin-tournoij on one of my other questions. He gave me this piece of code: :%s/0x[a-fA-F0-9]\+/\=submatch(0)->str2nr(16)/ which looks for every hex-number like 0x10 ...
Bog's user avatar
  • 509
0 votes
1 answer
286 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-...
minseong's user avatar
  • 2,383
1 vote
2 answers
70 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 ...
Paul Rougieux's user avatar
1 vote
1 answer
61 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 > ...
user90726's user avatar
  • 123
2 votes
1 answer
534 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
juaninf's user avatar
  • 153
0 votes
3 answers
112 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 ...
Greek2me's user avatar
2 votes
1 answer
1k 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 ...
danijar's user avatar
  • 391
4 votes
1 answer
941 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 ...
Yufei's user avatar
  • 41
1 vote
0 answers
32 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 "...
Owen's user avatar
  • 111
2 votes
2 answers
470 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 ...
Yehuda's user avatar
  • 275
0 votes
2 answers
246 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="...
andrescmasmas's user avatar
1 vote
1 answer
151 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 ...
Antonio's user avatar
  • 289
1 vote
1 answer
145 views

Automate find and replace

When having selected something in visual mode where I want to perform a find-and-replace I always have to run :'<,'>s/foo/bar/g manually. Is there some way so that I can bind it to some key ...
Richard's user avatar
  • 155
2 votes
1 answer
326 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 .*...
Katie And's user avatar
1 vote
2 answers
208 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 ...
manolius's user avatar
  • 147
0 votes
2 answers
62 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 ...
Fisher's user avatar
  • 1,086
3 votes
0 answers
64 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 ...
Fisher's user avatar
  • 1,086
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
lima2018's user avatar
0 votes
1 answer
269 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",...
Saul Axel Martinez Ortiz's user avatar
0 votes
0 answers
39 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,...
wiggitywacker's user avatar

1
2 3 4 5
8