Outils pour utilisateurs

Outils du site


issue56:c_c

Ceci est une ancienne révision du document !


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://pastebin.com/EqrfBFhF). I'll cover using visual block mode, some tricks for commenting large numbers of lines, a couple of tricks for using the mouse, and copying/pasting to/from external programs from/to Vim. If you're familiar with all of these topics, you can safely skip this article.

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; 16>12. What the script does is simply calculate which numbers (from a supplied range of numbers) are abundant, and which are not. The function is part of my solution to an Euler Project problem.

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)

For those of you who are programmers, you'll be familiar with the concept of commenting out all code besides a small segment you want to test, when things aren't working. My approach to doing this is to use Visual Block Mode. The steps are as follows (from the beginning of the first line you want to comment):

<ctrl>+[v]; [j]; <shift>+[i], [#]; [Esc]

The first step enters Visual Block mode, the j key acts as a down arrow key, and <shift>+[i] enters insert mode for all selected lines. After these steps, you then hit the key for the comment symbol (in Python's case it's the octothorpe, a.k.a. hash symbol). For uncommenting, check Area 2 for Deleting in Visual Block Mode.

The reader who contacted me offered the following script to do the same:

“ COMMENTING OUT A # CHARACTER IN BASH SCRIPTS function! AddDelBashComment()

 let char=getline('.')[0]
 if char == "#"
     s/^#//g
  else
     s/^/#/g
  endif

endfunction vmap <silent> # :call AddDelBashComment()<CR>

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):

<ctrl>+[v][v][V]; [j]; [#]

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.

Area 2 (Visual Block Mode)

Since we covered inserting in Visual Block Mode in Area 1, I will not repeat it in this area.

Deleting in Visual Block Mode:

<ctrl>+[v]; [j][h][l][k]; [d][x][X] 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. Yanking (copying) text in Visual Block mode: Section of a line: [v]; [h][j][k][l]; [y]; [h][j][k][l]; <ctrl>+[v]; [h][j][k][l]; <shift>+[i]<shift>+[a]; <ctrl>+[r]+[“]; [Esc] <shift>+[i] inserts at the start of the line/selection, and <shift>+[a] appends to the end of the line/selection. Copying and pasting an entire line in multiple lines doesn't work with this method (at least not for me). As such, we won't cover it. As a side-note: <ctrl>+[r]+[“] works in any insert mode and pastes the contents of the Vim register (the local clipboard). Area 3 (Mouse usage) Just a brief tip: If you want to highlight something in Vim using Visual Block Mode, hold <shift>+<alt> as you select. Area 4 (Copying and Pasting to/from external programs) 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][j][k][l]; [“][+][y] A quick explanation: you select the text you want (first two steps) and then you hit the quotation marks key (on German keyboards it's <shift>+[2]), and then the plus key, and then the y key. Do this one after the other, not all at once. Then to paste in the external program, just use <ctrl>+[v], as per usual. Pasting: [“][+][p] 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. Area 5 (Extra tips) Syntax Highlighting: You can enable syntax highlighting in Vim by using: :set syntax=on (in Vim itself) or syntax enable (in your .vimrc) Hide Vim in the terminal: <ctrl>+[z] will suspend a task into the background (tested in Zsh and Bash). Once you've suspended a task, you can re-open it using the command fg in the terminal. In keypresses: <ctrl>+[z]; [f][g][Enter] Encrypt files with Vim: 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). Viewing History: [q][:] This will pull up a list of past commands. You can enter the number from the list in order to pull up the command, or enter [:][q] to quit the list. Execute system commands from within Vim: [!](command) An example: :w !sudo tee % This will save the file with sudo rights (in case you open a system file and made changes before realizing you had no rights to save the file). Vim will ask you afterwards if it ought to re-load the file, which you'll want to do. Vimdiff: Vimdiff is an extended version of Vim where you can open multiple files to compare them. Usage: vimdiff file1 file2 For horizontal split: vimdiff -o file1 file2 For more information: http://vimdoc.sourceforge.net/htmldoc/diff.html Specify that a Tab is 4 spaces (useful for Python users): set tabstop=4 Write this into your .vimrc, and any time you press the tab key, it will actually insert up to 4 spaces. This should be more than enough to keep everyone busy until next month. If you have any questions, comments, or requests, feel free to email me at lswest34@gmail.com. If you do email me, please be sure to include “C&C” or “FCM” in the subject, so that I don't overlook it. My .vimrc file: http://pastebin.com/wv260CJk 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, feel free to email me at lswest34@gmail.com. If you do email me, please include “FCM” or “C&C” (or, as a regular expression: [fFcC][cC&][mMcC]) in the subject header.

issue56/c_c.1325489156.txt.gz · Dernière modification : 2012/01/02 08:25 de fredphil91