File: viref.txt

# resources
Repository for vim plugins - https://vimawesome.com
Vim screencasts - http://vimcasts.org
Repository for vim color schemes - http://vimcolors.com
Vim tips repository - https://vim.fandom.com/wiki/Vim_Tips_Wiki
Effective text editing (with vim) - https://moolenaar.net/habits.html
Using template files in vim - https://shapeshed.com/vim-templates/

vim --version # info on .vimrc file

Read: vimtutor, vim man page,
/usr/share/vim/vim74/doc/^.txt  # The Vim documentation files
Use ":help doc-file-list" to get the complete list.

# info on autocmd
:help autocmd
:h autocmd

The default shift is eight spaces (right or left). This default can be changed
with a command such as:
:set shiftwidth=4

vi provides automatic indentation control. To use it, issue the command:
:set autoindent

/usr/share/vim/vimrc    # system wide Vim initializations
~/.vimrc                # your personal Vim initializations

# Vi options  [~/.vimrc]
echo set number >> ~/.vimrc       # shows line numbers when vi opens a file
echo set nonumber >> ~/.vimrc     # don't show line numbers
echo syntax on >> ~/.vimrc        # turns on syntax highlighting
echo set tabstop=4 >> ~/.vimrc    # sets the tab size to 4 spaces (default is 8)
echo set autoindent >> ~/.vimrc   # carries over previous indent to the next line
echo set noautoindent >> ~/.vimrc # turn off autoindent
echo set showmatch >> ~/.vimrc    # show matching set of parentheses as they are typed
echo set noshowmatch >> ~/.vimrc  # turn off showmatch
echo set showmode >> ~/.vimrc     # display mode on last line of screen
echo set noshowmode >> ~/.vimrc   # turn off showmode

[ctrl] G # display the name of the current file and the cursor position in it

:s/^/#
:1,4s/^/#
:1.4s/^#
:1,4s/^/#/g       # s - substitute
:1,4s/^#/g        # uncomment
:%s/^/#/g         # comment all the lines in the file

# search and replace
:%s/apple/pear/g # replaces every occurances of apple with pear
:%s/apple/pear/gc # c can be added to the end to ask for confirmation

ctrl+w and v for spliting screen vertically

# editing multiple files at a time
vi file1 file2 file3
(First, vi will open file1. To switch to the next file (file2), we need to use
the :n command. When we want to return to the previous file, :N will do the
job.) :buffers  # the buffers command will show a list of the file currently
being edited

Basic & Essential Vim Commands:
i - Enter insert mode
Esc - Enter command mode
x - Delete a character
X - Delete character in backspace mode
u - Undo changes
Ctrl+r - Redo changes
yy - Copy a line
dd - Delete a line
p - Paste the content of the buffer
/<search_term> - Search and then cycle through matches with n and N
[[ or gg - Move to the beginning of a file
]] or G -  Move to the end of a file
:%s/foo/bar/gci - Search and replace all occurrences with confirmation
Esc + :w - Save changes
Esc + :wq - Save and quit Vim
Esc + :q! - Force quit Vim discarding all changes

vi -d file1 file2 # Start in diff mode. There should be two, three or four file
                    name arguments. Vim will open all the files and show
                    differences between them. Works like vimdiff(1).

vi -h # Give a bit of help about the command line arguments and options.
        After this Vim exits.

vi -l # Lisp mode. Sets the 'lisp' and 'showmatch' options on.

vi -m # Modifying files is disabled. Resets the 'write' option.
        You can still modify the buffer, but writing a file is not possible.

vi -M # Modifications not allowed. The 'modifiable' and 'write' options will
        be unset, so that changes are not allowed and files can not be written.
        Note that these options can be set to enable making modifications.

vim -v # Start Vim in Vi mode, just like the executable was called "vi". This
         only has effect when the executable is called "ex".

## Deleting characters, words and lines
x               delete the character at the cursor location
dw              delete the current word
D               delete the remainder of the line after the cursor
dd              delete the current line

## Looking for strings
/string # find the first occurrence of string after the cursor
?string # find the first occurrence of string before the cursor
n       # find the next occurrence in the last search


## Replacing strings
n,ps/str1/str2/g between line numbers n and p, substitute all (g:global) occurrences
                 of str1 by str2
1,$s/str1/str2/g in the whole file ($: last line), substitute all occurrences of
                 str1 by str2

Note: http://vimsheet.com/advanced.html

# all tabs into spaces
:ret
:se et (:set expandtab) # to make sure that any new lines will not use literal tabs

# To copy lines 1 through 20 of the current file to your current cursor position, use
this command:
:1,20 co .

# cit
delete the tag

