Vim basics and more

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,210
Reaction score
2,240
Credits
3,485
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 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 left​
j moves the curser one character up​
k moves the curser one character down​
l moves the curser one character to the right​

Now 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 left​
2j moves the curser 2 lines up​
3k moves the curser 3 lines down​
6l 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 forward​
ctrl-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 is​
a will put you into insert mode, but one spot after where the curser is​
o will start a new line under where the curser is and put you in insert mode​
I (capital i) puts you at the beginning of the current line and into insert mode​
A 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-only​
ZZ 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 word​
yy yanks (copies) one line​
5yy yanks (copies) 5 lines​
Deleting (and/or cutting)​
x delete character​
5x delete 5 characters​
dw delete word - more specifically, deletes from your position to the end of the word​
dd 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 state
ctrl-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 vim

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 :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:


You mentioned 'p' to paste (after the cursor) but not 'P' to paste before the cursor.

When I was first learning vi the really cool and powerful thing to me was how you could follow the 'c', 'd' and 'y' commands by any cursor movement command to control how much of the file would be affected by the command. You mentioned that "dw" would delete a word but not that this worked because 'w' would move the cursor by one word. It is also very powerful to know that you can add a count to these commands. For example "3w" will move the cursor three words and "d3w" will delete three words.
 
On the movement front, in command mode you also have:

ctrl-u and ctrl-d which will scroll up/down half a page at a time.

And ctrl-e and ctrl-y which will scroll up/down one line at a time without moving the cursor.
This one is good if you have the cursor at the top, or bottom of the screen, ready to edit some text - but you need to scroll backwards or forwards a few lines to remind yourself of what came before, or after the line with the cursor on. You can scroll up/down without losing your place in the file.

Also H, M or L will cause the cursor to jump to the top, middle or bottom line of the current screen-full of text. To remember these shortcuts I think High, Middle and Low.
 
Also on the command-mode cursor movement front there are f and F.

f will move the cursor forward to a particular character on the line.
F will move the cursor backwards to a particular character on the line.

e.g.
Hitting the keys:
fc - Would move the cursor forward to the next lowercase c character.

Fc - would move the cursor backwards to the previous lowercase c.

Likewise:
fC - Would go forwards to the next uppercase C character.
FC - Would go backwards to the previous uppercase C character.

You can use motions with the f and F commands.
e.g.
3fa - would move the cursor to the third lowercase a character in the current line.

I think you get the idea!


Also, there are a few neat keybinds for edit mode:
CTRL-w delete word to the left of cursor
CTRL-u delete everything to the left of cursor
CTRL-h backspace/delete
CTRL-j insert newline (easier than reaching for the return key)
CTRL-t indent current line
CTRL-d un-indent current line

And I know that any experienced vim users will be screaming that the whole point in vim being modal is that you do short edits and switch back to command mode. But when you are doing some serious writing or coding, unnecessarily switching modes breaks the flow a little. But the edit-mode shortcuts/keybinds can help you to keep going for a while longer, without switching to command-mode.

Another cool thing in edit mode:
CTRL-o allows you to perform a single command-mode command and immediately return to edit mode. I often use CTRL-o to fire off a macro, or to perform edits without leaving edit mode.
After pressing CTRL-o you can perform any typical command mode command by pressing the associated keybind, or by hitting the colon : and typing any command. As soon as the command has done it's thing, you're straight back in edit mode and can carry on typing.

So for example:
Perhaps you've started editing in the middle of a line and you've decided that everything to the right of the cursor is no longer needed. You can get rid of everything to the right of the cursor using: CTRL-o D
NOTE: That's capital D, so you'd have to press shift and D

After hitting that keybind - the extraneous characters at the end of the line are removed and you are put straight back into edit mode, so you can carry on with your edit.


And finally we have vims registers.
Vims registers are a massive topic unto themselves.
You can use the registers to hold macros, or to hold snippets of text. I'll leave macros for another day! But I'll explain registers, because there is an edit mode shortcut that will allow you to paste text from a register using a keybind in edit-mode.

