***********************************************************************



                          ##        ##      ##   #########
              ##  #       ##        ##      ##  ##
             #    #       ##        ##      ##  ##
             #    #       ##        ##      ##  ##
           #####  ####    ##        ##      ##  ##    ####
             #    #   #   ##        ##      ##  ##      ##
             #    #   #   ##        ##      ##  ##      ##
             #    #   #    #######   ########    ########

~           FH Hagenberg    Linux      User       Group               ~

~                December 2019 :: <http://fhLUG.at>                   ~




CC BY-SA 4.0                                        Daniel KNITTL-FRANK
***********************************************************************

# Vim -- A christmas special


```
         _   _
        ( ) ( ) _
        | | | |(_)  ___ ___   ______   ___ ___     _ _   ___
        | | | || |/' _ ` _ `\(______)/' _ ` _ `\ /'_` )/',__)
        | \_/ || || ( ) ( ) |        | ( ) ( ) |( (_| |\__, \
        `\___/'(_)(_) (_) (_)        (_) (_) (_)`\__,_)(____/

                ___                                   _
               (  _`\                       _        (_ )
               | (_(_) _ _      __     ___ (_)   _ _  | |
               `\__ \ ( '_`\  /'__`\ /'___)| | /'_` ) | |
               ( )_) || (_) )(  ___/( (___ | |( (_| | | |
               `\____)| ,__/'`\____)`\____)(_)`\__,_)(___)
                      | |
                      (_)
```


# First Things First

* How to start vim? Type `vim`

* How to exit Vim? Type `:wq<Enter>` to save and exit,
  `:q!` to exit without save

  ```
        _    _ _
       (_) /'_` )
        _ ( (_) |
       (_)`\__, |
              | |
              (_)
  ```

  (Protip: `ZZ` and `ZQ` in normal mode)


# History of Vim

Go read it up on Wikipedia:

<https://en.wikipedia.org/wiki/Vim_(text_editor)>


## Testimonials

> Coming with a very GUI mindset from Windows, I always thought of people
> using Vi as some kind of outer space alien in human clothes.  Once I
> tried I really got addicted by its power and now I found myself typing
> Vim keypresses in the oddest places! That's why I would like to see Vim
> embedded in every application which deals with text editing.
> --- (José Fonseca)


# Mode Based Editing

* Vim supports different modes (normal, `i`nsert, `v`isual, etc.)
* `<Esc>` to exit any mode and return to normal mode


# Getting Started

Vim comes with `vimtutor` binary which provides an "interactive" tutorial.


# Basic Motions

         ^
         k         Hint:  The h key is at the left and moves left.
    < h     l >           The l key is at the right and moves right.
         j                The j key looks like a down arrow.
         v

*But* you can almost always simply use the arrow keys to navigate


# Basic Inserting

To enter insert mode:

* `i`: `i`nsert
* `a`: `a`ppend
* `o`: starts a new line


# When Something Goes Wrong

* `u`:     `u`ndo
* `<C-R>`: `r`edo

(`g-`/`g+` for walking through _undo-tree_)


# More Motions

* `w`:      `w`ord forward
* `b`:      word `b`ackward
* `e`:      `e`nd of word
* `)`, `(`: sentence forward, backward
* `}`, `{`: paragraph forward, backward
* `%`:      matching item, e.g. `()` `[]` `{}`
* `^`, `$`: start of text and end of line
* `1G`:     go to line `1`


# Cool Selection Tricks

* `<C-V>`:   block/column selection
* `V`:       line-based select
* `o`:       jump to start/end of selection
* `gv`:      re-select previous selection


# Basic Editing

* `x`: delete character (cross out)
* `dd`: `d`elete line
* `cc`: `c`hange line
* `s`: `s`ubstitute character under cursor
* `J`: `J`oin lines


# Copy & Paste

* `y`:        copy (`y`ank)
* `d` or `x`: cut (`d`elete)
* `p`:        `p`aste


# Combining Keys

Compose single keys to build powerful actions:

* `2.`: repeat last action `2` times

* `yw`:  `y`ank to next `w`ord
* `y3w`: `y`ank next `3` `w`ords

* `das`: `d`elete `a` `s`entence
* `ciw`: `c`hange `i`nner `w`ord

* `gUiw`: `U`pper-case `i`nner `w`ord


# Text Object Selection

* `viw`: `i`nner `w`ord
* `vis`: `i`nner `s`entence
* `vib`: `i`nner `b`lock, e.g. `()` `[]`
* `vi"`: `i`nside double quotes
* `vap`: `a`round `p`aragraph
* `va]`: `a`round brackets
* `vat`: `a`round `t`ag
* ... plus many more


# Searching

* `/regex`, `?regex`: search regular expression forward, backward
* `*`, `#`: forward search for word under cursor, backward search
* `n`, `N`: next match, previous match

* `fc`, `Fc`: next/previous character 'c' (inclusive)
* `tc`, `Tc`: next/previous character 'c' (exclusive)
* `;`, `,`:   repeat forward, repeat backward

* `:%s/pattern/replace/g`: *s*ubstitute text file-wide


# Command Line Mode ("ex" mode)

`:[range][command]`

Useful commands:

* `:make`:                execute Makefile
* `:!cmd`:                run external command
* `:%!cmd`:               filter and replace content by command
* `:1,10w file`:          *w*rite first 10 lines to file
* `:r!date`:              *r*ead current date and time into file
* `:g/pattern/command`:   perform *g*lobal command
* `:v/pattern/command`:   perform in*v*erted global command
  * e.g. `:g/^$/d`:       delete empty lines
  * e.g. `:g/^#/norm A #` add " #" after each Markdown header line


# Quick Filter

Need to filter current line through external command?

* `:.!cmd`: filter current line through command

(Protip: Type `!!`)


# Text layout

* `=`:       auto-indent text/source code
* `>`, `<`:  indent, unindent
* `gq`:      format text
* `:center`: center text
* `:left`:   left align text
* `:right`:  right aliign text


# Autocompletion

* `<C-N>`:       autocomplete next
* `<C-P>`:       autocomplete previous
* `<C-X> <C-L>`: autocomplete line
* `<C-X> <C-F>`: autocomplete file path
* `<C-X> <C-N>`: autocomplete with context
* `<C-X> <C-O>`: omni-completion


# Registers

* Yank and delete fill numbered registers 0 to 9
* 26 named registers a to z
* Access registers with `"r`

* `"ry3w`: `y`ank next `3` `w`ords into register "`r`"
* `"r2p`:  `p`aste `2` times from register "`r`"


# Macros

Registers can be used to store and replay macros

* `qr`: start recording into register "r" and `q` when finished
* `@r`: execute macro from register "r"
* `@@`: re-run last macro
* `@:`: re-run last "ex" command

E.g. macro to capitalize first word in sentence,
     then jump to next sentence:

    ()guw~)


# Marks

Store bookmarks for text positions and use them as motion or ranges

* `mr`:   `m`ark position and store as `r`
* `` `r ``: jump to mark `r`
* `'r`:   jump to line of mark `r`

* Lowercase marks are valid within a single file
* Uppercase marks are valid between files


# Window Management

* `<C-W>s`, `<C-W>v`: create `w`indow `s`plit, split `w`indow `v`ertically
* `<C-W>w`:           cycle windows
* `<C-W>j`:           activate window down
* `<C-W>c`:           `c`lose window


# Tab Management

* `:tabedit path`: edit `path` in new tab
* `:tabclose`:     close current tab
* `gt`, `gT`:      next tab, previous tab


# Files

* `:e path`: *e*dit `path` in current buffer
* `gf`:      `g`o to `f`ile under cursor (or visual selection)
* `<C-W>gf`: open file under cursor in new tab


# Useful Config Options

```
set number               " show line numbers

set ignorecase smartcase " ignore case if pattern lower case

set incsearch            " incremental search
set hlsearch             " highlight all matches

" show non-printing characters (make sure your terminal supports UTF8!)
set list listchars=tab:→ ,nbsp:␣,trail:•,space:⋅
```

Can be persisted in `~/.vimrc`. If you want to have a look at my settings,
check out <https://github.com/knittl/.files/blob/master/.vimrc>


# Plugins

* Possibility to write and load plugins written in _VimL_
* Stored in `~/.vim/plugin/`


## Plugin Manager: Plug

* Single-file plugin manager
* <https://github.com/junegunn/vim-plug>

Installing:

```sh
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://github.com/junegunn/vim-plug/raw/master/plug.vim
```

Then add to your `~/.vimrc`:

```
call plug#begin()
  Plug 'git repository url'
call plug#end()
```


# Cool Stuff

Using Vim as hex editor:

```
:%!xxd
:%!xxd -r
```

# Useful Plugins (Incomplete List)

* mattn/emmet-vim:         HTML and CSS editing on steroids
* tpope/vim-surround:      Clever way of changing surrounding elements
* tpope/vim-fugitive:      Never leave Vim, when you have to do Git stuff
* plasticboy/vim-markdown: Better visual editing of markdown files
* junegunn/vim-easy-align: Align text and tables


# Using Vim in Other Applications

* [vscodevim](https://marketplace.visualstudio.com/items?itemName=vscodevim.vim)  A Vim plugin for Visual Studio Code
* [ideavim](https://github.com/JetBrains/ideavim)    A Vim plugin for IntelliJ
* [vimperator](http://vimperator.org/) A Vim plugin for Firefox


# Sources and Further Reading

* `man vim`
* Vim's `:help`
* <https://en.wikipedia.org/wiki/Vim_(text_editor)>
* <https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim>
* <https://vim-adventures.com/>
* <https://vi.stackexchange.com>
* <https://fhLUG.at/2014/01/vim-workshop/>
* <https://vimawesome.com>
* <https://github.com/akrawchyk/awesome-vim>


# Contact

* [fhLUG.at](https://fhLUG.at)
* [freenode#fhlug](https://fhLUG.at/irc)


