Replace text in vi with confirmation
Answer:
Similar to this article, you want to replace text, but you want to vi/vim to ask for confirmation before the actual replacements.
:%s/foo/bar/gc
Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.
Replace text in vi with confirmation
Answer:
Similar to this article, you want to replace text, but you want to vi/vim to ask for confirmation before the actual replacements.
:%s/foo/bar/gc
Replace text in vi
Answer:
To replace a text in a file using vi/vim
Enter the command mode by typing :, and use the %s/foo/bar/g for global replacement(s).
:%s/foo/bar/g
All foo in the file will be replaced by bar
Save a file in vim without the needed permissions
Answer:
Very often, you opened a file using vim, edited it and find out that the file is opened as read only, i.e. you don't have permission to make any change(s).
What will you do? Simplest method is to save it into a temp file and rename it afterward.
However, there exist a simple method that allow you to save the file anyway.
Instead of using :wq, you enter the following.
:w !sudo tee %
That is what you need.
Quickly open to file and position a cursor with vim
Answer:
To open a file and automatically move the cursor to a particular line number
# vi +80 test.txt
The vim will move the cursor to line 80 when opened the file.
How to find the differences between two files (visually)
Answer:
Use the vimdiff command
E.g.
# vimdiff foo.txt bar.txt
You can enter vim and the differences between two files will be shown visually.