# Vim

Vim è un editor di testo molto potente, che può essere ampliato ulteriormente tramite **plugin** vari.

Prima, però, ecco una configurazione base che aggiunge varie funzionalità:

```vim
" IMPOSTAZIONI ----------------------------------------------------------- {{{

colorscheme catppuccin_mocha
set spell spelllang=it,en
syntax on 			  			      " Enable type file detection. Vim will be able to try to detect the type of file in use.
filetype off 						  " Enable plugins and load plugin for the detected file type.
set nocompatible              		  " be iMproved, required
filetype plugin indent on   		  " required
set omnifunc=syntaxcomplete           " Enable Omnicomplete features
filetype plugin on 				      " Load an indent file for the detected file type.
filetype indent on
set foldenable
set autoindent                        " Automatically guesses the indentation given the previous one
set hlsearch                          " Automatically highlights search results, to hide them run :noh
set incsearch                         " Searches incrementally as you type.
set ruler                             " Shows the rulers
set showcmd                           " Shows current cursor info
set ignorecase                        " Ignores cases when searching
set noswapfile                        " Disables swap files
set undofile                          " Enable keeping history across sessions, don't forget to mkdir
set undodir=~/.vim/undo/
set nofixeol                          " Do not insert a new line at the end of the file automatically
set backspace=indent,eol,start        " Enable backspace key to delete stuffs properly
set termguicolors
set number						      " Add numbers to each line on the left-hand side.
set relativenumber
set clipboard=unnamedplus
set cursorline						  " Highlight cursor line underneath the cursor horizontally.
set nobackup    					  " Do not save backup files.
set nowrap						      " Do not wrap lines. Allow long lines to extend as far as the line goes.
set incsearch					      " While searching though a file incrementally highlight matching characters as you type.
set ignorecase						  " Ignore capital letters during search.
set smartcase						  " This will allow you to search specifically for capital letters.
set showmode						  " Show the mode you are on the last line.
set showmatch						  " Show matching words during a search.
set hlsearch						  " Use highlighting when doing a search.
set wildmenu						  " Enable auto completion menu after pressing TAB.
set wildmode=list:longest			  " Make wildmenu behave like similar to Bash completion.
set foldenable
set foldmethod=indent
set ttyfast						      " Speed up scrolling in Vim
set encoding=UTF-8
set nobackup                          " Neoclide
set nowritebackup
set updatetime=300
set signcolumn=yes
" }}}

```

## Plugin Manager

Per poter installare dei plugin è necessario installare prima un plugin-manager.

### Vundle

```bash
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
```

Dopo aver clonato Vundle sulla propria macchina, inserire le seguenti righe di testo nel file `.vimrc`

```vimrc
" PLUGINS ---------------------------------------------------------------- {{{

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
"
" All of your Plugins must be added before the following line
" 

call vundle#end()            " required

" }}}
```

## Installazione plugin

Installare un plugin è molto semplice, basta aggiungerlo nel file `.vimrc`, come da codice sottostante:

```vim
" PLUGINS ---------------------------------------------------------------- {{{

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
"
" All of your Plugins must be added before the following line
" 
Plugin 'neoclide/coc.nvim', {'branch': 'release'}
Plugin 'itchyny/vim-cursorword'
Plugin 'preservim/nerdtree'
Plugin 'iamcco/markdown-preview.nvim'
Plugin 'alvan/vim-closetag'
Plugin 'mattn/emmet-vim'
Plugin 'jiangmiao/auto-pairs'
Plugin 'ryanoasis/vim-devicons'
Plugin 'frazrepo/vim-rainbow'
Plugin 'mg979/vim-visual-multi', {'branch': 'master'}
Plugin 'itchyny/lightline.vim'
Plugin 'voldikss/vim-floaterm'
Bundle 'https://github.com/prashanthellina/follow-markdown-links'
Plugin 'mracos/mermaid.vim'
Plugin 'mzlogin/vim-markdown-toc'
Plugin 'bbrtj/vim-vorg-md'
Plugin 'sheerun/vim-polyglot'

call vundle#end()            " required
```

Una volta salvato il file, digitare `:PluginInstall`.

Quando l'installazione è completata, è buona norma cancellare la cache del buffer digitando il comando `:bdelete`.

È possibile ricercare plugin nel Web oppure tramite il comando `:PluginSearch`. Per aggiornare l'elenco locale dei plugin dal sito vimscripts digitare:
`:PluginSearch!`

### Alcuni plugin utili

