= Vim basic ! Apostolos rootApostolos@swarmlab.io // Metadata: :description: Intro and Install :keywords: ubuntu install dual :data-uri: :toc: right :toc-title: Πίνακας περιεχομένων :toclevels: 4 :source-highlighter: highlight :icons: font :sectnums: {empty} + .vim **** Vim is the editor of choice for many developers and power users. It’s a _modal_ text editor based on the vi editor. It inherits the key bindings of vi, but also adds a great deal of functionality and extensibility that are missing from the original vi. [NOTE] ==== *vim modals*: *insert* mode and *command* mode. TIP: vim *starts* in *command mode*. * command mode ** In this mode, characters you type is interpreted as a command * insert mode ** in insert mode, the characters you type is just inserted. TIP: To *enter insert* mode, press *i*. To get back in command mode, press *esc* ==== **** [NOTE] ==== *All editors have a command and an input mode;* Most editors default to the input mode whithin which the text we type is interpretted as a raw string to be appendeed to the cursors position. To "enter" command mode we the have to use some king of control key/button. Thus the command mode is disguised and we only use it briefly. Vim on the other hand has three distinct modes: - normal mode: The COMMAND mode. Here we can insert commands that can perform all sorts of tasks just like the buttons we mentioned before. The difference is that vim will stay in normal mode unsless we tell it to enter another mode. This alowes for faster commands(no need for menus and mouse clicks) but also for more complicated commands (combos). - insert mode: the mode to INSERT text like in any other editor - visual mode: this mode exists in all editors, although it usually is combined into insert mode. Here we can do visual tasks like selecting text or highlighting regions. ==== == Vim Key Bindings === Move .Key combinations [options="header,footer"] |======================= |Key|Action | Followed by |b |back word | |e |end of word | |f |find character after cursor in current line | |m |mark current line and position mark character | tag (a-z) |n |repeat last search | |t |same as "f" but cursor moves to just before found character | character to find |w |move foreward one word | |z |position current line | CR = top; "." = center; "-"=bottom |B |move back one Word | |E |move to end of Word | |F |backwards version of 'f' | character to find |G |goto line number prefixed, or goto end if none | |H |home cursor - goto first line on screen | |J |join current line with next line | |L |goto last line on screen | |M |goto middle line on screen | |N |repeat last search, but in opposite direction of original search | |T |backwards version of "t" character to find | |W |foreward Word | |0 |move to column zero | |! |shell command filter cursor motion command, shell command | |$ |move to end of line | |% |match nearest [],(),{} on line, to its match (same line or others) | |^ |move to first non-whitespace character of line | |( |move to previous sentence | |) |move to next sentence | |\| |move to column zero | |- |move to first non-whitespace of previous line | |+ |move to first non-whitespace of next line | |[ |move to previous "{...}" section | "[" |] |move to next "{...}" section | "]" |{ |move to previous blank-line separated section | "{" |} |move to next blank-line separated section | "}" |' |move to marked line, first non-whitespace | character tag (a-z) |` |move to marked line, memorized column | character tag (a-z) |/ |search forward | search string, ESC or CR |? |search backward | search string, ESC or CR |^D |down half screen | |^J |line down | |^T |go to the file/code you were editing before the last tag jump | |^U |up half screen | |^^ |switch file buffers |======================= [source,bash] ---- w // <1> e // <2> ---- <1> move the cursor forwards to the start of the next word <2> move the cursor forwards to the end of the next word === List Allmost complete key binding reference .Definitions [NOTE] ==== - *word* - a lower-case word ("w", "b", "e" commands) is defined by a consecutive string of letters, numbers, or underscore, or a consecutive string of characters that is not any of {letters, numbers, underscore, whitespace} - Word - an upper-case word ("W", "B", "E" commands) is a consecutive sequence of non-whitespace. - *cursor motion command* - any command which positions the cursor is ok here, including the use of numeric prefixes. In addition, a repeat of the edit command usually means to apply to the entire current line. For example, "<<" means shift current line left; "cc" means replace entire current line; and "dd" means delete entire current line. *Key Bindings in Editing Modes* - While in any edit mode (insert, replace, etc.) there are some keys that are used to adjust behaviour, rather than just to insert text. [source,bash] ---- ESC - leave edit mode, return to command mode ^D - move line backwards one shiftwidth. shiftwidth must be set, and either the line must be newly added, or ^T must have been used. ^T - move all after cursor forwards one shiftwidth ---- ==== .Key combinations [options="header,footer"] |======================= |Key|Action | Followed by |a |enter insertion mode after current character | text, ESC |b |back word | |c |change command cursor | motion command |d |delete command cursor | motion command |e |end of word | |f |find character after cursor in current line | |i |enter insertion mode before current character | text, ESC |m |mark current line and position mark character | tag (a-z) |n |repeat last search | |o |open line below and enter insertion mode | text, ESC |p |put buffer after cursor |r |replace single character at cursor | replacement character expected |s |substitute single character with new text | text, ESC |t |same as "f" but cursor moves to just before found character | character to find |u |undo | |w |move foreward one word | |y |yank command | cursor motion command |z |position current line | CR = top; "." = center; "-"=bottom |A |enter insertion mode after end of line | text, ESC |B |move back one Word | |C |change to end of line | text, ESC |D |delete to end of line | |E |move to end of Word | |F |backwards version of "f" | character to find |G |goto line number prefixed, or goto end if none | |H |home cursor - goto first line on screen | |I |enter insertion mode before first non-whitespace character | text, ESC |J |join current line with next line | |L |goto last line on screen | |M |goto middle line on screen | |N |repeat last search, but in opposite direction of original search | |O |open line above and enter insertion mode | text, ESC |P |put buffer before cursor | |Q |leave visual mode (go into "ex" mode) | |R |replace mode - replaces through end of current line, then inserts | text, ESC |S |substitute entire line - deletes line, enters insertion mode | text, ESC |T |backwards version of "t" character to find | |U |restores line to state when cursor was moved into it | |W |foreward Word | |Y |yank entire line | |0 |move to column zero | |1-9| numeric precursor to other commands [additional numbers (0-9)] command | |! |shell command filter cursor motion command, shell command | | |@ |vi eval buffer name (a-z) | |$ |move to end of line | |% |match nearest [],(),{} on line, to its match (same line or others) | |^ |move to first non-whitespace character of line | |& |repeat last ex substitution (":s ...") not including modifiers | |( |move to previous sentence | |) |move to next sentence | |\| |move to column zero | |- |move to first non-whitespace of previous line | |_ |similar to '^' but uses numeric prefix oddly | |+ |move to first non-whitespace of next line | |[ |move to previous "{...}" section | "[" |] |move to next "{...}" section | "]" |{ |move to previous blank-line separated section | "{" |} |move to next blank-line separated section | "}" |; |repeat last "f", "F", "t", or "T" command | |' |move to marked line, first non-whitespace | character tag (a-z) |` |move to marked line, memorized column | character tag (a-z) |: |ex-submode ex command | |" |access numbered buffer; load or access lettered buffer | 1-9,a-z |~ |reverse case of current character and move cursor forward | |, |reverse direction of last "f", "F", "t", or "T" command | |. |repeat last text-changing command | |/ |search forward | search string, ESC or CR |< |unindent command | cursor motion command |> |indent command | cursor motion command |? |search backward | search string, ESC or CR |^D |down half screen | |^G |show status | |^J |line down | |^T |go to the file/code you were editing before the last tag jump | |^U |up half screen | |^Z |suspend program | |^[ |(ESC) cancel started command; otherwise UNBOUND | |^\ |leave visual mode (go into "ex" mode) | |^^ |switch file buffers |=======================