I've been using vim for about several years, and even today I find some tips and tricks! This article will review some of the things I use most often in vim, and I hope we get some great answers with some of your favorite tips! Let's go.
Open a file, scroll, insert, and save the text.
When you want to edit a file in vim, simply type vim file.txt. You enter vim in "normal" mode and you can move around the file using the H J K L keys (or use the arrow keys if you wish).
h Move the pointer one character to the left
j Move the cursor one character up
k Move the cursor one character down
l Move the pointer one character to the right
Now that you have become where you want to be in the file, you'll need to click on the letter i to switch to "insert" mode. Now, you can type some text. Press ESC (escape key) to exit "insert" mode and return to "normal" mode.
We've just finished editing the file and going back to normal mode. To save the file, simply type :wq (write to quit).
However, there is much to see and we will analyze some of that now.
Move (Bonus)
Without a doubt, you want to be able to scroll through the file to find where you want to edit it. I've already explained some common methods, but there are other practical ways to sort your file.
5h Move the cursor 5 characters to the left
2j The cursor moves 2 lines up
3k The cursor moves 3 lines down
6l Move the cursor 6 characters to the right
1G moves the cursor to line 1. You can replace the number 1 with any number to get this line of the file. For example: 25G will take in line 25 of the file.
G can take you to the last line of the file.
Ctrl-f will move one page forward
Ctrl-b will return one page back
There are many, but these are the most common I use.
Insertion mode
Perhaps the most common way to enter insertion mode is to press I as described above. This will take you to insert the position where the cursor sits in the document. But there are other options! I will remind them here:
i will put you into insertion mode just in front of where the cursor is located.
a will put you in insertion mode, but place after cursor place
o start a new line below where the cursor is located and place it in insertion mode
I (uppercase i) Place it at the beginning of the current line and in insertion mode
A will place you in the insertion mode and move the cursor to the end of the current line.
O (uppercase) starts a new line above its current position and places it in insertion mode
When you finish insertion mode, make sure to press ESC to return to normal mode.
Quit/Save
:q to Exit, do not save - they do not work unless no changes have been made.
:q! Leave, do not save - works even if changes have been made.
:w writes/saves the file, does not close.
:wq writes/saves the file, closes - will not work if the file is read-only
:wq! Type / save the file, close it - will force write even if the file is read-only
:x writes/saves the file, closes - will not work if the file is read-only
:x! Type / save the file, close it - will force write even if the file is read-only
ZZ writes/saves the file, closes it - will not work if the file is read-only - NOTE NO: It is used.
Copy / Paste / Delete / Search / Replace
Deleting a vim character is as easy as moving the cursor over it and pressing the x key in normal mode. Let's list some ways you can delete, replace, copy, and paste. You will need to do all this while you are in "normal" mode.
Yanks (copying)
yw yanks (copy) word; more specifically, it moves from its position at the end of the word
yy yanks (copy) line
5yy yanks (copy) 5 lines
Delete (and/cut)
x Delete the character
5x erase 5 characters
dw Remove the word - more specifically, delete from the position until the end of the word
dd Delete the line
Paste (works after yanks or deleting)
p Pastes anything deleted or deleted (any of the above-mentioned drag/drop operations)
Search
/pattern to find a file for your style. Look down, but it will be set and you'll see the beginning of the file after seeing what's on your back.
? pattern will look for your pattern in the opposite direction. Practical if it is at the end of the log file and looks for the last match closest to the end.
Replace
r Replace r with the current character .. so replacing s with a letter t would move the pointer over s and rt
R replaces the word/string from the cursor point. You do R and then you will stick to your variant.
Search and Replace
:s/old/new replaces the first instance of the old string with the new string in the current line
:s/old/new/ g Replace all instances of the old string with the new string in the current line
:%s/old/new/g g Replace all instances of the old string with the new string in the entire file
:7,25s/old/new Replace all instances of the old string with the new string in lines 7 through 25
in the fresh air
Other useful things
u You can undo - you can still hit to u undo the changes you've made.
U Undo the entire line - This will undo the last line you modified to its original state
CTRL-r redo - will u undo you
:set paste This will enable Paste Mode, so the paste will not be automatically indented on your paste
:set nopaste This will turn off Paste Mode,
:set numbers This will display the number lines along the left side
:set nonumbers This will turn off line number
:set ic Ignores the situation when looking for things
:his History of Your command
:! this with commands such as pwd or ls to run commands within vim
Get help inside vim
If you are wondering how to use something, vim has an incredible help command you can call to learn more about things ... Just enter the normal mode and type h followed by the command you are asking, such as dd Solo: q Leave the help.
Roll
Well, that's all I can think of now; you can go back and update this article in the future. I know there are more than a ton of FIM. You can always use vim on a Linux system for more information at any time!
Source and thanks to: Linux.org
Answer below with some of the most common things you use within vim!
- [message]
- ##laptop## Support
- Support Us by Disable your ad block.
- thank you
COMMENTS