I've been using vim for about 22 years and even today find little tips and tidbits that I didn't know about! This article will go through some of the things I use often in vim - and I hope we'll get some great replies with what some of your favorite tips are! Let's get going.
Let's start out with some basics first, then down lower in the article we'll get to some of the more interesting/fun tips and tricks.
Opening a file, getting around, inserting text and saving
When you want to edit a file in vim, you simply type
Now that you're where you want to be in the file, you'll want to click the letter
We're now finished editing the file and back in 'normal' mode. To save the file, simply type
There's MUCH more to vim, however - and we'll go into some of that now.
Moving around (bonus round)
No doubt you'll want to be able to get around in the file so you can find the spot you want to edit. Above I showed you some common ways, but here some other handy ways to get around the file...
Insert mode
Probably the most common way of getting into insert mode is simply pressing
Quitting / Saving
Copying / Pasting / Deleting / Searching / Replacing
Removing a character in vim is as easy as moving the curser over it and hitting the
If you're wondering about how to use something, vim has an awesome help command that you can invoke to learn more about things.. just go into normal mode and type
Wrapping it up
Well, that's about all I can think of for now - I may come back and update this article some time in the future. I know there's a ton more that vim can do. You can always
Please reply below with some of the most common things you use within vim!
Let's start out with some basics first, then down lower in the article we'll get to some of the more interesting/fun tips and tricks.
Opening a file, getting around, inserting text and saving
When you want to edit a file in vim, you simply type
vim filename.txt
. You will enter vim in 'normal' mode and you'll be able to move around the file with the h j k l
keys (or, use the arrow keys if you like).h
moves the curser one character to the leftj
moves the curser one character upk
moves the curser one character downl
moves the curser one character to the rightNow that you're where you want to be in the file, you'll want to click the letter
i
to change to 'insert' mode. Now, you can type some text. Hit ESC
(escape key) to exit 'insert' mode and go back into 'normal' mode.We're now finished editing the file and back in 'normal' mode. To save the file, simply type
:wq
(for write quit).There's MUCH more to vim, however - and we'll go into some of that now.
Moving around (bonus round)
No doubt you'll want to be able to get around in the file so you can find the spot you want to edit. Above I showed you some common ways, but here some other handy ways to get around the file...
5h
moves the curser 5 characters to the left2j
moves the curser 2 lines up3k
moves the curser 3 lines down6l
moves the curser 6 characters to the right
1G
moves curser to line 1. You can replace 1
with any number to get on that line of the file. eg: 25G
would get you to the 25th line of the file.G
will get you to the last line of the file.ctrl-f
will move you 1 page forwardctrl-b
will move you 1 page back
There are plenty more, but these are the most common that I use...
Insert mode
Probably the most common way of getting into insert mode is simply pressing
i
as demonstrated above. This will get you into insert mode right where your curser is sitting in the document. There's other options though! I'll list them out here:i
will put you into insert mode right in front of where the curser isa
will put you into insert mode, but one spot after where the curser iso
will start a new line under where the curser is and put you in insert modeI
(capital i) puts you at the beginning of the current line and into insert modeA
will put you in insert mode and move the curser to the end of the current line.O
(capital o) starts a new line above your current position and puts you in insert mode
When you're done with insert mode, make sure to hit ESC
to get back into normal mode..Quitting / Saving
:q
quit, don't save - only works if no changes were made:q!
quit, don't save - works even if changes were made:w
writes/saves the file, does not quit:wq
writes/saves the file, quits - will not work if file is read-only:wq!
writes/saves the file, quits - will force write even if file is read-only:x
writes/saves the file, quits - will not work if file is read-only:x!
writes/saves the file, quits - will force write even if file is read-onlyZZ
writes/saves the file, quits - will not work if file is read-only - note no :
is used.Copying / Pasting / Deleting / Searching / Replacing
Removing a character in vim is as easy as moving the curser over it and hitting the
x
key while in normal mode. Let's list out some of the ways you can delete, replace, copy (yank) and paste. You'll want to do all of these while in 'normal' mode.
Yanking (copying)
yw
yanks (copies) a word - more specifically, yanks from your position to the end of the wordyy
yanks (copies) one line5yy
yanks (copies) 5 lines
Deleting (and/or cutting)
x
delete character5x
delete 5 charactersdw
delete word - more specifically, deletes from your position to the end of the worddd
delete line
Pasting (works after yanking or deleting)
p
pastes whatever you just yanked or deleted (any of the yanks/deletes listed above)
Searching
/pattern
to search the file for your pattern. It searches downward, but will wrap and look at the beginning of the file after looking through whatever is below you currently.?pattern
will search for your pattern in the opposite direction. Handy if you're at the end of a log file and looking for the latest match closest to the bottom.
Replacing
r
replaces current character.. so to replace s with t you would move the curser over the s and rt
R
replaces word/string from the spot of your curser. You'd do R
then paste your replacement
Searching AND Replacing
:s/old/new
replaces the first instance of the string old
with the string new
on your current line:s/old/new/g
replaces all of the instances of the string old
with the string new
on your current line:%s/old/new/g
replaces all of the instances of the string old
with the string new
in your whole file:7,25s/old/new
replaces all of the instances of the string old
with the string new
on lines 7 through 25
Other handy things...u
undo - you can keep hitting u
to undo changes you've made.U
undo whole line - this will undo the entire last line you modified to it's original statectrl-r
redo - will redo the undone from u
:set paste
This will enable 'paste mode' so it won't automatically indent on your paste:set nopaste
This will turn off the 'paste mode':set numbers
This will display number lines along the left side:set nonumbers
This will turn off line number display:set ic
ignores case when you're searching for stuff
:his
view your command history:!
follow this with commands like pwd
or ls
to run commands within vim
Getting help within vimIf you're wondering about how to use something, vim has an awesome help command that you can invoke to learn more about things.. just go into normal mode and type
:h
followed by the command you're wondering about, like :h dd
. Just :q
to quit out of the help.Wrapping it up
Well, that's about all I can think of for now - I may come back and update this article some time in the future. I know there's a ton more that vim can do. You can always
man vim
on your Linux system to get more info any time! Please reply below with some of the most common things you use within vim!
Last edited: