issue56:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue56:c_c [2012/01/02 08:25] – créée fredphil91 | issue56:c_c [2012/02/07 13:16] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Due to the large interest presented in this topic by a reader, I've decided to write another one or two articles on Vim (including this one). This month I'll be focusing on a tangible example (file can be found here: http:// | + | **Due to the large interest presented in this topic by a reader, I've decided to write another one or two articles on Vim (including this one). This month I'll be focusing on a tangible example (file can be found here: http:// |
Before we begin, I will briefly explain what an abundant number is, so that everyone can roughly follow the script. An abundant number is a number for which the sum of all factors (a factor is a number which divides a value without a remainder) is greater than the number itself. Example: The factors of 12 are: 1,2,3,4,6; the sum of the factors: 1+2+3+4+6=16; | Before we begin, I will briefly explain what an abundant number is, so that everyone can roughly follow the script. An abundant number is a number for which the sum of all factors (a factor is a number which divides a value without a remainder) is greater than the number itself. Example: The factors of 12 are: 1,2,3,4,6; the sum of the factors: 1+2+3+4+6=16; | ||
- | In order to follow the article, I'd highly recommend opening a copy of the file from Pastebin in Vim (or GVim) so you can work along. | + | In order to follow the article, I'd highly recommend opening a copy of the file from Pastebin in Vim (or GVim) so you can work along.** |
- | Area 1 (Commenting) | + | Vu le grand intérêt suscité par ce sujet chez un lecteur, j'ai décidé d' |
+ | |||
+ | Avant de commencer, je vais vous expliquer brièvement ce qu'est un nombre abondant, afin que chacun puisse à peu près suivre le script. Un nombre abondant est un nombre qui est plus grand que la somme de tous ses diviseurs (un diviseur est un nombre qui divise une valeur sans un reste). Exemple : les facteurs de 12 sont 1,2,3,4,6 ; la somme des diviseurs vaut : 1+2+3+4+6 = 16, et 16>12. Le script calcule simplement quels nombres (compris entre deux valeurs données) sont abondants et lesquels ne le sont pas. La fonction fait partie de ma solution à un problème du Projet Euler. | ||
+ | |||
+ | Afin de suivre l' | ||
+ | |||
+ | **Area 1 (Commenting) | ||
For those of you who are programmers, | For those of you who are programmers, | ||
Ligne 15: | Ligne 21: | ||
The reader who contacted me offered the following script to do the same: | The reader who contacted me offered the following script to do the same: | ||
- | " COMMENTING OUT A # CHARACTER IN BASH SCRIPTS | + | |
- | function! AddDelBashComment() | + | function! AddDelBashComment() |
- | | + | let char=getline(' |
- | | + | if char == "#" |
+ | < | ||
+ | else | ||
+ | | ||
+ | endif | ||
+ | endfunction | ||
+ | vmap < | ||
+ | |||
+ | Section 1 (Commenter) | ||
+ | |||
+ | Les programmeurs parmi vous seront familiers avec le concept de commenter tout le code sauf un petit bout que vous voulez tester ; c'est utile quand les choses ne fonctionnent pas. Pour ce faire, j' | ||
+ | |||
+ | < | ||
+ | |||
+ | La première étape passe en mode visuel par bloc, la touche j agit comme la flèche vers le bas et < | ||
+ | |||
+ | Le lecteur qui m'a contacté a proposé le script suivant pour faire la même chose: | ||
+ | |||
+ | " COMMENTER AVEC UN # DANS LES SCRIPTS BASH | ||
+ | function! AjouterSupprimerCommentaireBash() | ||
+ | let char=getline(' | ||
+ | | ||
| | ||
else | else | ||
| | ||
endif | endif | ||
- | endfunction | + | |
- | vmap < | + | vmap < |
- | This script has to be added to your .vimrc. Once it has been added, you can call it in the following way (same process for commenting and uncommenting): | + | **This script has to be added to your .vimrc. Once it has been added, you can call it in the following way (same process for commenting and uncommenting): |
- | < | + | < |
- | As you can see, the only thing you save by doing this is entering and exiting insert mode (and possibly having to press the control key). I've included this script for the sake of those for whom every keystroke counts. You will need to adjust the substitute lines for each comment character you frequently use. For SQL you would replace if char == “#” with if char == “-” and s/^#//g with s/^--//g (same for the other substitution command). You must also replace the octothorpe in the vmap line, otherwise you'll be using the same key for multiple functions. | + | As you can see, the only thing you save by doing this is entering and exiting insert mode (and possibly having to press the control key). I've included this script for the sake of those for whom every keystroke counts. You will need to adjust the substitute lines for each comment character you frequently use. For SQL you would replace if char == “#” with if char == “-” and < |
- | Area 2 (Visual Block Mode) | + | Ce script doit être ajouté à votre .vimrc. Une fois qu'il a été ajouté, vous pouvez l' |
+ | |||
+ | < | ||
+ | |||
+ | Comme vous pouvez le voir, la seule chose que vous économisez en faisant cela est d' | ||
+ | |||
+ | **Area 2 (Visual Block Mode) | ||
Since we covered inserting in Visual Block Mode in Area 1, I will not repeat it in this area. | Since we covered inserting in Visual Block Mode in Area 1, I will not repeat it in this area. | ||
Ligne 38: | Ligne 71: | ||
Deleting in Visual Block Mode: | Deleting in Visual Block Mode: | ||
- | < | + | < |
Which key you use in the second step is entirely dependent upon which direction you want to go (down, left, right, up, respectively). The key in the last step is entirely up to you, they all do the same. | Which key you use in the second step is entirely dependent upon which direction you want to go (down, left, right, up, respectively). The key in the last step is entirely up to you, they all do the same. | ||
Ligne 45: | Ligne 78: | ||
Section of a line: | Section of a line: | ||
- | [v]; [h]// | + | < |
< | < | ||
- | Copying and pasting an entire line in multiple lines doesn' | + | Copying and pasting an entire line in multiple lines doesn' |
- | Area 3 (Mouse usage) | + | Section 2 (mode Visuel par bloc) |
+ | |||
+ | Puisque nous avons parlé de l' | ||
+ | |||
+ | Suppression en mode Visuel par bloc : | ||
+ | |||
+ | < | ||
+ | |||
+ | La touche à utiliser dans la deuxième étape est entièrement dépendante de la direction dans laquelle vous voulez aller (respectivement vers le bas, la gauche, la droite, le haut). La touche dans la dernière étape est à votre choix, elles font toutes la même chose. | ||
+ | |||
+ | Emporter (copier) du texte en mode Visuel par bloc : | ||
+ | |||
+ | Section d'une ligne : | ||
+ | < | ||
+ | |||
+ | < | ||
+ | |||
+ | Copier et coller une ligne entière sur plusieurs lignes ne fonctionne pas avec cette méthode (du moins pas pour moi). Donc je n'en parlerai pas. Une petite remarque : < | ||
+ | |||
+ | **Area 3 (Mouse usage) | ||
Just a brief tip: If you want to highlight something in Vim using Visual Block Mode, hold < | Just a brief tip: If you want to highlight something in Vim using Visual Block Mode, hold < | ||
Ligne 59: | Ligne 111: | ||
You may have noticed that the yank and paste methods work only within Vim. To copy text from Vim to another program (firefox, for example), you can select the text with the mouse and use the middle-mouse button paste. If, however, you are at another computer that runs a different operating system (or lacks that function), you can copy text to the system clipboard with: | You may have noticed that the yank and paste methods work only within Vim. To copy text from Vim to another program (firefox, for example), you can select the text with the mouse and use the middle-mouse button paste. If, however, you are at another computer that runs a different operating system (or lacks that function), you can copy text to the system clipboard with: | ||
- | [v]//[V]; [y]; [h]// | + | < |
A quick explanation: | A quick explanation: | ||
Ligne 69: | Ligne 121: | ||
That's it. Press those 3 keys and it will paste the clipboard onto the line you have selected (so you may need to start a new line if that's what you want). | That's it. Press those 3 keys and it will paste the clipboard onto the line you have selected (so you may need to start a new line if that's what you want). | ||
- | You can also set the clipboard to autoselect, which should automatically copy to the system clipboard when you highlight something, and automatically paste from the clipboard when you press the middle-mouse button. | + | You can also set the clipboard to autoselect, which should automatically copy to the system clipboard when you highlight something, and automatically paste from the clipboard when you press the middle-mouse button.** |
- | Area 5 (Extra tips) | + | Section 3 (utilisation de la souris) |
+ | |||
+ | Juste un conseil rapide : si vous voulez mettre en évidence quelque chose dans Vim en étant dans le mode Visuel par bloc, appuyez sur < | ||
+ | |||
+ | Section 4 (copier et coller depuis/vers des programmes externes) | ||
+ | |||
+ | Vous avez sans doute remarqué que les méthodes copier et coller fonctionnent uniquement à l' | ||
+ | |||
+ | < | ||
+ | |||
+ | Une explication rapide : vous sélectionnez le texte que vous voulez (deux premières étapes), puis vous appuyez sur la touche guillemets (sur les claviers allemands c'est < | ||
+ | |||
+ | Coller: | ||
+ | |||
+ | [" | ||
+ | |||
+ | C'est tout. Appuyez sur ces 3 touches et cela va coller le presse-papier sur la ligne que vous avez sélectionnée (vous devrez peut-être commencer une nouvelle ligne si c'est ce que vous voulez). | ||
+ | |||
+ | Vous pouvez également régler le presse-papiers sur autoselect, ce qui devrait automatiquement copier dans le presse-papiers système lorsque vous surlignez quelque chose et coller à partir du presse-papier automatiquement lorsque vous appuyez sur le bouton du milieu de la souris. | ||
+ | |||
+ | **Area 5 (Extra tips) | ||
Syntax Highlighting: | Syntax Highlighting: | ||
Ligne 93: | Ligne 165: | ||
vim -x <file name> | vim -x <file name> | ||
- | This command will prompt you for an encryption key before you view the file (if the file is empty/new, it will then store the password you enter). | + | This command will prompt you for an encryption key before you view the file (if the file is empty/new, it will then store the password you enter).** |
- | Viewing History: | + | Section 5 (astuces supplémentaires) |
+ | |||
+ | La coloration syntaxique : | ||
+ | Vous pouvez activer la coloration syntaxique dans Vim en utilisant : | ||
+ | |||
+ | :set syntax=on (dans Vim lui-même) | ||
+ | |||
+ | ou | ||
+ | |||
+ | syntax enable (in votre .vimrc) | ||
+ | |||
+ | Masquer Vim dans le terminal : | ||
+ | < | ||
+ | |||
+ | En frappes, cela donne : | ||
+ | |||
+ | < | ||
+ | |||
+ | Crypter les fichiers avec Vim : | ||
+ | |||
+ | vim -x < | ||
+ | |||
+ | Cette commande vous demandera une clé de chiffrement avant de voir le fichier (si le fichier est vide/ | ||
+ | |||
+ | **Viewing History: | ||
[q][:] | [q][:] | ||
Ligne 116: | Ligne 212: | ||
Usage: | Usage: | ||
- | vimdiff file1 file2 | + | vimdiff file1 file2** |
- | For horizontal split: | + | Affichage de l' |
+ | |||
+ | [q][:] | ||
+ | |||
+ | Cela affichera une liste des commandes passées. Vous pouvez entrer le numéro de la liste afin de faire revenir la commande ou entrer [:][q] pour quitter la liste. | ||
+ | |||
+ | Exécuter les commandes système à partir de Vim : | ||
+ | |||
+ | [!](commande) | ||
+ | |||
+ | Un exemple : | ||
+ | |||
+ | :w !sudo tee % | ||
+ | |||
+ | Cela sauvegardera le fichier avec les droits sudo (pour le cas où vous ouvrez un fichier système et le modifiez avant de réaliser que vous n' | ||
+ | |||
+ | Vimdiff : | ||
+ | Vimdiff est une version étendue de Vim où vous pouvez ouvrir plusieurs fichiers pour les comparer. | ||
+ | |||
+ | Utilisation : | ||
+ | |||
+ | vimdiff fichier1 fichier2 | ||
+ | |||
+ | **For horizontal split: | ||
vimdiff -o file1 file2 | vimdiff -o file1 file2 | ||
Ligne 134: | Ligne 253: | ||
My .vimrc file: http:// | My .vimrc file: http:// | ||
- | I hope you've found this article to be interesting. I plan to continue along this path next month. If you have any questions, comments, or suggestions, | + | I hope you've found this article to be interesting. I plan to continue along this path next month. If you have any questions, comments, or suggestions, |
+ | |||
+ | Pour une scission horizontale : | ||
+ | |||
+ | vimdiff -o fichier1 fichier2 | ||
+ | |||
+ | Pour plus d' | ||
+ | |||
+ | Spécifier qu'une tabulation vaut 4 espaces (utile pour les utilisateurs de Python) : | ||
+ | |||
+ | set tabstop=4 | ||
+ | |||
+ | Ecrivez ceci dans votre .vimrc, et à chaque fois que vous appuierez sur la touche de tabulation, il insérera en fait jusqu' | ||
+ | |||
+ | Cela devrait être plus que suffisant pour garder tout le monde occupé jusqu' | ||
+ | |||
+ | Mon fichier .vimrc : http:// | ||
+ | |||
+ | J' | ||
issue56/c_c.1325489156.txt.gz · Dernière modification : 2012/01/02 08:25 de fredphil91