Practical Vim
Metadata
- Author: Drew Neil
- Full Title: Practical Vim
Highlights
The d{motion} command can operate on a single character (dl), (Location 1186)review
! Filter {motion} lines through an external program (Location 1204)review
The commentary command is triggered by gc{motion}, which toggles commenting for the specified lines. It’s an operator command, so we can combine it with all of the usual motions. gcap will toggle commenting for the current paragraph. gcG comments from the current line to the end of the file. gcc comments the current line. (Location 1228)review
Kana Natsuno’s textobj-entire plugin is a good example.[5] It adds two new text objects to Vim: ie and ae, which act upon the entire file. (Location 1236)review
Note that if we had both the commentary and textobj-entire plugins installed, we could use them together. Running gcae would toggle commenting throughout the current file. (Location 1244)review
Expert typists recommend drastic measures: delete the entire word; then type it out again. If you can type at a rate above sixty words per minute, retyping a word from scratch will only take a second. If you can’t type that fast, consider this to be good practice! There are particular words that I consistently mistype. Since I started following this advice, I’ve become more aware of which words trip me up. As a result, I now make fewer mistakes. (Location 1285)review
The gv command is a useful little shortcut. It reselects the range of text that was last selected in Visual mode. (Location 1575)review
We can use the o key to toggle the free end. This is really handy if halfway through defining a selection we realize that we started in the wrong place. Rather than leaving Visual mode and starting afresh, we can just hit o and redefine the bounds of the selection. (Location 1599)review
The Visual mode U command has a Normal mode equivalent: gU{motion} (gUⓘ). If we use this to make the first change, we can complete the subsequent edits using the Dot Formula: (Location 1729)review
We can use the . symbol as an address to represent the current line. So, we can easily compose a range representing everything from here to the end of the file: ⇒ :2 ⇒ :.,$p ⇐ 2 3
Practical Vim
5 (Location 2101)reviewThe % symbol also has a special meaning—it stands for all the lines in the current file: (Location 2112)review
If we press the : key now, the command-line prompt will be prepopulated with the range :’<,’>. It looks cryptic, but you can think of it simply as a range standing for the visual selection. Then we can specify our Ex command, and it will execute on every selected line: (Location 2138)review
’< is a mark standing for the first line of the visual selection, while ’> is the last line of the visual selection (see Tip 54, for more about marks). These marks persist even when we leave Visual mode. If you try running :’<,’>p straight from Normal mode, it will always act on the lines that most recently formed a Visual mode selection. (Location 2149)review
The . symbol stands for the current line, so :.,.+3 is equivalent to :2,5 in this case. Discussion (Location 2185)review
Symbol Address 1 First line of the file $ Last line of the file 0 Virtual line above first line of the file . Line where the cursor is placed ’m Line containing mark m ’< Start of visual selection ’> End of visual selection % The entire file (shorthand for :1,$) (Location 2189)review
Remember that the ’<,’> range stands for the visual selection. We could easily make another visual selection and then repeat the :’<,’>m$ command to move the selected text to the end of the file. Repeating the last Ex command is as easy as pressing @: (see Tip 31, for another example), so this method is more easily reproducible than using Normal mode commands. (Location 2313)review
:’<,’>normal . (Location 2365)review
By default, Vim records the last twenty commands. With memory becoming ever cheaper in today’s computers, we can probably afford to up this limit by changing the ‘history’ option. Try adding this line to your vimrc: set history=200 (Location 2555)review
From Vim’s Command-Line mode, we can invoke external programs in the shell by prefixing them with a bang symbol (see :!ⓘ (Location 2628)review
On Vim’s command line, the % symbol is shorthand for the current file name (see cmdline-specialⓘ). We can exploit this to run external commands that do something with the current file. (Location 2642)review
Suppose that we’re running Vim inside a bash shell and we want to execute a series of shell commands. Pressing Ctrl-z suspends the process that’s running Vim and returns control to bash. The Vim process sits idle in the background, allowing us to interact with our bash session as normal. We can inspect the list of jobs by running this command: ⇒ $ jobs ⇐ [1]+ Stopped vim In bash, we can use the fg command to resume a suspended job, bringing it back into the foreground. That brings Vim back to life exactly as we left it. (Location 2672)review
The ** wildcard also matches zero or more characters, but it can recurse downward into directories below the specified directory (starstar-wildcardⓘ (Location 3022)review
:e[dit]! Read the file from disk back into the buffer (that is, revert changes) :qa[ll]! Close all windows, discarding changes without warning :wa[ll] Write all modified buffers to disk (Location 3130)review
The file we want to open is in the same directory as our active buffer. It would be ideal if we could use the context of the active buffer as a reference point. Try this: ⇒ :edit %
Try sourcing this line in your vimrc file: cnoremap
If we use the cut or copy command to capture text in an external application, then we can paste it inside Vim using “+p command (Location 4926)review
Register Contents ”% Name of the current file ”# Name of the alternate file “. Last inserted text ”: Last Ex command ”/ Last search pattern (Location 4955)review
Keystrokes Buffer Contents {start} I like chips and fish. fc I like chips and fish. de I like and fish. mm I like and fish. ww I like and fish. ve I like and fish. p I like and chips. `m I like and chips. P I like fish and chips. (Location 5005)review
I sometimes prefer to paste character-wise regions of text from Insert mode using the
It’s worth noting that Vim also provides gp and gP commands. These also put the text before or after the current line, but they leave the cursor positioned at the end of the pasted text instead of at the beginning. The gP command is especially useful when duplicating a range of lines, (Location 5107)review
Occasionally useful, the gea command can be read as “append at the end of the previous word.” (Location 3781)review
A word consists of a sequence of letters, digits, and underscores, or as a sequence of other nonblank characters separated with whitespace (see wordⓘ). The definition of a WORD is simpler: it consists of a sequence of nonblank characters separated with whitespace (see WORDⓘ). (Location 3787)review
deleting the last clause of a sentence is something we do often enough that we can treat f,dt. as a finger macro. (Location 3964)review
In general, I tend to use f{char} and F{char} in Normal mode when I want to move the cursor quickly within the current line, whereas I tend to use the t{char} and T{char} character search commands in combination with d{motion} or c{motion}. (Location 3966)review
When we press vi}, Vim initiates Visual mode and then selects all of the characters contained by the {} braces. Where the cursor is positioned to begin with doesn’t matter so long as it’s located somewhere inside a block of curly braces when the i} text object is invoked. We can expand the selection again using another text object. For example, a” selects a range of characters delimited by double quotes. i> selects everything inside a pair of angle brackets. Vim’s text objects consist of two characters, the first of which is always either i or a. In general, we can say that the text objects prefixed with i select inside the delimiters, whereas those that are prefixed with a select everything including the delimiters. As a mnemonic, think of i as inside and a as around (or all). (Location 4142)review
a) or ab A pair of (parentheses) i) or ib Inside of (parentheses) a} or aB A pair of {braces} i} or iB Inside of {braces} a] A pair of [brackets] i] Inside of [brackets] a> A pair of A pair of
backticks i
Inside of backticks
at A pair of
“ Position before the last jump within current file ‘. Location of last change ‘^ Location of last insertion ‘[ Start of last change or yank ‘] End of last change or yank ‘< Start of last visual selection ‘> End of last visual selection (Location 4306)review
In web browsers, we’re used to using the back button to return to pages that we visited earlier. Vim provides a similar feature by way of the jump list: the
(/) Jump to start of previous/next sentence (Location 4462)review
{/} Jump to start of previous/next paragraph (Location 4463)review
gf Jump to file name under the cursor (Location 4467)review
C-]> Jump to definition of keyword under the cursor (Location 4468)review
To jump back to the most recent modification in the document, we press g;. That places the cursor back on the line and column where it ended up after the previous edit. The result is the same as if we had pressed u
The :normal @a command tells Vim to execute the macro once for each line in the selection. Just as before, the macro succeeds on the first two lines and then aborts on line three, but it doesn’t stall there this time—it completes the job. Why? Previously, we queued up five repetitions in series by running 5@a. When the third iteration aborted, it killed the remaining items in the queue. This time, we’ve lined up five iterations in parallel. Each invocation of the macro is independent from the others. So when the third iteration fails, it does so in isolation. (Location 5565)review
Let’s stake out the terrain by building a list of the files that we want to act upon. We’ll keep track of them using the argument list (for more details, see Tip 38): ⇒ :cd code/macros/ruby_module ⇒ :args *.rb Running :args without arguments reveals the contents of the list: ⇒ :args ⇐ [animal.rb] banker.rb frog.rb person.rb (Location 5660)review
Vim records our search patterns so we can easily recall them. When the search prompt is visible, we can scroll through the previous searches by pressing the
Here, we search for /lang/e