Questions tagged [replace]

Command or mode to overwrite text. For search-and-replace operations, use the [substitute] tag.

Filter by
Sorted by
Tagged with
2
votes
1answer
87 views

How to handle intersections in an algorithm trying to draw ascii diagrams?

For learning purpose, I'm trying to reimplement a small subset of the features provided by the DrawIt plugin. So far, here's what I have: " data "{{{ let s:draw_state = 'disabled' let s:draw_chars ...
5
votes
2answers
154 views

How to replace buffer with a search range selection?

I'd like to replace the entire buffer (file) with the current search range selection: :/pattern1/;,/pattern2/p How this can be achieved?
3
votes
1answer
277 views

Evaluating and rounding a file of numbers in scientific form

I have a file full of expressions like this: 0.100000000000000000000*10^(1) 0.000000000000000000000*10^(0) 0.911403977421716024142*10^(-15) 0.983917614423065751378*10^(-15) and I would like ...
9
votes
1answer
1k views

How to get a preview for the substitution command?

Quite often I find myself in need of performing a substitution that will have matches sparsed all over a large file and with non-trivial search and replacement expressions. For example, say I have ...
1
vote
1answer
145 views

Unfamiliar character when copy pasting from PDF file

I am copy pasting a big chunk of code from an article in PDF format. The issue is that the operation is replacing every * character (the multiply operator in Fortran) by some sort of slash character, ...
2
votes
2answers
1k views

How to customize % symbol in makeprg command

When I set makeprg using: set makeprg=gcc\ -Wall\ -g\ %\ -o\ % and then invoke :make Vim does not pass proper filenames, because this command is translated as: gcc -Wall -g file.c -o file.c and ...
0
votes
4answers
756 views

How to quickly replace a single character in a word search, but only for a block of code in Fakevim?

3rd block of code needs to go from ipt1 to ipt4 4th block of code needs to go from ipt1 to ipt5 Because it is only 3 selections, creating a macro is probably slower than manually, but doing it ...
1
vote
0answers
830 views

Enter insert to insert mode from function

I want to create function that search for selection and then automatically replace selection with given text. Something like *Ncgn but with custom movement (not exactly word only). My current ...
0
votes
1answer
171 views

Globally replace a particular pattern with another string

In VI I need to replace a pattern that looks like this: seq- followed by exactly 24 chars (can be 0-9, a-z, A-Z only) followed by / followed by some number of (0-9, a-z, A-Z). So an example string ...
2
votes
2answers
148 views

Paste + Override Shortcut

I would like to have a Paste + Override shortcut, which should basically have the behaviour of going into Replace-mode, typing everything currently in the clipboard, exiting Replace-mode. How can I ...
1
vote
1answer
121 views

Text expansion for LaTeX editing

For writing a text book I would like text expansion; on typing this: |25,50,65| it should expand this to: $25\ux,50\uy,65\uz$ I tried to use simple text replacement with :s%/TEXT/NEWTEXT/gc, but ...
2
votes
1answer
108 views

Is there a way to make the `r` operator accept a motion?

What I would like to do is issue a raiw where the current word would be replaced by an aaaaa word. example: this is a w[o]rd <--- cursor on [] raiw <--- command this is a aaaa &...
3
votes
1answer
161 views

Search and replace a visual selection in the whole file

I want to search and replace the selection I made in Visual mode. For example I have the following text: Lorem ipsum dolor sit amet. Consectetur adipiscing elit. Sed quis gravida lectus amet. And ...
1
vote
4answers
135 views

Replace using info from the replaced string

What I want to do is replace this: <|Filename.ext|5|0|> with <|Filename.ext|5|0|> <|Filename.ext|5|1|> <|Filename.ext|5|2|> <|Filename.ext|5|3|> <|Filename.ext|5|4|&...
0
votes
1answer
45 views

How to replace all “printLog();” into “//printLog();” but not resulting “////printLog();”?

for example, I want to comment out all printLog() functions in a file: //printLog(); printLog(); //printLog(); but some printLog() are already comment out, and replacing printLog() with //printLog() ...
2
votes
2answers
397 views

Replace last instance of character (and there's more)

I am trying to format a list that uses tabs for indentation so it will render as a bulleted list on Confluence. That means that each instance of a tab needs to be replaced with an asterisk (*), plus ...
2
votes
1answer
94 views

How can I define an operator that takes a register as an argument?

I'm trying to write a "Paste-over" operator. It will work like a combination of p and R, so it will paste the contents of a register over the text currently in the buffer. I have this so far: ...
8
votes
4answers
269 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 ...
4
votes
2answers
105 views

How to change text in other line when the pattern is found?

I've got this 4-line content: foo baa baz quz and I'd like to replace baa to bar only when quz is found. I've tried these commands (which highlights baa correctly, but says it's not found): :/quz/...
8
votes
1answer
233 views

Perform previous substitution for different selection with single keystroke

I performed following command :'<,'>s/2/digit/g on the code below manually visually selected: return _.round(this.getTotalAbs(), 2).toFixed(2); And it worked as intended return _.round(...
4
votes
3answers
902 views

Concatenating continuation lines

I have a text file like this: Line text,line text next line 3rd line Line x where 2nd and 3rd, etc lines begin with a single space. I need to concatenate all lines prefixed with a space to the ...
5
votes
1answer
195 views

In Vim, how can you delete following identical rows in a visual selection, without sorting?

So Lets say I have a list \Summit \SummitSign \sun \ProgrammingOnABoat \SunCloud \SunshineOpenCircled \sup \supdsub \supdsub \supedot \suphsol \suphsub \Summit \SummitSign \sun \Summit \...
21
votes
2answers
11k views

Replace a pattern with current line number

I'd like to replace 'x' in this file with current line number. That is, change x x x to 1 2 3 For now, I use a Perl one-liner: perl -pi -e '$x=$.; s/x/$x/' myfile.txt I think UltraEdit can do it ...
1
vote
2answers
1k views

How to map key to a substitute command with dynamic text?

I've found this little trick in vim wiki. nnoremap <Leader>s :%s/\<<C-r><C-w>\>//g<Left><Left> Immediately I thought that in some cases it would be beneficial to ...
8
votes
2answers
929 views

How do I paste the same line of code X times but change one word in the line each time?

I'm writing HTML lists, and I want to be able to automagically paste 10 list items where the item text is the only thing that changes. I could accomplish this using PHP and then just copying the ...
2
votes
5answers
11k views

Replacing last comma on each line

The following will replace the first comma on each line with a pipe: :%s/,/|/ How can I do the same for the last comma?
4
votes
1answer
111 views

Search and replace depending on capitalization of letters [duplicate]

I would like to replace foo with bar and Foo with Bar. I can do :s/Foo/Bar/g :s/foo/bar/g Can I do this in one command?
4
votes
2answers
4k views

Searching/replacing exact word in vim by default

In vim when searching or replacing an exact word you need to type: \<word\> to match it exactly, this is cumbersome to type, and I find myself wanting to search/replace exact words a lot more ...
5
votes
3answers
668 views

Why does replace not accept a motion?

A lot of operator like delete, change, indent, switch-case accept a motion to apply on. For example, let say I want to replace the current word with all letter a, I do: viwra Compared to the ...
9
votes
2answers
6k views

Nested matching group in regex

I have a common usecase when I transform some python expression the following way: value 1 value 2 value 3 into ['value 1', 'value 2', 'value 3'] The easiest way may be to use a mapping, but I ...
1
vote
1answer
1k views

How to paste a text block in visual block mode?

I want to paste text which spans across multiple lines and is rectangular. For example, a rectangular box representing a device topology diagram. +-------+ | | | | ...
10
votes
6answers
6k views

How to copy first four characters of every line to the end of that same line?

Given a series of lines that look similar to this: 2001 "Some Kind of Title," Author's Name, Publication Name, 1 Mar. 2002 "Some Kind of Title," Author's Name, Publication Name, 12 Oct. 2003 "Some ...
10
votes
5answers
3k views

Replace selection with output of external command

Assuming I have a line in a file with XXXX being a placeholder I have selected via v as follows: XXXX Some other text How can I change only the XXXX with the output of an external command, such as ...
2
votes
1answer
487 views

How do I :substitute inside a selection? [duplicate]

The command :'<,'>s/search/replace/g will replace in the whole line of the current selection, but what if I want to substitute only inside the current selection, not the whole line? Update: ...
2
votes
1answer
9k views

Creating a regex to match html attributes

I know that there are a lot of answers to vim/regex, but I can't seem to find one that fits the bill for me. So, I'm trying to remove some attributes from HTML using the following: %s/( id="\| ...
4
votes
1answer
2k views

Change/delete matching brackets at the same time

Sometimes I write something in brackets and then realize I've typed the wrong kind. For instance, in LaTeX: \command(arguments) Which should be: \command{arguments} I can jump between the two ...
5
votes
3answers
544 views

How can I replace a word in a column without changing the width

I'm writing cucumber example tables, which are formatted like this: | title | title | | data | data | | data | data | How can I replace one of the data or title cells with a word of ...
7
votes
2answers
3k views

Replace current selection and all identical occurrences in a file

This is a sample sentence. This is a second sample sentence. Let's say I select 'is a' of the first sentence. Now I want to change all occurrences of what is selected (including the selection itself) ...
10
votes
2answers
7k views

Multiline Search and Replace

Would like to perform a search and replace in a file with 12000 lines. Specifically, if an occurrence of ^ SetFontSize 28 exists after a ^Hide block and before the next ^Hide or ^Show, change 28 ...
2
votes
1answer
77 views

How does this search-and-replace command work? `:g /start1/.,/start2/-1 move /end/-1`

I read this description of a different example, substitute command and it was very helpful, but I'm still not fully certain how to interpret the commands in :g /start1/.,/start2/-1 move /end/-1. It ...
5
votes
1answer
2k views

How can I replace a word with a yanked word in Vi?

Lately I've found myself working more and more with remote servers over SSH. Most of those severs don't have Vim, but just Vi. Using it hasn't been too rough with just a few problems. One is this: ...
10
votes
3answers
306 views

Alternative to escape slashes in search and replace for long strings

Is there any alternative in vim search and replace for strings, than having to escape every special character in string. For example, If I want to match following string and replace with something [...
7
votes
3answers
163 views

mapping to enclose symbol under cursor with an expression that contains the symbol

Have been reading vim scripting posts for an hour, but I'm afraid I don't have the skills to apply these posts to my problem. Editing math in latex documents, I'm frequently wanting to replace math ...
5
votes
1answer
496 views

Repeat replace one character (r) for next occurence

I'm using r a lot (e.g. for replacing " with ' when refactoring code). Is there an easy way to do something like this: For text: values = ["a", "b", "c"]. Place cursor on first ", press r' to change ...
12
votes
3answers
431 views

How to selectively change a hightlighted string?

By searching, I've highlighted the string that I want to change. Now I want to replace only the highlighted text. I'm using n to advance to the next instance of the search string. I was thinking I ...
8
votes
1answer
129 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 ...
2
votes
6answers
306 views

How to replace a list of numbers (or times/timestamps) with their sum?

For example, this text: 2:26 3:04 3:14 5:38 4:12 10:30 6:29 6:53 11:49 9:33 4:17 7:49 6:10 6:04 6:28 6:40 4:21 (These are actually minute:second format, not hour:minute, if it matters. So ideally ...
12
votes
3answers
24k views

How to add space on multiple lines when pressing spacebar (without extra configuration)?

I tried visual block, but visual block only allow to highlight or delete characters on multiple lines. Is there any function that allows to add space before the first character of multiple lines when ...
4
votes
1answer
128 views

Difference between two end of line signs

Both $ and \n represents the end of a line. To remove an empty line we can use either :g/^$/d or :%s/^\n//g My questions are: What's the difference between these two signs? It seems the first ...
10
votes
3answers
536 views

Is there a motion similar to a" that never includes leading whitespace?

I'm using the a" motion rather often in vim; e.g. using ca" while refactoring a piece of code to replace a hardcoded argument with a variable name. The problem with this approach is that a" includes ...