Comfortable PHP editing with VIM -5-
More than a half year after my last "Comfortable PHP editing with VIM" post, I take up this series again, although I decided to let it die in January. Sadly I did not find any time by now, to extend PDV (the PHP Documentor for VIM plugin) further than it's version 1.0.1, which is available through vim.org and on my SVN. Anyway, I collected a lot of (at least for me) helpful VIM tips, which I'd like to share here:
Contents
Next occurrence of a word
If you hit *
in command mode and your cursor resides on a word, you are taken to the next occurrence of the word. This is quite nice, if you like to know, where a function is called again.
Find matching brace
VIM 7.0 luckily highlights matching parenthesis, if your cursor resides on a brace, but sometimes you need to quickly jump to that matching brace. You can achieve this by hitting the % sign in command mode.
Repeat the last change
It often occurs, that you need to perform 1 change several times, but not often enough to write a short script or to address the changes with a complex regex. In those cases you can perform the change once, move the cursor to the next place and hit the . (dot) char, in command mode.
Enhanced functionality with external programs
This tip is quite commonly known, but I repeat it here, because it's really helpful: Although VIM has a huge set of useful functions, it does not have everything. If you don't find a utility in VIM, you can simply use extern shell tools to perform certain tasks, by using :!<command> in command or visual mode. For example sorting a range of lines can easily be achieved with the following 2 steps: 1. Select the lines to sort in visual mode. 2. Type :!sort and hit return.
Indenting and unindeting
Especially when editing code, it happens often, that you have to indent/unindent a code block. If you use VIM's auto-indenting features (like used by my .vimrc), VIM can do that job for you, too. Simply select the lines to (un-)indent in visual mode and hit <} to unindent 1 level or > to indent 1 more level. This also works (as usual in VIM) with quantifiers like 4> to indent 4 more levels.
Remote file editing
Often you have to quickly edit a file on a remote server. Usually you SSH to that server or mount a remote file system locally, edit the desired file and quit the session again. VIM allows you to do this i 1 step, using SCP. On your shell type vim scp://user@server//path/to/the/file and VIM will edit the file directly through SCP.
Recover the last visual selection
It often happens, that you selected some text in visual mode, e.g. to perform a regex on it. Occasionally your regex is incorrect and you have to revert the changes or they don't even happen. What you then need is to re-select the text portion again, which might take quite a long time, if you deal with large files. Hitting gv in command mode, takes that work from you and selects the last visual selection again.
Grep within VIM
Refactoring happens quite often in dynamic projects. "Real" IDE's offer a lot of tools for this task. While VIM does not, you can enhance your refactoring process a lot with using regex and this tiny tip: To grep through a lot of files for certain strings and then perform changes on each occurrence, vim offers the :grep command. :grep <string> <files> and VIM will store the list of changes for you and :cn will take you to the next occurance of your string. The :grep command utilizes your systems instance of grep, or a custom command stored in the grepprg configuration variable. There is also an internal implementation of grep available in VIM, which you can use with :vimgrep /<regex>/<flags> <files>. The benefit of the external implementation is, that you can easily customize the command to use, e.g. if you need to ignore certain directories while grepping recursively (for instance .svn dirs). You can also get a list of all occurrences of your grep command, using :ll, which is actually a feature of VIMs quickfix tool. Simply select the next entry you want to jump to and hit return in the location list.
Placeholders in commands
Who doesn't know the case, where you need to open a file in the same directory, the currently edited file resides in? Sure, you can simply use :split <file>, but if your current working directory is far away from the edited file, you possibly will have to type a long long path again. VIM offers you, to replace certain placeholders in commands automatically, most important: % is replaced with the actual file name (so, :split % will open a new window editing the current file - I know that just :split does the same, but it's a nice example to show what % does). So, how does this help in the described case? The replacement text can also be modified by certain modifiers, like :h, which removes the last portion of a file path (the file name). For instance if you currently edit /home/dotxp/dev/ez/ezc/trunk/ConsoleTools/src/table.php and want to split to /home/dotxp/dev/ez/ezc/trunk/ConsoleTools/src/output.php you can do this easily using :split %:h/output.php. Another useful modifier is :s?<regex>?<replacement>?, which obviously uses a regex to modify the given path. Unlikely the usual replacement command :s, you need to specify the global modifier g before the regex instead of doing so after it. A short example: :split %:gs?trunk?/releases/1.1/? will split to the file table.php of ConsoleTools release 1.1.
Emergency help
Appending an exclamation mark (!) to a command (:<command>!) forces VIM to execute the command, even if it might be dangerous to do so. Quite useless, but still funny is :help!.
Rewrap my text
I reached a stage, where do not only edit my source code using VIM, but mainly every other kind of text data (like documentation, todo lists, etc.), because I'm simply so used to its features. When editing plain text, the automatic wrap features (e.g. textwidth) of VIM are quite useful. In some cases, you might edit text later and therefore destroy a nicely wrapped text block. VIM can easily re-perform the wrapping for you: Select the text block in visual mode (using <SHIFT>+<v>) and hit "<g><q>".
Spelled correctly?
In VIM versions earlier to 7.0 you could add spellchecking using external scripts. The new version has this feature per default. What you need to do before spell checking is available, is to set the language of the document you are currently editing, using :setlocal spell spelllang=en_us (note, :setlocal only sets the value for the current window, use :set to set it globally). Now VIM highlights all unknown words and you have 3 important new commands available (in command mode). All are used when your cursor resides on a highlighted word: <z><=> opens a list of suggested replacements, you can choose one by typing its number or leave the selection untouched using <RETURN>. <z><g> adds the highlighted word to the global dictionary, so that it is recognized as correct on any spell checking session, later. <z><G> only adds the word to the local dictionary, so that is recognized as correct during the actual spell checking session.
Macros
Performing complex tasks very often might be quite time consuming and often not solvable using even highly complex regex. Before writing a complex script, you should consider, that VIM can simply follow your instructions and record them to a script: Type <q><a> in command mode, to instruct VIM to record all following commands to the register a. Perform whatever you like and finally hit <q> again (in command mode). Your macro is now recorded. Type <@><a> in command mode, to execute the contents of register a (namely your just recorded macro). A simple example for this is to move certain lines of a file to its top: Go to the first line you need. Type <q><a>. Now perform the changes: <d><d> to delete the line, <m><'> to set the "last editing position mark", <:><1><RETURN> to jump to the first line of the file, <P> to paste the just deleted line, <'><'> (2 single quotes) to return to the last editing position. Finally hit <q> to end the recording. Now you can just do <@><a> on every line you need at the start of your file. Easy, isn't it? :) Additionally <@><@> repeats the last macro execution (so you even don't need to remember the register you stored it in).
Uppercase letters
There are a lot of helpful shortcuts on your keyboard, which you might not know about. Every VIM user should now, that <d> deletes something, <y> (yank) copies something and <c> changes something (delete and start inserting) in VIM (all in command mode). Furthermore, most people know that e.g. <d><w> deletes the next word, etc.. What I did not know for a long time was, that <D>, <Y> and <C> are shortcuts to perform the desired operation until the end of the current line. For instance, simply type <C> to change the contents of the current line from your cursors position on.
Where am I?
If you have a large resolution (e.g. mine is 166x1200), you sometimes loose your cursor when looking at something else in the room. The new customization features of VIM 7 allow you to add a nice finding help: Add the lines autocmd InsertLeave *
se nocul and autocmd InsertEnter *
se cul to your .vimrc and you will get the current line underlined as soon as you enter insert mode.
Regex driven commands
Regular expressions belong to every VIM users toolbox. What some people might not know (so didn't I) is, that you can not only do replacements with them in VIM, but also address lines you want to perform commands on. Simply do :s/<regex>/=<command> to execute the given <command> on each text portion that matches <regex>.
Magic regex
Those of you who are used to PCRE syntax (and I hope all of you PHP developers out there), will often struggle over VIMs POSIX style regex and forget toescape parenthesis and other stuff. Usually VIMs regex work in so called magic mode, which means that VIM treats only a limited set of characters as magic inside a regex (that applies e.g. to ., matching any character, and *
as a quantifier). Other characters, like (, ) and | are per default not considered magic and have to be escaped to make them magic (i(, ) and |). While this might be convenient insome cases (like simple text replacements without back-references), it becomes quite ugly, if you need to refer to a lot of capturing parenthesis. For this cases, it makes sense to switch VIM into very magic mode, using v at the beginning of your regex. The opposite of this switch is V, which makes the regex very nomagic. Because VIM is normally in magic mode (which corresponds to setting m, if you change the default behaviour), there is also a nomagic mode, which considers less, but still some, characters as magic. Some simple examples:
Error with CSV data in "csv-table" directive: ',' expected after '"'
.. csv-table::
"s/(foo)bar/1/", "does not work, () must be escaped"
"s/\v(foo)bar//1/", "does work, regex is considered very magic"
"s/.*bar$/foobar/", "replaces a line with foobar, that ends on bar"
"s/\V.*bar$/foobar/", "replaces a line with foobar, contains literaly ".``*``\bar$""
This were quite many tips, especially for unexperienced VIM users. I recommend to not try to learn them all at once. Pick out 2-3 tips that you consider very important for your daily work and start getting used to them. When you are sure, you use them every time it makes sense, grab the next 2-3 and so on. I also recommend to every programmer out there, to read a work I personally call "Bible of text editing", written by Bram Moolenaar, the author of VIM itself. These 7 tips really make sense and explain quite simple, why I personally love to use VIM.
I hope the above shown tips help some people to make their daily work a bit more efficient and I also hope that I will manage to set up a new website soon, so I can consolidate all of my VIM tips in 1 central place. So long you will have to use my blog to find them again, so here is the whole collection of my "Comfortable PHP editing with VIM" series:
Part -1- Part -2- Part -3- Part -4-
Note, that in the links in earlier editions of this series might be broken. Please refer to the latest versions of the links, contained in this article!
Have fun! :)
Comments