Opening a file, getting around, inserting text and saving

vim filename.txt

h j k l

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​

i

ESC

:wq

Moving around (bonus round)

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

i

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​

​

ESC

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

x

​

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

:h

:h dd

:q

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!

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.When you want to edit a file in vim, you simply type. You will enter vim in 'normal' mode and you'll be able to move around the file with thekeys (or, use the arrow keys if you like).Now that you're where you want to be in the file, you'll want to click the letterto change to 'insert' mode. Now, you can type some text. Hit(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(for write quit).There's MUCH more to vim, however - and we'll go into some of that now.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...Probably the most common way of getting into insert mode is simply pressingas 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:When you're done with insert mode, make sure to hitto get back into normal mode..Removing a character in vim is as easy as moving the curser over it and hitting thekey 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.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 typefollowed by the command you're wondering about, like. Justto quit out of the help.