# read contents from a file
:r! sed -n 147,227p /path/to/file

# convert tabs to spaces
:set list                               # list tabs
:set expandtab                          # use expand tab to convert new tabs to spaces
:set tabstop=4 shiftwidth=8 expandtab   # use expand tab to convert new tabs to spaces
:retab                                  # convert existing tabs to spaces
:set nolist                             # normal viewing

# search and replace
:%s/text1/text2
.       for the current position
.+10    for 10 lines down
%       for the whole file
'<,'>   for the lines of visually selected block
'a,'b   from mark a to mark b
.,$     from the current line to the end of the file

# tabs
:o <filename>                   # open files in vi text editor
:e <directoryname>/<filename>   # open files in vi text editor using tab
:tabedit <filename>             # open files in tab mode
gt  # forward tab
gT  # backward tab
2gt # second tab
vim -p file1.txt file2.txt      # opening multiple files in vim in tab mode

# saved sessions
: vsplit    # vertical split
: split     # horizontal split
: mksession file.vim # save a session
$ vim -S file.vim   # open the saved session

# spell check (~/.vim/spell/)
:set spell
:set spelllang=en_us,de_de,fr_ch    # spell check with english, german, french

# file type
:set filetype=c

# let vim do typing
to repeat previous typing ----> press . (normal mode)
to complete any previous text ----> press ctrl + p (insert mode)
ctrl + p and ctrl + n loops the previous text in backward and forward mode

# file explore
:Explore
:Hex
:Sex
:Lex
:Lex!

# shift indent text (visual block mode)
ctrl + V ----------> visual mode
then press < for towards left and > for towards right

# recall commands from history
Vim records the commands that we enter in command-line mode and provides two
ways of recalling them: scrolling through past command-lines with the cursor
keys or dialing up the command-line window. Let's switch to command-line mode
by pressing : key. Leave the prompt empty; then press the <UP> arrow key. The
command-line should be populated with the most recent command that we executed.
We can use the <UP> key again to go further back through our command history or
use the <DOWN> key.

# vim to shell
:sh

# shell within vim (Ctrl+W Ctrl+W toggle between vim and shell)
:term
:terminal

# options available inside vim
:options
/tabstop    # to get info regarding tabstop
:q          # to quit

# tabpage
:h tabpage

# vim environment variables
:echo $VIM
:echo $VIMRUNTIME

# compile python (% - current file)
:!python %

# highlight white spaces
/ $

# move to the last edit
`.

# source the edited .vimrc file
:so%

# set/change the color scheme
:colorscheme <press tab to toggle>

# edit commands
Text object                     Change          Delete          Copy(yank)
One word                        cw              dw              yw
Two words, whitespace separted  2cw or c2w      2dw or d2w      2yw or y2w
Three words back                3cb or c3b      3db or d3b      3yb or y3b
One line                        cc              dd              yy
To end of line                  c$ or C         d$ or D         y$
To beginning of line            c0              d0              y0
Single character                r               x or X          yl or yh
Five characters                 5s              5x              5yl

# movement
To first character of next line         +
To first character of previous line     -
To end of word                          e or E
Forward by word                         w or W
Backward by word                        b or B
To end of line                          $
To beginning of line                    0
To a particular line                    G

There are vi commands to scroll forward and backward through the file by full
and half screens:
^F  -   scroll forward one screen
^B  -   scroll backward one screen
^D  -   scroll forward a half screen (down)
^U  -   scroll backward a half screen (up)

# read manual page
pressing shift+k in keyword opens the manual page of that keyword

# <leader>
By default <leader> is \, backslash. You can check it with:
:echo mapleader
If this gives you an E121: Undefined variable: mapleader, it means it's set to
the default of \.
You can easily remap it. To mapped it to the space-bar:
:let mapleader = "\<Space>"

# open terminal inside vim
:vert term
ctrl-w ctrl-w # move focus to the next window

# check file <tab> / <space>
$ cat -etv makefile
[it show line starting by ^I if <tab> is given to that line and it end the line by $]

# math calculations
"Ctrl+r" then type =7*7 then press Enter

# digraph
:digraph
:help digraph

In insert mode, you can insert special characters by Ctrl-k followed by a two-character
lookup code. For example, Ctrl-k ?2 will insert the approximately equal symbol ≈.

• oo • (bullet)
• Db ◆ (diamond bullet)
• '% ϴ (Greek theta)
• Pd £ (British pound symbols)
• Co © (copyright symbol)
• Rg ® (registered trademark symbol)
• Eu € (euro symbol)
• -1 ‐ (hyphen)
• -N – (en dash)
• -M — (em dash)