But first a bit about registers:

The available registers are 0-9 and a-z
There are also some special buffers: " + = * : / -

You can see the content of all non-empty registers in command mode using the :reg command.

To copy (or yank) text to a register, you highlight it using visual mode and then hit the " key and then the key associated with the register you want to copy the text to and then either the d (delete) or the y (yank) key.

So to copy/yank a piece of text to buffer t, you'd select the text you wanted to cut/copy/yank in visual mode and then copy it to the t buffer by pressing "ty or "td.

To paste from buffer t in command mode, you would use "tp or "tP depending on whether you wanted it posted before the cursor position or after it.

Also, you can quickly paste things from any of vims registers in edit mode, without having to switch to command mode.

The way to do this is to simply press CTRL-r and then hit the key associated with the register you want to paste from.
NOTE: This has to be done in edit mode. In command-mode CTRL-r is the shortcut for redo!

Anyway - So if you're typing away in edit mode and you suddenly need to paste the text from buffer t, you'd hit:
CTRL-r t

The + and * registers are associated with the system clipboard. So to quickly paste something from the system clipboard into vim (in edit mode) you would use:
CTRL-r +
And that would paste from the system clipboard into the current buffer/document and allow you to continue typing without leaving edit mode.

Another useful register in edit mode is the evaluation register = which will allow you to evaluate mathematical expressions as you type.
So if you're typing and you need to enter the square root of PI, you could calculate it on the fly like this:
ctrl-r =
Then type the expression to be evaluated and press enter:
e.g.
sqrt(3.14159265)<enter>

So the entire command would be:
ctrl-r = sqrt(3.14159265)<enter>

Then the numeric value of the square-root of PI will appear in your text and you can carry on typing in edit mode.

And if any of your registers contain macros, you can fire them off from edit mode using:
CTRL-o @{register}

Where {register} is the register containing the macro.
So if you have a macro set up which inserts todays date stored in register d (easy to remember, d for date!), you could fire off the macro in edit mode using:
CTRL-o @d

And todays date will appear in the text and you can continue editing. I'll go into recording and playing back macros in another post.

So I've covered a lot here. The primary focus was on some edit mode shortcuts, to prevent unnecessary switching to command mode. But we did go a little off-topic with the registers and macros. But it was still relevant, because the registers and macros can be used in edit mode, without switching to command mode.
 
Last edited:
Also on the command-mode cursor movement front there are f and F.

f will move the cursor forward to a particular character on the line.
F will move the cursor backwards to a particular character on the line.

e.g.
Hitting the keys:
fc - Would move the cursor forward to the next lowercase c character.

Fc - would move the cursor backwards to the previous lowercase c.

Likewise:
fC - Would go forwards to the next uppercase C character.
FC - Would go backwards to the previous uppercase C character.

You can use motions with the f and F commands.
e.g.
3fa - would move the cursor to the third lowercase a character in the current line.

I think you get the idea!


Also, there are a few neat keybinds for edit mode:
CTRL-w delete word to the left of cursor
CTRL-u delete everything to the left of cursor
CTRL-h backspace/delete
CTRL-j insert newline (easier than reaching for the return key)
CTRL-t indent current line
CTRL-d un-indent current line

And I know that any experienced vim users will be screaming that the whole point in vim being modal is that you do short edits and switch back to command mode. But when you are doing some serious writing or coding, unnecessarily switching modes breaks the flow a little. But the edit-mode shortcuts/keybinds can help you to keep going for a while longer, without switching to command-mode.

Another cool thing in edit mode:
CTRL-o allows you to perform a single command-mode command and immediately return to edit mode. I often use CTRL-o to fire off a macro, or to perform edits without leaving edit mode.
After pressing CTRL-o you can perform any typical command mode command by pressing the associated keybind, or by hitting the colon : and typing any command. As soon as the command has done it's thing, you're straight back in edit mode and can carry on typing.

