Skip to content

Commit

Permalink
chore(content/til): add 2024_01_17-find_and_replace_vim
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Jan 18, 2024
1 parent fa982ab commit 3cdfd4e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions content/til/2024_01_17-find_and_replace_vim.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: 'Find-And-Replace Vim'
description: 'How to fast replace all mathing strings on vim'
date: '2024-01-17'
tags: [vim,editor,ide,cli]
---

For base find-and-replace on vim, we can use the `:substituto` (`:s`) command.

The general form of the substitute command is as follow:
```
:[range]s/{pattern}/{string}/[flags] [count]
```

The command search each line in `[range]` for a `{pattern}`, and replaces it with a `{string}`.
`[count]` is a positive integer that multiplies the command.

If no `[range]` and `[count]` are given, only the pattern found in the current line is replaced.
The current line is the line where the cursor is placed.

For example, to search for the first occurrences of the string `"foo"` in the current line, and replace it with `"bar"`, you should use:
```
:s/foo/bar
```

To replace all occurrences of the search pattern in the current line, add the `g` flag:
```
:s/foo/bar/g
```

If you want to search and replace the pattern in the intire file, use the percentage character `%` as a range.
This caracter indicates a range from the first to the last line of the file:
```
:%s/foo/bar/g
```

We can also use `|` as parameter separator:
```
:s|foo|bar
```

To show confirmation for each substituition, use `c` flag:
```
:s/foo/bar/gc
```

To ignore case sensitivity, use `i` flag:
```
:s/foo/bar/gi
```

> There is much more options, look at the **ref** link for more...
**ref**: https://linuxize.com/post/vim-find-replace/

1 comment on commit 3cdfd4e

@vercel
Copy link

@vercel vercel bot commented on 3cdfd4e Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.