Simple UNIX and VIM Tips"Simplify, simplify, simplify" | ![]() |
TIP: Simple Macro Recording in VIM.Doing it1. Let's take the mystery out of macro recording in VIM; it is truly very simple. In vim type in:
How does this work? 2. Using simple macros in VIM is very easy, the whole operation consist of three commands: q [name of the macro] (to be technically correct it should be called a 'register') to start recording, example: 'qa'q (to stop recording the macros)[number of repetitions]@[name of the macro], i.e. '4@a' means "repeat macro 'a' 4 times"Extra super-bonus 3. But wait, there is more! Here is one way you could re-indent a snippet of code: Suppose you have inherited the following function written in PHP:
function func_get_value( $x )
and you would like to indent the internals of it for better readability. Here is how you can do it very quickly.
(In another example here I show how you can create a simple table using macro recording. Yes, I know, there are many rows in this example, but they are mostly very simple things you already know: moving up and down, copying (or 'yanking') and pasting.) You are done! ANfSCD top |
|||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||
TIP: Split & Conquer - splitting screens in VIM.Doing it1. In vim type in: :split [some file name] (to split the screen horizontally)CTRL-W-W (to switch between the sreens)How does this work? 2. There are several options available for screen splitting, and :split is just one of them. Another useful option is :vsplit, which splits the window vertically. Naturally, you can split a window horizontally for two files, and then split one of them vertically for the third file.For example, say you have 3 files: one.html, two.html and three.html.
vim one.html (to open the first file):split two.html (to open the second file in the same window):vsplit three.html (to open the third file in the two.html window and split it vertically)CTRL-W-WExtra super-bonus 3. But wait, there is more! In fact there is wa-a-a-y too much more. But here are some basics that you probably will not forget. First, window resizing. To change the size of the window simply type in: :res 50 (to change the size of the current window to '50'):vertical res 40 (to change the vertical size of the current window to '40')Second, window placing. To move windows in the screen use these simple commands: CTRL-W r (to rotate windows downward for horizontal splits or to the right for the vertical splits)CTRL-W R (to rotate windows upward for horizontal splits or to the left for the vertical splits), this is more of a pain however - Ctrl, Shift, upper case, lower case... I suspect CTRL-W r will serve you in most cases. :help splitting if you are interested)Third, once you have all these windows open and split in all sorts of way, you might want to regain your sanity and close them. The easiest way to close a window is through regular: :q command. But if you want to close all windows except for the one you are in, you can use: :only command.You are done! ANfSCD top |
|||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||
TIP: Manage Your Cases. | |||||||||||||||||||||||||||||||||||||||||
|
Doing it v (to switch to Visual mode)k or h (to select a word, a line or a phrase)~ (to switch the case)How does this work? 2. A special case of this case-changing exercise is a case where you want to change the case to upper-case (as opposed just switching to the other case). The case switching operation is done via either u or U characters. In other words, if you want to switch a character to upper case you would use this command:gUguThis is good to know, but in my opinion, why bother? Just use the '~' approach. |
|||||||||||||||||||||||||||||||||||||||||
|
Extra super-bonus 4. But wait, there is more! If you set a 'notildeop' option in vim you can then just use '~' to switch the case of character under cursor
and move the curser one position to the right. Let me demonsterrate::set notildeop3b (to bring the cursor back to the word 'hundred')~ (to make it 'Hundred')w (to bring the cursor to the next word)~ (to make the next word 'Acre')w~ (you get the idea)And, yes, if you want to switch the case of the entire word, or a line or the whole document you can just keep pressing '~'. However, a better option would be to use 'g~~' to change the case of a line, orjust select the entire doc in visual mode and type '~' to make a global case switch.
ANfSCD top |
|||||||||||||||||||||||||||||||||||||||||
TIP: Matching brackets in your code.
Doing it :set showmatchTesting it 2. Now type in a bunch of code to test it out. Here is a PHP example (code lifted from PHP.net):
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
Now, if you move the curser to any bracket or a brace -"[", "(" or "{", you will notice that the cursor
jumps briefly to the matchng bracket. By the way, if the cursor is out of site because the routine is to big, (kinda like this sentense)
you can always match it manually by typing "%" at the bracket you are trying to match.
Extra super-bonus 4. But wait, there is more! Having an ability to jump to a matching bracket allows you to select entire routines very simply. For example:
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
If you want to select (to copy and paste for instance) the entire routine, you could this: 1. Get to the first brace "{"; cursor is going to jump briefly to the matching bracket. 2. Type v - this will switch you to the visual mode.3. Type % - this will select the entire routine.4. Now you can copy it ( y) and paste it (p) later on.NOTE TO THE PURISTS: Yeah, I know about not copying and pasting code. It happens and sometimes it is useful. ANfSCD top TIP: Recalling your previous commands at the term line.
Doing it %>ls -r |grep mytext "mytext" should be "mynum" "k""w" (5 times)"cw mynum"ANfSCD top TIP: Counting files in a TAR package without openning it.
Doing it %>tar tvf |wc -lfiles.tar.
How does this work? 2. Essentially we are taking several commands and are stringing them together with "piping". tar tvf extracts the table of contents from files.tar.t - table of contentsv - verbose outputf - file namewc -l performs a "word count", the l switch counts lines only.Extra super-bonus 3. But wait, there is more! This approach will let you count how many file of a particular type there are are in files.tar
For example:
files.tar contains .txt files, .php files and .config files.grep to select what type of files you would like to count.txt files. Command: %>tar tvf files.tar |grep *.txt |wc -l*.txt files.ANfSCD top TIP: Subsituting characters for carriage returns in VIM.
Doing it :%s/,/^M/g^M in the command line in VIM do this:^M displayed in the command line.
Testing it 2. Create a CSV file, such as:
all,work,no joy,makes,dull
Extra super-bonus 3. But wait, there is more! Be careful when you make this change, because it is a one-way substitution. You'd think that like any good UNIX/VIM command this would work in reverse, but it doesn't. In other words, if you want to replace your carriage returns with commas, this approach will not work. So, if you modify your file and then want to reverse your changes, you will need to resort to something else, like sed.In other words, command: %s/^M/, /gwill not do any magic, you will get an error: E486: Pattern not found: ^M, as sad as it sounds. So, in this case...ANfSCD top |
|||||||||||||||||||||||||||||||||||||||||