- [https://github.com/preservim/nerdtree](https://github.com/preservim/nerdtree)
- [https://github.com/VundleVim/Vundle.vim](https://github.com/VundleVim/Vundle.vim)
- [https://github.com/sheerun/vim-polyglot](https://github.com/sheerun/vim-polyglot)
- [https://github.com/alvan/vim-closetag](https://github.com/alvan/vim-closetag)
- [https://github.com/mattn/emmet-vim](https://github.com/mattn/emmet-vim)
- [https://github.com/ryanoasis/vim-devicons](https://github.com/ryanoasis/vim-devicons)
- [https://github.com/nopecha-ai/nopecha-extension](https://github.com/nopecha-ai/nopecha-extension)
- [https://github.com/thameera/vimv](https://github.com/thameera/vimv)
- [https://github.com/voldikss/vim-floaterm#requirements](https://github.com/voldikss/vim-floaterm#requirements)
- [https://github.com/akrawchyk/awesome-vim](https://github.com/akrawchyk/awesome-vim)
- [https://github.com/neoclide/coc.nvim](https://github.com/neoclide/coc.nvim)
- [https://github.com/fannheyward/coc-marketplace](https://github.com/fannheyward/coc-marketplace)
- [https://github.com/OmniSharp/omnisharp-vim](https://github.com/OmniSharp/omnisharp-vim)
- [https://github.com/ncm2/ncm2](https://github.com/ncm2/ncm2)
- [https://github.com/prashanthellina/follow-markdown-links](https://github.com/prashanthellina/follow-markdown-links)
- [https://github.com/itchyny/calendar.vim](https://github.com/itchyny/calendar.vim)
- [https://github.com/mracos/mermaid.vim](https://github.com/mracos/mermaid.vim)
- [https://github.com/bbrtj/vim-vorg-md](https://github.com/bbrtj/vim-vorg-md)
- [https://github.com/rderik/vim-markdown-toc](https://github.com/rderik/vim-markdown-toc)
- [https://github.com/tpope/vim-commentary](https://github.com/tpope/vim-commentary)
- [https://clay-atlas.com/us/blog/2021/07/04/vim-en-you-complete-me-plugin-auto-code/](https://clay-atlas.com/us/blog/2021/07/04/vim-en-you-complete-me-plugin-auto-code/)
- [https://github.com/ycm-core/YouCompleteMe](https://github.com/ycm-core/YouCompleteMe)
- [https://github.com/primitivorm/vim-predictive](https://github.com/primitivorm/vim-predictive)
- [https://github.com/itchyny/vim-cursorword](https://github.com/itchyny/vim-cursorword)

Nel caso di `neoclide`, un plugin che abilita l'autocompletamento, da installare i seguenti moduli:

```vim
CocInstall coc-cmake coc-css coc-cssmodules coc-docker coc-emmet coc-flutter coc-highlight coc-html coc-htmlhint coc-html-css-support coc-json coc-ltex coc-markdown-preview-enhanced coc-markdownlint coc-omnisharp coc-prettier coc-pytho
n coc-sh coc-sql coc-vimlsp coc-xml coc-yaml coc-markdown-preview-enhanced coc-webview
```

Quindi eseguire:

```bash
cd ~/.vim/bundle/coc.nvim && git checkout release && yarn install && yarn build
```

![vim](./asset/vim/vim.png)


## Comandi utili

| Comando   | Funzione                                                                                                                                       |
|-----------|------------------------------------------------------------------------------------------------------------------------------------------------|
| `gx`      | Apre il link sotto al cursore nel browser                                                                                                      |
| `:m +/-n` | Sposta la riga sopra o sotto di *n* posizioni                                                                                                  |
| `"+y`     | In Visual Mode, copia il testo selezionato nella clipboard del SO                                                                              |
| `vimv`    | Da terminale, permette di rinominare cartelle e file coi comandi di Vim ([https://github.com/thameera/vimv](https://github.com/thameera/vimv)) |
| `:args`   | Seleziona dei file come argomenti. È possibile utilizzare i caratteri jolly                                                                    |
| `:argdo`  | Permette di eseguire un comando su n files                                                                                                     |


Ad esempio:

```vim
:argdo %s/stringToBeReplaced/replacementString/g | update
```
`update` serve per salvare le modifiche automaticamente.

## Collegamenti

- [https://irian.to/blogs/how-to-search-and-replace-string-across-multiple-files-in-vim/](https://irian.to/blogs/how-to-search-and-replace-string-across-multiple-files-in-vim/)
- [https://linuxhint.com/vim-copy-text-system-clipboard/](https://linuxhint.com/vim-copy-text-system-clipboard/)
- [https://vim.fandom.com/wiki/Accessing_the_system_clipboard](https://vim.fandom.com/wiki/Accessing_the_system_clipboard)
