usr_02.txt ForVim version 9.2. Last change: 2026 Feb 14 VIM USER MANUALbyBramMoolenaar The first steps in VimThis chapter provides just enough information to edita file with Vim. Notwell or fast, but you can edit. Take some time to practice with thesecommands, they form the base for what follows.02.1 Running Vim for the First Time02.2 Inserting text02.3 Moving around02.4 Deleting characters02.5 Undo and Redo02.6 Other editing commands02.7 Getting out02.8 Findinghelp Next chapter:usr_03.txt Moving around Previous chapter:usr_01.txt About the manualsTable of contents:usr_toc.txt==============================================================================02.1 Running Vim for the First TimeTo start Vim, enter this command:gvim file.txtIn UNIX you can type thisat any command prompt. If you are running MicrosoftWindows, opena Command Prompt and enter the command. In either case, Vim starts editinga file called file.txt. Because thisisa new file, you geta blank window. Thisis what your screen will looklike:+---------------------------------------+|#||~||~||~||~||"file.txt" [New file]|+---------------------------------------+('#'is the cursor position.)The tilde (~) lines indicate lines not in the file. In other words, when Vimruns out of file to display,it displays tilde lines. At the bottom of thescreen,a message line indicates the fileis named file.txt and shows that youare creatinga new file. The message informationis temporary and otherinformation overwrites it.THE VIM COMMANDThegvim command causes the editor to createa newwindow for editing. If youuse this command:vim file.txtthe editing occurs inside your command window. In other words, if you arerunning inside an xterm, the editor uses your xterm window. If you are usinganMS-Windows command prompt window, the editing occurs inside this window.The text in thewindow will look the same for both versions, but withgvim youhave extra features, likea menu bar. More about that later.==============================================================================02.2 Inserting textThe Vim editorisa modal editor. That means that the editor behavesdifferently, depending on which mode you are in. The two basic modes arecalledNormal mode andInsert mode. InNormal mode the characters you typeare commands. InInsert mode the characters are insertedas text. Since you have just started Vimit will be inNormal mode. To startInsertmode you type the "i" command (i for Insert). Then you can enterthe text. It will be inserted into the file. Do not worry if you makemistakes; you can correct them later. To enter the following programmer'slimerick, thisis what you type:iA very intelligent turtleFound programming UNIX a hurdleAfter typing "turtle" you press the<Enter> key to starta new line. Finallyyou press the<Esc> key to stopInsert mode andgo back toNormal mode. Younow have two lines of text in your Vim window:+---------------------------------------+|A very intelligent turtle||Found programming UNIXa hurdle||~||~|||+---------------------------------------+WHAT IS THE MODE?To be able to see what mode you are in, type this command::set showmodeYou will notice that when typing the colon Vim moves the cursor to the lastline of the window. That's where you type colon commands (commands that startwitha colon). Finish this command by pressing the<Enter> key (all commandsthat start witha colon are finished this way). Now, if you type the "i" command Vim will display --INSERT--at the bottomof the window. This indicates you are inInsert mode.+---------------------------------------+|A very intelligent turtle||Found programming UNIXa hurdle||~||~||-- INSERT--|+---------------------------------------+If you press<Esc> togo back toNormal mode the last line will be made blank.GETTING OUT OF TROUBLEOne of the problems for Vim novicesis mode confusion, whichis caused byforgetting which mode you are in or by accidentally typinga command thatswitches modes. To get back toNormal mode, no matter what mode you are in,press the<Esc> key. Sometimes you have to pressit twice. If Vim beeps backat you, you already are inNormal mode.==============================================================================02.3 Moving aroundAfter you return toNormal mode, you can move around by using these keys:h lefthjklj downk upl rightAt first,it may appear that these commands were chosenat random. After all,who ever heard of usingl for right? But actually, thereisa very goodreason for these choices: Moving the cursoris the most common thing youdo inan editor, and these keys are on thehome row of your right hand. In otherwords, these commands are placed where you can type them the fastest(especially when you type with ten fingers).Note:You can also move the cursor by using the arrow keys. If you do,however, you greatly slow down your editing because to press the arrowkeys, youmust move your hand from the text keys to the arrow keys.Considering that you might be doingit hundreds of times an hour, thiscan takea significant amount of time. Also, there are keyboards whichdo not have arrow keys, or whichlocate them in unusual places; therefore, knowing the use of thehjklkeys helps in those situations.One way to remember these commandsis thathis on the left,lis on theright andj points down. Ina picture: k h l jThe best way to learn these commandsis by using them. Use the "i" command toinsert some more lines of text. Then use thehjkl keys to move around andinsertaword somewhere. Don't forget to press<Esc> togo back toNormalmode. Thevimtutoris alsoa nice way to learn by doing.ForJapanese users, Hiroshi Iwatani suggested using this:Komsomolsk^ | Huan Ho<--- ---> Los Angeles(Yellow river) |v Java (the island, not the programming language)==============================================================================02.4 Deleting charactersTo deletea character, move the cursor overit and type "x". (Thisisathrowback to the old days of the typewriter, when you deleted things by typingxxxx over them.) Move the cursor to the beginning of the first line, forexample, and type xxxxxxx (seven x's) to delete "A very ". The result shouldlook like this:+---------------------------------------+|intelligent turtle||Found programming UNIXa hurdle||~||~|||+---------------------------------------+Now you caninsert new text, for example by typing:iA young <Esc>This begins aninsert (the i), inserts the words "A young", and then exitsinsert mode (the final<Esc>).The result:+---------------------------------------+|A young intelligent turtle||Found programming UNIXa hurdle||~||~|||+---------------------------------------+DELETING A LINETo deletea whole line use the "dd" command. The following line willthen move up to fill the gap:+---------------------------------------+|Found programming UNIXa hurdle||~||~||~|||+---------------------------------------+DELETING A LINE BREAKIn Vim you can join two lines together, which means that the line breakbetween themis deleted. The "J" command does this. Take these two lines:A young intelligentturtleMove the cursor to the first line and press "J":A young intelligent turtle==============================================================================02.5 Undo and RedoSuppose you delete too much. Well, you can typeit in again, but an easierway exists. The "u" command undoes the last edit. Takea lookat this inaction: After using "dd" to delete the first line, "u" bringsit back. Another one: Move the cursor to theA in the first line:A young intelligent turtleNow type xxxxxxx to delete "A young". The resultisas follows: intelligent turtleType "u" toundo the last delete. That delete removed theg, so theundorestores the character.g intelligent turtleThe next "u" command restores the next-to-last character deleted:ng intelligent turtleThe next "u" command gives you the u, and so on:ung intelligent turtleoung intelligent turtleyoung intelligent turtle young intelligent turtleA young intelligent turtleNote:If you type "u" twice, and the resultis that you get the same textback, you have Vim configured to workVi compatible. Look here to fixthis:not-compatible. This text assumes you work "The Vim Way". You might prefer to usethe good oldVi way, but you will have to watch out for smalldifferences in the text then.REDOIf youundo too many times, you can pressCTRL-R (redo) to reverse thepreceding command. In other words,it undoes the undo. To see this inaction, pressCTRL-R twice. The characterA and thespace afterit disappear:young intelligent turtleThere'sa special version of theundo command, the "U" (undo line) command.Theundo line command undoes all the changes made on the last line that wasedited. Typing this command twice cancels the preceding "U".A very intelligent turtle xxxxDelete veryA intelligent turtle xxxxxxDelete turtleA intelligentRestore line with "U"A very intelligent turtleUndo "U" with "u"A intelligentThe "U" commandisa change by itself, which the "u" command undoes andCTRL-Rredoes. This might bea bit confusing. Don't worry, with "u" andCTRL-R youcango to any of the situations you had. More about that insection32.2.==============================================================================02.6 Other editing commandsVim hasa large number of commands to change the text. SeeQ_in and below.Here area few often used ones.APPENDINGThe "i" command insertsa character before the character under the cursor.That works fine; but what happens if you want to add stuff to theend of theline? For that you need toinsert text after the cursor. Thisis done withthe "a" (append) command. For example, to change the lineand that's not saying much for the turtle.toand that's not saying much for the turtle!!!move the cursor over to the dotat theend of the line. Then type "x" todelete the period. The cursoris now positionedat theend of the line on thee in turtle. Now typea!!!<Esc>to append three exclamation points after thee in turtle:and that's not saying much for the turtle!!!OPENING UP A NEW LINEThe "o" command createsa new, empty line below the cursor and puts Vim inInsert mode. Then you can type the text for the new line. Suppose the cursoris somewhere in the first of these two lines:A very intelligent turtleFound programming UNIX a hurdleIf you now use the "o" command and type new text:oThat liked using Vim<Esc>The result is:A very intelligent turtleThat liked using VimFound programming UNIX a hurdleThe "O" command (uppercase) opensa line above the cursor.USING A COUNTSuppose you want to move up nine lines. You can type "kkkkkkkkk" or you canenter the command "9k". In fact, you can precede many commands witha number.Earlier in this chapter, for instance, you added three exclamation points totheend ofa line by typing "a!!!<Esc>". Another way todo thisis to use thecommand "3a!<Esc>". Thecount of 3 tells the command that follows to tripleits effect. Similarly, to delete three characters, use the command "3x". Thecount always comes before the commandit applies to.==============================================================================02.7 Getting outTo exit, use the "ZZ" command. This command writes the file and exits.Note:Unlike many other editors, Vim does not automatically makeabackupfile. If you type "ZZ", your changes are committed and there's noturning back. You can configure the Vim editor to producebackupfiles; see07.4.DISCARDING CHANGESSometimes you will makea sequence of changes and suddenly realize you werebetter off before you started. Not to worry; Vim hasaquit-and-throw-things-away command. It is::q!Don't forget to press<Enter> to finish the command.For those of you interested in the details, the three parts of this commandare the colon (:), which entersCommand-line mode; theq command, which tellsthe editor to quit; and the override command modifier (!). The override command modifieris needed because Vimis reluctant to throwaway changes. If you were to just type ":q", Vim would display an errormessage and refuse to exit:E37: No write since last change (use ! to override)By specifying the override, you are in effect telling Vim, "I know that whatI'm doing looks stupid, butI really want todo this."If you want to continue editing with Vim: The ":e!" command reloads theoriginal version of the file.==============================================================================02.8 FindinghelpEverything you always wanted to know can be found in the Vimhelp files.Don't be afraid to ask!If you know what you are looking for,itis usually easier to search foritusing thehelp system, instead of using Google. Because the subjects followa certain style guide.Also thehelp has the advantage of belonging to your particular Vim version.You won't seehelp for commands added later. These would not work for you.To get generichelp use this command::helpYou could also use the first function key<F1>. If your keyboard hasa<Help>keyit might workas well. If you don't supplya subject, ":help" displays the generalhelp window.The creators of Vim did something very clever (or very lazy) with thehelpsystem: They made thehelpwindowa normal editing window. You can use allthe normal Vim commands to move through thehelp information. Therefore h, j,k, andl move left, down, up and right. To get out of thehelp window, use the same command you use to get out ofthe editor: "ZZ". This will only close thehelp window, not exit Vim.As you read thehelp text, you will notice some text enclosed in verticalbars(for example,help). This indicatesa hyperlink. If you position thecursor anywhere between thebars and pressCTRL-] (jump to tag), thehelpsystem takes you to the indicated subject. (For reasons not discussed here,the Vim terminology fora hyperlinkis tag. SoCTRL-] jumps to the locationof thetag given by theword under the cursor.) Aftera few jumps, you might want togo back.CTRL-T (pop tag) takes youback to the preceding position.CTRL-O (jump to older position) also worksnicely here. At the top of thehelp screen, thereis thenotation "*help.txt*". Thisname between "*" charactersis used by thehelp system to defineatag(hyperlink destination). See29.1 for details about using tags.To gethelp ona given subject, use the following command::help {subject}To gethelp on the "x" command, for example, enter the following::help xTo find out how to delete text, use this command::help deletingTo geta completeindex of all Vim commands, use the following command::help indexWhen you need to gethelp foracontrol character command (for example,CTRL-A), you need tospellit with the prefix "CTRL-".:help CTRL-AThe Vim editor has many different modes. By default, thehelp system displaysthe normal-mode commands. For example, the following command displayshelpfor the normal-modeCTRL-H command::help CTRL-HTo identify other modes, usea mode prefix. If you want thehelp for theinsert-mode version ofa command, use "i_". ForCTRL-H this gives you thefollowing command::help i_CTRL-HWhen you start the Vim editor, you can use several command-line arguments.These all begin witha dash (-). To find what the-t argument does, forexample, use the command::help -tThe Vim editor hasa number ofoptions that enable you to configure andcustomize the editor. If you wanthelp for an option, you need to encloseitin single quotation marks. To find out what the'number' option does, forexample, use the following command::help 'number'The table with all mode prefixes can be found below:help-summary.Special keys are enclosed in angle brackets. To findhelp on the up-arrow keyinInsert mode, for instance, use this command::help i_<Up>If you see an error message that you don't understand, for example:E37: No write since last change (use ! to override)You can use the error IDat the start to findhelp about it::help E37Summary:help-summary1) Use Ctrl-D after typinga topic and let Vim show all available topics. Or pressTab to complete::help some<Tab> More information on how to use the help::help helphelp2) Follow the links inbars to related help. You cango from the detailedhelp to the user documentation, which describes certain commands more froma user perspective andless detailed. E.g. after::help pattern.txt You can see the user guide topics03.9 andusr_27.txt in the introduction.3) Options are enclosed in single apostrophes. Togo to thehelp topic for thelist option::help 'list' If you only know you are looking fora certain option, you can also do::help options.txt to open thehelp page which describes all option handling and then search using regular expressions, e.g. textwidth. Certainoptions have their own namespace, e.g.::help cpo-<letter> for the corresponding flag of the'cpoptions' settings, substitute<letter> bya specific flag, e.g.::help cpo-; And for the'guioptions' flags::help go-<letter>4)Normal mode commandsdo not havea prefix. Togo to thehelp page for the "gt" command::help gt5)Insert mode commands start with i_. Help fordeletinga word::help i_CTRL-W6)Visual mode commands start with v_. Help for jumping to the other side of theVisual area::help v_o7) Command line editing and arguments start with c_. Help for using the command argument %::help c_%8) Ex-commands always start with ":", so togo to the ":s" command help::help :s9) Commands specifically for debugging start with ">". Togo to thehelp for the "cont" debug command::help >cont10) Key combinations. They usually start witha singleletter indicating the mode for which they can be used. E.g.::help i_CTRL-X takes you to the family ofCTRL-X commands forinsert mode which can be used to auto-complete different things. Note, that certain keys will always be written the same, e.g. Control will always be CTRL. For normal mode commands thereis no prefix and the topicis availableat:hCTRL-<Letter>. E.g.:help CTRL-W In contrast:help c_CTRL-R will describe what theCTRL-R does when entering commands in the Command line and:help v_CTRL-A talks about incrementing numbers in visual mode and:help g_CTRL-A talks about the "g<C-A>" command (e.g. you have to press "g" then<CTRL-A>). Here the "g" stands for the normal command "g" which always expectsa second key before doing something similar to the commandsstarting with "z".11) Regexp items always start with/. So to gethelp for the "\+" quantifier in Vim regexes::help /\+ If you need to know everything about regular expressions, start reading at::help pattern.txt12) Registers always start with "quote". To find out about the special ":" register::help quote:13) Vimscriptis availableat:help eval.txt Certain aspects of the language are availableat:h expr-X where "X"isa single letter. E.g.:help expr-! will take you to the topic describing the "!" (Not)operator for Vim script. Also importantis:help function-list to finda short description of allfunctions available. Help topics for Vimscriptfunctions always include the "()", so::help append() talks about the append Vimscript function rather than how to append text in the current buffer.14) Mappings are talked about in thehelp page:hmap.txt. Use:help mapmode-i to find out about the:imap command. Also use :map-topic to find out about certain subtopics particular for mappings. e.g::help :map-local for buffer-local mappings or:help map-bar for how the'|'is handled in mappings.15) Commanddefinitions are talked about:h command-topic, so use:help command-bang to find out about the '!' argument for custom commands.16) Window management commands always start withCTRL-W, so you find the correspondinghelpat:hCTRL-W_letter. E.g.:help CTRL-W_p for moving the previous accessed window. You can also access:help windows.txt and read your way through if you are looking forwindow handling commands.17) Use:helpgrep to search in allhelp pages (and also of any installed plugins). See:helpgrep for how to use it. To search fora topic::helpgrep topic This takes you to the first match. Togo to the next one::cnext All matches are available in thequickfixwindow which can be opened with::copen Move around to the match you like and press Enter to jump to that help.18) The user manual. This describeshelp topics for beginners ina rather friendly way. Startatusr_toc.txt to find the table of content (as you might have guessed)::help usr_toc.txt Skim over the contents to find interesting topics. The "Digraphs" and "Entering special characters" items are in chapter 24, so togo to that particularhelp page::help usr_24.txt Also if you want to accessa certain chapter in the help, the chapter number can be accessed directly like this::help 10.1 which goes to chapter10.1 inusr_10.txt and talks aboutrecording macros.19) Highlighting groups. Always start with hl-groupname. E.g.:help hl-WarningMsg talks about the WarningMsg highlighting group.20)Syntax highlightingis namespaced to :syn-topic. E.g.:help :syn-conceal talks about theconceal argument for the ":syn" command.21)Quickfix commands usually start with:c while locationlist commands usually start with:l22) Autocommand events can be found by their name::help BufWinLeave To see all possible events::help autocommand-events23)Command-line switches always start with "-". So for thehelp of the-f command switch of Vim use::help -f24) Optional features always start with "+". To find out about theconceal feature use::help +conceal25) Documentation for includedfiletype specific functionalityis usually available in the form ft-<filetype>-<functionality>. So:help ft-c-syntax talks about theCsyntax file and the optionit provides. Sometimes, additional sections for omni completion:help ft-php-omni orfiletype plugins:help ft-tex-plugin are available.26) Error and Warning codes can be looked up directly in the help. So:help E297 takes you exactly to the description of the swap error message and:help W10 talks about the warning "Changinga readonly file". Sometimes, however, those error codes are not described, but rather are listedat the Vim command that usually causes this. So::help E128 takes you to the:function command27) Documentation forpackages distributed with Vim have the form package-<name>. So:help package-comment will bring you to thehelpsection for the included commentplugin and how to enable it.==============================================================================Next chapter:usr_03.txt Moving aroundCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: