9 votes

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

The substitution is easier to read with word-boundaries in my opinion, and very magic mode makes typing easier: :%substitute/\v>\s+</_/g
D. Ben Knoble's user avatar
  • 24.7k
7 votes
Accepted

Keep specific parts of a :s command in nvim

Yes and no. Depends a lot on the complexity of the documents. Textile has a lot of various formats for links and Markdown has it's share. Keeping pattern When you do a substitute you can capture parts ...
Moba's user avatar
  • 390
6 votes
Accepted

Search and Replace on odd/even numbered lines using g

The global command marks every line matching a pattern and executes a list of actions on them. Each action is separated by the pipe symbol, |. In :g/^/s/foo/bar/|+t+|-d the pattern is ^, so all the ...
Quasímodo's user avatar
  • 2,406
4 votes
Accepted

Match one regex pattern if not preceded by another one

It's very nice that you gave this fairly complex regex a good try! There are some issues with it which are fairly hard to spot, so learning how to debug a regex would come handy. To debug a regex, it'...
filbranden's user avatar
  • 28.1k
4 votes
Accepted

Use *-like command in normal mode to execute a find/replace

You could do the replace in two steps: You prepare a search command using * (/\<the\>) You do your replace with an empty search (:%s//the only/g) The replace will default the search to the ...
Vivian De Smedt's user avatar
4 votes
Accepted

Upcase only the 3 and 4 letter words

I would propose: :%s/\<\w\{3,4}\>/\U\0/g
Vivian De Smedt's user avatar
3 votes
Accepted

Use regex to substitute a match except the first and last characters

I would propose you: :s/\s\@<=\s\s\@=/./g Where: \s\@<= match only if the previous character is a space \s\@= match only if the next character is a space Since both match with zero width you ...
Vivian De Smedt's user avatar
3 votes

Vimscript to replace persian/arabic digits with english ones

Use the following :s command for Persian and Urdu digits: :%s/\([۰-۹]\)/\=char2nr(submatch(1))-1776/g And the following for Arabic digits: :%s/\([۰-۹]\)/\=char2nr(submatch(1))-1536/g
yukashima huksay's user avatar
3 votes

Use *-like command in normal mode to execute a find/replace

One nice version of this is *cgn replacementEsc. This is repeatable with ., or n. to see what will change before changing it (kind of like /gc flags for :substitute). If you wanted to do a global ...
D. Ben Knoble's user avatar
  • 24.7k
3 votes
Accepted

Vim find and replace with filename as variable

As pointed out in other answers, :help sub-replace-special, and :help \=, you can use any expression as the replacement text like so: :%substitute/\<VARIABLE\>/\=expression/g In your case: :%...
D. Ben Knoble's user avatar
  • 24.7k
3 votes
Accepted

Transform text that matches a pattern to uppercase

check :h :s% for substitute options, including: \U following characters made uppercase, until \E *s/\U* So :%s/word/\U&/g will replace all word to WORD.
Maxim Kim's user avatar
  • 12.3k
2 votes

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

I found this answer posted by u/rewardit on Reddit which does the trick, but I didn't find anything on SO or networked sites so thought I would post it here as I prefer this format to Reddit This will ...
Rasmus Groth's user avatar
2 votes

add quotes to each string and change the delimiter for a comma

You can break this up into 3 substitutions and apply them in sequence: :s/ | /", "/g | s/^/"/ | s/$/"/ s/ | /", "/g replaces all the | delimiters with the quote and ...
mattb's user avatar
  • 1,081
2 votes

Capitalize a match in search and replace

For this specific task, an alternative approach is not to use a :substitute command at all! :%norm!$Bgul % # On every line, norm! # Run the normal mode commands: $ #...
Rich's user avatar
  • 30.9k
2 votes

Capitalize a match in search and replace

This worked for me %s/.*\zs \([A-Z]\)/\L&/ If you want to consider lines like Insomnia (Cures) you can use %s/.*\zs [^A-Z]\?\([A-Z]\)/\L&/ To manipulate strings at the beginning of the line ...
user81371's user avatar
2 votes

Capitalize a match in search and replace

You could do the following: :%s/\S\+\s*$/\l&/ The \S\+ match any non space characters (a word) Since its is followed by \s*$, that only match trailing spaces at the end of the line, this word ...
Vivian De Smedt's user avatar
2 votes

Illegal back reference when trying to delete \1

You can't use \1 as a back reference to what matched in a separate pattern match. I see you're using the pattern in the :g capture group to match the location and use a capture group to pick the part ...
filbranden's user avatar
  • 28.1k
2 votes

Use regex to substitute a match except the first and last characters

There may be a better way, but this is what I came up with: :s/\S\s\zs\s*\ze\s\S/\=substitute(submatch(0), '\s', '.', 'g')/ \A matches anything that isn't an alphabetic character, so it makes sense ...
Pak's user avatar
  • 1,092
2 votes

Use regex to substitute a match except the first and last characters

Try this: :%s/ \+/\=' '.repeat('.',len(submatch(0))-2).' '/g %s/PATTERN/\=EXPR/g substitutes all occurrences of PATTERN with the result from evaluating EXPR \+ matches at least 2 spaces len(...
ngn's user avatar
  • 163
2 votes

How to grab visual selection for search, but apply the appropriate escapes?

I have had this function for years in my config: " return a representation of the selected text " suitable for use as a search pattern function! GetSelection(escape) let old_reg = getreg(...
romainl's user avatar
  • 38.3k
1 vote
Accepted

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

I doubt a simple solution exist for any regular expression but for you case you could: Define the following function: function! GenerateLineRange(start, end, i1, i2) let ret = '' for i in ...
Vivian De Smedt's user avatar
1 vote

Trying to replace pattern _1_ with _01_ but its not working

When Vim says, replace with _0\1_ ..., it hasn't replaced the \1 you typed with the first group yet. The first group in your expression is \(_\), so the resulting text after the substitution is _0__. ...
Rich's user avatar
  • 30.9k
1 vote
Accepted

How do I include the match number when searching and replacing in vim using regex?

Generally, there is no such thing as a "match number." But, you can create a counter and refer to it: :let count = 0 :'<,'>global/^/substitute/pub \(.*\):.*/\=printf('%d => &...
D. Ben Knoble's user avatar
  • 24.7k
1 vote
Accepted

Removing a line that ends with a " : " with vi (on AIX)

Unless the character is special, the command :g/:$/d (abbreviated from :global/:$/delete) should do it. Perhaps you meant the line ends with ":", in which case simply change /:$/ to /":&...
D. Ben Knoble's user avatar
  • 24.7k
1 vote
Accepted

Is it possible to use language mappings in search-and-replace?

When switching to the command line mode (i.e. colon but not slash) the &imsearch option is forcefully reset "as you're expected to type command" as the help page says. So you have to ...
Matt's user avatar
  • 20.1k
1 vote
Accepted

Replace arithmetic expression by their result

Here's one solution: vi[s<C-r>=eval(@")<cr><esc>. You can probably xnoremap the bit after s. How? vi[ select expression s delete it, go to Insert mode <C-r>=…<CR> ...
D. Ben Knoble's user avatar
  • 24.7k
1 vote

Vim find and replace with filename as variable

Assuming you have defined VARIABLE as a global Vim Script variable like this: :let g:VARIABLE='fanceFileName.txt' you can then use: %s/\<VARIABLE\>/\=get(g:, submatch(0), '')/g This uses the ...
Christian Brabandt's user avatar
1 vote
Accepted

VIM search and replace regex - how to keep the search string intact and adding to it?

You can use & or \0: :%s/".*"/(&)/
Vivian De Smedt's user avatar
1 vote
Accepted

Interrupt a global search and replace with confirmation

Run your command as :%substitute/{pat}/{repl}/gc which is equivalent (in this particular case), and use <esc> or q to stop substituting. The difference is :global/{pat}/substitute//{repl}/gc ...
D. Ben Knoble's user avatar
  • 24.7k
1 vote

Illegal back reference when trying to delete \1

Here’s two ways to accomplish the whole thing: A macro qq0fTabdf@%lDq A command :normal! 0fCtrl-VTabdf@%lDq You can use a range with:normal to apply to multiple lines. In fact you can even do :...
D. Ben Knoble's user avatar
  • 24.7k

Only top scored, non community-wiki answers of a minimum length are eligible