aide vim en aide vim

Vim help

back to the home page

version available in : it ru zh es ja de nl fr

date of creation : 20220618- date of update : 20220618- generation date : 20240612_171233

1.Gvim

vim vim_25_presentation_by_bram_moolenaar_on_2016_november_2 mastering_the_vim_language

2.Get started with vi

  • vi is the standard editor on any UNIX distribution. vi has not evolved since its creation which makes it a rustic editor.
    But its original design is well in the spirit of unix"^.
  • the evolution of vi is the vim editor (vi-improved de Bram Moolenar)see below.
  • launch the editor with the name of the file to process as argument "vi toto.txt"
  • the editor launches a full screen window where the lines of text appear. Empty lines beginning with "~" are visible if the number of lines in the file is less than the number of lines in the window.
  • by default we are in the command mode of the editor, that is to say that all the keys of the keyboard are a command.
  • for example, to move, we use the keys h,j,k,l . touches de déplacement vi 
  • to insert text use for example the key "i" for "insert", "a for append", "R" for replace
  • when you have finished editing the text press the "esc" key to exit the editing mode.
  • under vi, it is necessary to exit the editing mode to change the line: you have to switch back to command mode to make a move Fortunately under vim this constraint is removed.
  • for all orders see . vimaz 
  • there is also a . mode ligne de commande vi  which is obtained by typing the " key:" and allows to launch global commands on a line selection of the file.
  • to conclude this introduction, under a rustic, even archaic approach, vi is a unique editor in its conception, a type of editor of its own on its own. It is this initial design that makes vim recognized as the most powerful editor available.

    3.Getting started with vim

    You have just entered the vim editor.
    You can use the arrow keys (arrows and pagination) to move around in this text.
    The most important thing to know that makes vim (vi) unique is the fact that vim (vi) is a multimode editor.
    The major feature of the vim editor (vi) is to operate primarily in the . mode commande vi  We can say that there are two kinds of editors those that work by default and mainly in an editing mode (we type on the keyboard and it writes text) and those that work by default and mainly in a command mode (we type on the keyboard and it does things).
    In the first one there are all editors except vi and vim (emacs, ultraedit, notepad, ...getting started with vim) in the second one there are vi and vim.
    vim is the historical extension, the development of the original vi of the unix system which has not evolved except by vim.
    The modes of vim are:

  • the . mode commande vi : this is the default mode of vim, in this mode the keys of the keyboard are not used to enter characters but are commands.
    These commands are, it seems surprising at first sight, very ergonomic, see . vimaz  Indeed, they are easily memorized if you look for the English word at the origin of the choice of the letter.
    If we compare the ergonomics of WORD, linked to the function keys with the combination of the control keys SHIFT,CTRL,ALT, we can see right away that it will be very difficult to become an experienced WORD user, whereas becoming an experienced vim user may require an effort of adaptation at the beginning, but then becomes quite easy, because there is an underlying order in the nomenclature of commands.
  • the . mode ligne de commande vi : if you type the character ":" we go to the vim command line where we can type commands.
    To exit this mode you can do so by . ENTER  which allows to validate the entered order, either by . ESCAPE  which exits command mode without executing anything.
  • the . mode de saisie vi : this is the mode that allows you to enter the text.
    This mode is present in all editors.
    To enter this mode, you must enter a command . commande de modification vi  which allows you to type, for example, by pressing the "i" key, you enter the insertion mode.
    the entry of . ESCAPE  allows you to exit this mode and return to the basic mode on . mode commande vi .

    4.Input mode vi

    In vim the input mode is identical to an editor like notepad.
    You enter this mode from the . mode commande vi  by entering the following commands:

  • "a" to add text (adds after the current character)
  • "i" for text insertion (inserts just before the current character)
  • "R" for text replacement (replaces the text from the current character)
  • "o" to open a new line ( "O" to open it above the current)
  • "s" for subsitute (equivalent to "R").
    You can enter text with the usual keys, move with the arrow keys, use the paging keys, change the mode "insert" "replace", delete a character with . Suppr  and use the menu commands to save changes, go out and search, select with the mouse, use copy and paste.
    We can therefore consider that vim is a superset of notepad.
    So there is no reason to continue using notepad if you have vim.
    Exit the input mode with the escape key . ESCAPE  .

    5.Command line mode vi

    These are in fact the commands of the "teletype" editor, the precursor of vi, the "ed" editor..
    When computer input could be viewed on a screen rather than on a teletype, "ed" evolved into "vi" (which is an abbreviation of "visual").
    This means that "vi" contains the genes of "ed".
    ed by the way gave birth to the command "sed" which could mean "stream ed" for example.
    This mode is accessed by entering the character ".:" from . mode commande vi .
    Under vim, it is possible to retrieve commands already entered by navigating with the up and down arrows.
    You can even enter the beginning of a command and then use the arrows to select only the commands corresponding to the prefix entered.

    vim
    ordernote_en action  
    :b nn    rend courant le buffer (fichier) identified under the number "nn", for the list of buffers see command :ls  
    :e file    edit the file whose path is entered  
    :g/expr/    global : selects the lines that contain the regular expression "exp" and :  
    :g/expr/l    - list the lines  
    :g/expr/d    - destroys the lines  
    :g/expr/s///    - applies the substitution command "s" to the selected lines  
    :ls    lists the buffers already edited during the session and displays their number (see order :b nn)  
    :n    next - outputs the next buffer if multiple files were requested  
    :q    leave the editor ":q!" force output without saving  
    :r    rewind - returns to the first buffer requested when opening vi or when ordering ":n file"  
    :s    . substitution de chaine   
    :w    saves the current file ":w!" force rewriting in case of warning  
    :x    eXit saves the current buffer and exits vi  
    :map_en    creates a new command for the . mode commande vi  by command combination of this mode  
    !    . shell vi allows interaction with the external environment of the program vi, offers unlimited possibilities  
    / or :/    search for a . expression régulière  (targets are displayed in yellow) the "n" command allows you to navigate the search  
    specific
    :gf    go file, opens the file whose path is under the cursor  
    :sp    split ,splits the workspace in two to open a second buffer switch by ctrl-W w  
    :tabnew    added a workspace tab  

    5.1.Tip command line mode vi

  • the exclamation mark character "!" indicates :

    5.2.Shell vi

    The use of the " character!" in different contexts allows to launch system commands.

    On the command line vi for example type ":!dir " to open a shell and run the dir command.
    or in . mode commande vi  , "!" followed by a move command (ne would be that ENTER) displays a prompt on which you can enter a command system. The buffer selected by the move is sent to the standard input of the (stdin) and the standard output of the (stdout) is retrieved and places the result in the delimited buffer by moving (the old buffer is lost).

    We can launch a SQL query with the isql command, transform a text with a perl script, write C programs to create new editing commands, ... The possibilities are endless.

    5.3.Map cmd

    the order :map allows you to create a new command.
    For example the command :"map ² j.
    " creates the command "²" which finds the last command on the next line.

    5.3.1.Example command map analogy vim mq
    I can't help but make an amusing analogy here with the <mécanique_quantique_en_nweb>(fr.wikipedia.org/wiki/M%C30X0P+09canique_quantique) and the Hamiltonian H operator
    responsible for the evolution over time. This operator can be seen as the combination of the operators of creation and destruction of energy states (or particles in the quantum field theory).
    The energies are often H = potential energy kinetic energy = a² b² and with the complex factorization or i²=-1 H = (a ib)(a-ib) = C * D, C and D being the creation and destruction operators.

    If we consider a line of text in vim and the "x" command to delete a character (destruction),
    the default buffer content insertion command "p" (creation)
    and their combination "xp" in a command ":map
    we will have created the new command ": the evolution.
    But here the space is symbolized by a line of underscore and for example the character o as a particle :
    by applying the command ".

    You can also create the inverse operator by ":map

    __________________________________o___________________________________________________________________

    - execute the macros by placing yourself on the "o" (the quantum particle :-quantum mechanics nweb)

    - to compile the macros without copying them to the command line you can use the macro [xlm_en] by placing yourself on the line of the macro.
    map
    map
    - A trajectory can be defined by
    map #H
    - A cyclical trajectory (oscillation) by the recursive macro:
    map #S #H#S
    - Attention this resursive macro can only be interrupted by CTRL-C
    you can see that when you stop the macro you leave the system with the destroyed particle, the "o" has disappeared (it has returned to the quantum vacuum) or created, the "o" is present but probably elsewhere than in its initial position.
    This tells us that when a macro is interrupted it is done on a "atomic" command and not on a macro called by a macro.
    I think this example shows the dynamics of macros very well.
    In Vim there is another way to record a command sequence.
    This is the "q" command that allows you to record the sequence of commands that will be typed on the keyboard (entering another "q" stops the registration) and the "@" command which allows to execute these commands.
    Commands are stored in buffers named by an alphanumeric character that must be specified in conjunction with the commands "q" and "@".
    For example q03dwj stores in buffer 0 the command 3dwjbbb which deletes 3 words and moves to the next line by going back 3 words while executing this sequence.
    the @0 command allows to repeat the sequence.

    5.4.Chain substitution

    The command ":s/chaine1/chaine2/" of chain1 by chain2 substitution offers very powerful possibilities: If you want to make changes to the "/" character you must use "\\/" to specify this character or use another string delimiter character ":s.
    chaine1.
    channel2.
    "

    :s/toto/titi/  replace "toto" with "titi" on the line  
    :s/toto/titi/g  replace all "toto" with "titi" on the line  
    :1,$/toto/titi/g  replace all "toto" with "titi" on the whole file  
    :., 10/toto/titi/g  replace all "toto" with "titi" starting from the current line on ten lines  
    :g/tata/s/to/titi/g  replace all "toto" with "titi" on all lines that contain the string "tata"  
    :'a,'bg/tata/s/to/titi/greplaces all "toto" with "titi" on all lines that contain the string "tata" between the lines stored by the indexes "a" and "b" 

    5.4.1.. expression régulière : regular expression

    Regular expressions allow you to specify complex search strings.

    The " character." is used to specify any character.
    The "*" character is used to specify any sequence of characters.
    The ""^ character is used to specify the beginning of the line The "$" character is used to specify the end of the line

    A block "" is used to specify a possible character set in the regular expression.
    For example :

  • a-zA-Z_en designates a single upper or lower case alphabetical character.
  • 0-9_en means any number
  • 1-2_en0-9_en0-9_en0-9_en designates a form of year from "1000" "2999"

    For example "toto.*titi" designates the portion of lines that start with toto and end with titi.
    For example ".*this.*titi.*" designates the lines that contain both toto and titi in this order.
    For example "^toto.*titi$" designates lines that start with toto and end with titi.

    |" (backslash-pipe) so "toto.|titi.*toto" allows for example in a g//s// command to make the modification only on the lines corresponding to the criterion.
    We are almost at the level of a text mode requester.

    5.4.1.1.Under fields

    It is possible to break down a line or a portion of a line into sub-fields in a regular expression in the command ":s".

  • Subfields are defined in the target string by "backslash" parentheses. :(.)(..)
  • The subfields can then be used in the replacement string by \\1 \\2 \\3 etc...

    Example: target chain: "Beautiful marquise your beautiful eyes make me die of love" (B.)(v.)()()()" which is called \\1 \\2 \\3 \\4 \\5

    The command ":(B.)(v.)()()()/\\5 \\4\\3/" with all permutations of subfields generate less beautiful forms of proses of the first sentence: of love your beautiful eyes die me Beautiful marquise

    6.Command mode vi

    It is this mode that explains the power of vim.
    Each key is a command.

    There are major types of commands:

  • . commande de déplacement vi 
  • . commande de modification vi 
  • . commande de recherche vi 

    You can combine commands of different types: change*move, for example :

    Macro commands can be created by combining basic commands with the command . map cmd  from . mode ligne de commande vi .
    Note: The commands of this mode can be considered as operators that can be combined.
    This operator combination mechanism (can we talk about operator algebra?) is very powerful.
    Operators can be associated with cardinalities that indicate how many times the command must be repeated.
    for example "5dw" deletes the next five words.
    The macro-commands can be combined and even recursively (the stop of the recursion must be foreseen: triggered by the impossibility of carrying out an order)
  • A new mode available only in vim is text selection with "v"

    6.1.Displacement command vi

    The move commands, see type "d" in the list . vimaz  can be combined with other change commands to form a new change command.
    The arrow keys are of course operational and work in conjunction with the SHIFT, CTRL.

    6.2.Change command vi

    The modification commands, see type "m" in the list . vimaz 

    6.3.Search command vi

    Search commands, see type "r" in the list . vimaz 

    6.4.Commande de gestion de buffer vi

    The buffer commands, see type "g" in the list . vimaz 

    6.5.. vimaz :I came from A to Z

    The vim alphabet.
    When you are in . mode commande vi  each letter is a command :

    columndescription  
    . lettre   the letter of the order. It can be a single letter, doubled, followed by another letter, followed again by the type of a command
     Doubled letter mentioned by ² which is not used by vim  
     Letter followed by any other letter this last case is indicated by the presence of a "underscore" for example f_  
     Followed by the type of a command "d" for displacement for example, in this case all displacement commands are operative  
    . commande type    order type : d Move, m Edit, r Search, g Buffer management  
    . note vim   utility or power of the control mentioned by a series of more ... or less  
    . mnemo   origin of the use of the letter for the command, mnemonic means, in English necessarily  

    . lettre  . type cmd  . mnemo  . note vim  action  
           . caractère non alphabétique vi   
    .  m      repeats the last modification command  
    ~  m      upper-case-lower-case reverse. To be used with "g" : g~d  
    ^  d      goes to the beginning of the line  
    $  d      goes to the end of the line  
    *  r      vim searches for the word under the cursor : all targets are colored yellow . See here g*  
    '_  d      "cote" followed by a letter moves the cursor to the line stored by that letter see m_  
             
    "_yd  g      "double dimension" followed by a letter, followed by "y" yank followed by a move command stores the text to be moved in a buffer named by this letter  
    "ayfo  g      example: "afo stores in the buffer "a" from the current character to the next "o" on the current line  
    "byy  g      "byy stores the current line in buffer b  
    "by'b  g      "b'b stores in the buffer b from the current line to the line stored by the index b  
           . caractère alphabétique vi   
    a  m  append    adds text after the current character and switches to . mode de saisie vi   
    A  md  Append    adds text at the end of the line switches to . mode de saisie vi   
    b  d  back    moves the cursor back one word, if . majuscule  the words are just separated by blanks and spaces  
    B  d  Back    idem "b" si . majuscule  words defined by what is separated by blanks and spaces  
    c_  m  change  -  "c" followed by a . commande de déplacement vi  changes the text from the current character to the end of the move  
    C  m  Change  -  changes the text from the current character to the end of the line  
    dd  m  delete    "d" followed by a . commande de déplacement vi  deletes the text from the current character to the end of the move  
    d²  m  delete    "d" doubled deletes the current line  
    D  m  delete    deletes the text from the current character to the end of the line  
    e  d  end    moves the cursor to the end of the current word  
    E  d  End    moves the cursor to the end of the ". mot large " current  
    f_  r  find    "f" followed by a character moves the cursor on the line to the character if it exists  
    F_  r  find    "F" same as "f" but moves to the beginning of the line  
    g²  d  go    moves the cursor to the beginning of the file  
    G  d  go    moves the cursor to the end of the file  
    g*  r  global    (vim) same as "*" but it is not the word but the string that is searched (if it appears in a word)  
    g~d  m  global    inverts lower case letters until you move  
    gf  d  go file    edit the file whose path is under the cursor  
    i  m  insert    insert the characters typed later before the current character toggles to . mode de saisie vi   
    I  md  Insert    insert the characters typed later at the beginning of the line toggles to . mode de saisie vi   
             
    J  m  join    concatenates the current line with the next one (merging of two lines) a blank is inserted between the two lines  
           displacement keyboard : hjkl jk:up down , hl left right (to be used in the macro instead of the arrows) original keyboard without trackpad  
    . touches de déplacement vi 
    h  d      left arrow  
    j  d      up arrow  
    k  d      down arrow  
    l  d      right arrow  
             
    m_  d  memorise    "m" followed by a letter memorizes the next line with this letter as index, the recall is done with the "cote" character: '  
             
    o  m  open    opens or inserts a line after the current line  
    O  m  Open    opens or inserts a line BEFORE the current line  
    p  m  put    inserts the content of the current buffer  
    r_  m  replace    replaces the current character toggle remains in . mode commande vi   
    R  m  Replace    replaces the text from the current position switches to . mode de saisie vi   
    s  m  subsitute    replaces the text from the current position switches to . mode de saisie vi   
    S  m  Subsitute  -  substitutes the current line with the characters typed later and switches to . mode de saisie vi   
    t_  r  to    "t" followed by a character moves the cursor to the line immediately before the character if it exists  
    T_  r  To    "T" same as "t" but moves to the beginning of the line (usefulness in combination with double-rated control  
    u  m  undo    "undo" undoes the last modification. undo cancels all changes since the last edited file change . "redo" ctrl-r is the inverse of undo  
    v  g      (vim) enters the text selection mode (linear view of the text) ctrl-v in rectangular block selection mode (surface vision of the text)  
    w  d  end    moves the cursor to the end of the current word  
    W  d  End    moves the cursor to the end of the ". mot large " current  
    x  m      removes the letter  
    yd  g  yank    "y" followed by a . commande de déplacement vi  stores in the default buffer the text concerned by the move, used in conjunction with the buffers named cf "_  
    z²        "z." or "zz" centers the window on the current line  
    z. ENTER         positions the current line at the top of the window  
    z-        positions the current line at the bottom of the window  
           . commandes utilisant la touche CTRL   
    CTRL-R        "redo"^ inverse of "u"" "undo"^  
    CTRL-A        increments the number under the cursor  
    CTRL-X        decrements the number under the cursor  

    7.Vim method

    How to move easily in a file you are working on.

  • Use of anchor points ("m a", "m b", ...
    "m z").
    Standard anchor points must be defined (letter of the alphabet) that will be used systematically for a specific purpose.
    So we define a kind of personal nomenclature of anchor points.
    The first anchor point should be the main editing point, the others are the other points where we go to get the information we are retrieving for editing.
    For example similar code template.
  • Use of the search string (input or command *, alt-*) and commands n and N (next).
  • with Utags :

    7.1.Broad word

    a word defined by what is separated by blanks and spaces only.
    example: "since Monday morning" is a single "broad word"

    7.2.Upper case

    In vi the alteration of a key by shift (upper case) alters the command corresponding to the lowercase.
    The alteration can be of several types but the resulting command is close to the initial.
    possible alteration:

  • change of direction - action performed at the end or beginning of a line