So for example:
Perhaps you've started editing in the middle of a line and you've decided that everything to the right of the cursor is no longer needed. You can get rid of everything to the right of the cursor using: CTRL-o D
NOTE: That's capital D, so you'd have to press shift and D

After hitting that keybind - the extraneous characters at the end of the line are removed and you are put straight back into edit mode, so you can carry on with your edit.


And finally we have vims registers.
Vims registers are a massive topic unto themselves.
You can use the registers to hold macros, or to hold snippets of text. I'll leave macros for another day! But I'll explain registers, because there is an edit mode shortcut that will allow you to paste text from a register using a keybind in edit-mode.

But first a bit about registers:

The available registers are 0-9 and a-z
There are also some special buffers: " + = * : / -

You can see the content of all non-empty registers in command mode using the :reg command.

To copy (or yank) text to a register, you highlight it using visual mode and then hit the " key and then the key associated with the register you want to copy the text to and then either the d (delete) or the y (yank) key.

So to copy/yank a piece of text to buffer t, you'd select the text you wanted to cut/copy/yank in visual mode and then copy it to the t buffer by pressing "ty or "td.

To paste from buffer t in command mode, you would use "tp or "tP depending on whether you wanted it posted before the cursor position or after it.

Also, you can quickly paste things from any of vims registers in edit mode, without having to switch to command mode.

The way to do this is to simply press CTRL-r and then hit the key associated with the register you want to paste from.
NOTE: This has to be done in edit mode. In command-mode CTRL-r is the shortcut for redo!

Anyway - So if you're typing away in edit mode and you suddenly need to paste the text from buffer t, you'd hit:
CTRL-r t

The + and * registers are associated with the system clipboard. So to quickly paste something from the system clipboard into vim (in edit mode) you would use:
CTRL-r +
And that would paste from the system clipboard into the current buffer/document and allow you to continue typing without leaving edit mode.

Another useful register in edit mode is the evaluation register = which will allow you to evaluate mathematical expressions as you type.
So if you're typing and you need to enter the square root of PI, you could calculate it on the fly like this:
ctrl-r =
Then type the expression to be evaluated and press enter:
e.g.
sqrt(3.14159265)<enter>

So the entire command would be:
ctrl-r = sqrt(3.14159265)<enter>

Then the numeric value of the square-root of PI will appear in your text and you can carry on typing in edit mode.

And if any of your registers contain macros, you can fire them off from edit mode using:
CTRL-o @{register}

Where {register} is the register containing the macro.
So if you have a macro set up which inserts todays date stored in register d (easy to remember, d for date!), you could fire off the macro in edit mode using:
CTRL-o @d

And todays date will appear in the text and you can continue editing. I'll go into recording and playing back macros in another post.

So I've covered a lot here. The primary focus was on some edit mode shortcuts, to prevent unnecessary switching to command mode. But we did go a little off-topic with the registers and macros. But it was still relevant, because the registers and macros can be used in edit mode, without switching to command mode.

Very helpful. Thanks for this!
 
Another useful register in edit mode is the evaluation register = which will allow you to evaluate mathematical expressions as you type.
So if you're typing and you need to enter the square root of PI, you could calculate it on the fly like this:
ctrl-r =
Then type the expression to be evaluated and press enter:
e.g.
sqrt(3.14159265)<enter>

So the entire command would be:
ctrl-r = sqrt(3.14159265)<enter>

Then the numeric value of the square-root of PI will appear in your text and you can carry on typing in edit mode.

And another tip I never knew existed here! My god, I'm going to use this SO much!
 
# This clever lines are to check for blank spaces after the end of a line every time you save & exit. Simply put them on your .rc file

autocmd BufWritePre * %s/\s\+$//e

vnoremap <C-c> "*Y :let @+=@*<CR>
map <C-p> "+P

Screenshot from 2022-11-18 16-54-27.png



Screenshot from 2022-11-18 16-55-10.png
 

Members online


Latest posts

Top