Introduction générale à Vim

Deux excellentes introduction à Vim.

Essential tricks and settings

Visual cues for coders

Syntax highlighting may be enabled by default by adding the following line to your .vimrc.

syntax on

Depending on the language(s) you're working with, some adjustments may be required to highlight the correct file. Drupal developers may wish to also include the following settings:

if has("autocmd")
  filetype on " Enable filetype detection
  filetype indent on " Enable filetype-specific indenting
  filetype plugin on " Enable filetype-specific plugins
  augroup module
    " Drupal settings
    autocmd BufRead,BufNewFile *.module set filetype=php
    autocmd BufRead,BufNewFile *.install set filetype=php
    autocmd BufRead,BufNewFile *.test set filetype=php
    autocmd BufRead,BufNewFile *.inc set filetype=php
    autocmd BufRead,BufNewFile *.profile set filetype=php
    autocmd BufRead,BufNewFile *.view set filetype=php
    autocmd BufRead,BufNewFile *.info set filetype=dosini
  augroup end 
endif

Visualizing line numbers is also a one-liner:

set number

To toggle showing/hiding line numbers, you may wish to remap the « ,l » key sequence as follows:

let mapleader = ","
nnoremap <leader>l :set nonumber!<CR>

Autocompletion enhancements

Wildmenu offers a comprehensive autocomplete feature. Press the TAB key while in the :command mode to visualize a suggestion of autocomplete candidates. To activate wildmenu, place the following line in your .vimrc.

set wildmenu

In insert mode, the control-p key will offer Vim/Ctags users a list of suggestions for completing your function calls.

Essential vim modules

See : https://vimawesome.com/

Pathogen

Pathogen simplifies vim extension management.

Just add pathogen.vim to your .vim/autoload folder and add the following line at the top of your .vimrc and you will be able to activate any vim extension simply by extracting it to your .vim/bundle folder.

call pathogen#infect()

TagList

TagList uses vim's Vim/Ctags file to present you with a list of constants, variables and function names available within the scope of your code file. Just like an IDE does.

You may be required to specify the location of the ctags command in order to activate this feature. On Debian systems, adding the following line to your .vimrc should do the trick:

let Tlist_Ctags_Cmd = "/usr/bin/ctags"

TagBar

Une alternative au précédent.

Syntastic

Syntastic allows for easy Lint-style syntax checking for a variety of languages.

To install Syntastic, you can either clone the upstream git repository as linked just above, and use a tool like Pathogen to manage installation, or you can install the debian package:

apt install vim-syntastic

These configurations in your .vimrc might make your experience more pleasant with Syntastic:

" Populate and open Location List when checking syntax
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
"let g:syntastic_check_on_open = 1 " This one's annoying when opening a large file
" Disable syntax check on that useless case
let g:syntastic_check_on_wq = 0

The following .vimrc settings will allow you to launch a syntax check and open up an error report pane quite rapidly:

nmap <silent> <leader>sc :SyntasticCheck<CR>
nmap <silent> <leader>se :Errors<CR>

Using Syntastic may require setting up a working syntax checker on your system. For example, to verify your code against the Drupal Coding Standards, you should install the Drupal Code Sniffer module, and tell vim to use it in place of the default PHP checks.

let g:syntastic_phpcs_conf='--standard=Drupal'

Note that the the Drupal Code Sniffer can be a bit too strict (even for core!), and slow on large files.

NERD Tree

NERD Tree is a filesystem explorer.

vinegar.vim

Snipmate

Snipmate lets you define per-file type templates (or snippets) and use them in a manner similar to how you would in TextMate.

Write the snippet name and then hit the Tab key to paste the snippet in your buffer. You can then cycle through "variable" spots to change them (i.e. class name, parameter names, values, etc).

Here's an example, editing a php file:

<?php

ife<TAB>

The "ife" snippet is defined in this way in the php snippets:

