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
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 ...
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 ...
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'...
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 ...
4
votes
Accepted
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 ...
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
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 ...
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:
:%...
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.
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 ...
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 ...
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:
$ #...
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 ...
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 ...
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 ...
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 ...
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(...
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(...
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 ...
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__.
...
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 => &...
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 /":&...
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 ...
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> ...
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 ...
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/".*"/(&)/
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 ...
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 :...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
search-replace × 26regular-expression × 10
replace × 9
substitute × 5
search × 3
find × 3
neovim × 2
vimscript × 1
normal-mode × 1
visual-mode × 1
linux × 1
visual-block × 1
filetype-python × 1
whitespace × 1
unicode × 1
command × 1
count × 1
delimiter-matching × 1
original-vi × 1
filetype-csv × 1