snippet ife
        if (${1:/* condition */}) {
                ${2:// code...}
        } else {
                ${3:// code...}
        }
        ${4}

Hitting Tab right after typing "ife" will paste the associated snippet:

<?php

if (/* condition */) {
    // code...
} else {
    // code...
}

The "/* condition */" part will be highlighted. You can start typing to replace that part with the condition you'd like to verify.

When you placed what you wanted in the condition, hit Tab and it'll highlight the first "// code..." line. You can start typing to replace the line by the code you want to see in this part.

Hit Tab again, and it'll highlight the second "// code..." line. Type in the code for the second part.

When done, hit Tab again to move the cursor after the last closing brace.

Sadly, the original author seems to have stopped development, and stopped responding to tickets in the github interface. So, others have started to fork the project in order to add their own useful things..

Surround

Surround.vim makes your life easier when you want to substitute a delimiter around a string of text. With this plugin, you can easily change a singly-quoted string into a doubly-quoted one, a pair of curly braces around a block of code into square brackets or parentheses.

I can't really explain it better than this post by Peteris Krumins did.

Repeat

Repeat.vim re-maps your "." key to a function that can repeat mappings. So, it'll enable you to repeat a whole bunch of non-visual commands, like the "csw" mapping (change surrounding around word to something else) that the Surround.vim plugin above brings you.

Check out an example here.

Speeddating

Speeddating Adds date increment/decrement functionality to Ctrl-A/Ctrl-X.

... You knew about Ctrl-A/Ctrl-X before, right? You should!

The repeat.vim plugin makes those commands able to not only increase decimal, octal and hex numbers, but also dates. It wraps around time, day, month and year:

1999-12-30 # this is in your text, and you're in "normal" (e.g. not insert) mode

Place your cursor on or before the above date, and hit Ctrl-A to increase the date. It becomes:

2000-01-01

The plugin also adds mappings to transform a date in the text to the current date and time. In the above example, if you place your cursor on the date and hit the sequence 'd<Ctrl-X>', the date will be changed to the current date.

It can also increment/decrement roman numbers (I, II, III, IV, etc) and english ordinals ("1st", "2nd", "3rd", etc)

Gitgutter

vim-gitgutter will show you diff-like indicators (e.g. +/-/~ to show added, removed and modified lines) in the gutter (vertical line left of line numbers, or the text buffer if line numbers are not enabled, when editing a file that's in a git repository. Indicators are updated every time you save the file.

This way you'll quickly know what changes are in place for this file with regards to what's in the repository.

Limelight

LimeLight creates a mode that can be activated to gray out all text but the current paragraph or block of code. It makes it super easy to focus on the current lines that you're editing.

With the following line in your vimrc, it'll make it easy to toggle LimeLight mode using Ctrl-g+l:

nnoremap <C-g>l :Limelight!!<CR>

fzf

fzf is a command line tool that takes lines of text as input and lets you interactively type to fuzzy-search something in those lines, then outputs the selection.

The vim plugin called fzf lets you use the fuzzy-search functionalities within vim. By itself it's only an integration, but you need to define functions for what text to search on and actions to take with the selection.

Unfortunately fzf is not yet packaged in debian, but the fzf vim plugin will offer to download it for you if it's missing. The binary will be installed in a bin/ directory inside the plugin's own directory (e.g. for me that was ~/.vim/bundle/fzf/bin/fzf)

fzf.vim is a plugin from the same author as the fzf vim plugin that basically just defines a couple of search functions for you. You can fuzzy-search file names on disk, file names tracked by git, git commit messages, file lines within vim buffers, file lines with the ag command (think grep -r), window names, buffer names, marks.

To use the recursive grep look-alike ag to search for file lines, you need to install that tool first: apt install silversearcher-ag.

To get a better idea of all functions from fzf.vim, see this presentation

* Il se peut que vous aillez des problèmes à utiliser fzf. Les instructions ne sont pas généralisé à pathogen par exemple.

* Si vous voyez: No fzf#run function c'est parce qu'il vous faut aussi installer l'utilitaire fzf en plus de fzf-vim: junegunn/fzf: A command-line fuzzy finder

* Une fois que vous avez clonez le repos, vous pouvez utiliser ./install --bin pour installer localement dans votre home.

* Dans .vimrc, il faut aujouter: set rtp+=~/.fzf. Cela va fonctionner si votre git clone de fzf est nommé .fzf et est à la racine de votre home.

tabular

tabular permet d'aligner des éléments par exemple pour former des tableaux bien espacés.

Pour l'utiliser on appelle la fonction Tabularize et on lui donne comme argument / et la chaîne de caractères autour duquel on veut aligner les choses. Par exemple, pour aligner un tableau dans moinmoin (penser rapport de paye), on peut utiliser la commande suivante pour aligner les double barres verticales:

:Tabularize /||

Aussi, dans le contexte du code puppet, le plugin est extrêmement utile pour aligner les flèches dans les "hash" de données ou bien les paramètres de define ou classe:

:Tabularize /=>

fugitive.vim

surround.vim

Guides visuels pour l'indentation: vim-indent-guides ou indentLine

Garder les choses alignées comme du monde sur les niveaux d'indentation c'est toujours un peu chiant quand on n'a pas d'indicateur visuel pour aider. Les deux plugins plus bas ajoutent des guides visuels pour ça!

Useful subsitute commands

moinmoin formatting

s/\[\[\([^|]*\)|\([^|]*\)\]\]/\2 (\1)/gc

Takes a moinmoin formatted wiki link and reformats it in the format:

Linktext (Linkpath)

Convert from camel case to underscore seperated

%s/\([a-z]\)\([A-Z]\)/\1_\L\2/gc

Themes

Solarized and vimdiff

set background=dark
let g:solarized_diffmode="high"
colorscheme solarized


CatégorieOutilSysadmin

Vim (last edited 2018-12-06 17:51:59 by GabrielFilion)