I think that's probably more to people not wanting to learn how to do it right. For the longest time, I only taught myself just enough vim to be able to use it for basic editing, because it's convenient and it's better than nano.
Mine is playing with errorformat (personal blogspam: http://flukus.github.io/vim-errorformat-demystified.html) and combining this with :cf (read errors from file) or :cb (read errors from buffer) to parse a file/buffer into the quicklist. Great for dealing with log files.
`gq` will work with a text object, so you don't need to go to the trouble of visually selecting a block. Say, for example, you have a mis-wrapped paragraph in a text file, providing your cursor is anywhere within that paragraph, `gqip` will rewrap the whole paragraph.
The nice thing about using `=` instead of `gq` is that you can “set equalprg” to something smarter than the internal Vim formatter, such as `fmt` or `par`:
These formatters will do nice things like choose line breaks intelligently to reduce the raggedness of the right edge. I just tested `gqip` on a paragraph in a Markdown document here that was formatted with `fmt`: the `fmt` output put breaks in such that all but the last line was within one character of equal, whereas `gqip` had line lengths varying as much as 5 characters on a 72 character line length.
A quicker alternative to Shift-V, arrows, then `=` to reformat a paragraph with `fmt` is this:
:map <F2> !}fmt -w72<CR>
That works anywhere inside the paragraph like with your `gqip`, but it uses the smarter `fmt` paragraph formatter.
Select your keystroke to taste. I chose F2 so long ago that I forget why that was a good idea at the time.
Not really a trick of Vim itself, but when using Vim from the terminal I find ctrl-z very useful to background Vim, type in a few shell commands for git or whatever and then use "fg" to hop back into Vim.
Ctrl-Z only gets converted to SIGTSTP if the terminal has ISIG set. For example, try `stty -isig; sleep 5` and try to suspend or interrupt the sleep command.
* ctrl+z; kill %1
* Open a new terminal and ignore the one with vim running.
* ZZ
* Pull the power cord, erasing any sign of it running.
* ESC :q!
* ESC :wq
* ESC :exit
* ESC :quit
If you forgot to sudo vi, you've already edited the file, saving the file as root without first saving it as your own and then having to move it:
<ESC>:w !sudo tee %
Pipes the content of the file to a tee process as root and lets tee save it under the current file name. Vim will tell you the file has changed on disk and ask if it should reload it afterwards.
Make sure you are 100% confident that you know the proper syntax for this when you use it.
I once blanked by mistake an important file, lucky for me I had a backup.
Generally a good idea, before you sudo anything. That's one of the reasons to prefer this approach to `sudo vim`, actually - that's essentially "sudo of anything vim does", and that's a lot of very different things just a couple keystrokes away.
Macros, Macros, Macros. These are a game changer when editing large files. Use the `q` key and followed by any letter you like, I default to `a`. Perform some action, like deleting a line, appending to the start of a line, or searching for a word then deleting it. Press `esc`, then type in the number of times you want to repeat the action, followed by `@` and the letter you chose. I.E `10@a` and it will perform that action 10 times.
Vim can be great for quickly doing tedious text transformations. Far easier to get right than doing it the 'proper way' with a script in Python/Perl/whatever.
To do something like scan through a JSON or XML document and split a 'name' field/tag into 'firstName'/'lastName', Vim is the tool I'd reach for.
I think of it as 'cursor-oriented programming', but what I do with it isn't really programming, it's generally of the pattern of 1. Find start of pattern 2. Transform pattern 3. GOTO 1
Or at worst, the decorate/sort/undecorate pattern.
Also fun: macros can call themselves. So if you want to repeat something across a whole file, it's sometimes easiest to write an infinitely recursive macro and let it error when it bumps up against the bottom of the file.
A useful thing to know when using macros is that the letter you press after `q` is just the register in which Vim stores the macro. Registers are also used when copying and pasting. As such, if you want to edit a macro, you can just paste it into a new buffer (e.g., `"ap` for register `a`), edit it as you wish, then copy it back into that register (`:%y a`). You can even use this method to write macros from scratch, rather than recording them first.
`Ctrl + v` to visually select one character at a time (instead of say `Shift + v` that selects a whole line) and then use the movement keys to select a block of characters horizontally as well as vertically.
Imagine several well indented HTML <li class="something"> elements, you could for instance delete/modify all the class attributes on all lines visually in one go (to insert text use `Shift + i`, type, then Esc).
You want to change all the instances of "wrong" to "right". So you put the cursor on the first "w", then `Ctrl-V` to enter blockwise visual mode, then `e2j` to select the "wrong" words, then `c` to remove all of them and enter insert mode to type a replacement. The replacement text is then applied to each line separately. Full series of keystrokes (starting with the cursor on the "w" in the first line) if you want to follow along:
98 comments
[ 2.1 ms ] story [ 166 ms ] threadI figured this out today. And I submitted this Ask HN to find out more such things.
Even better: Map "jk" to Escape. Enter normal mode without even leaving the home keys; so much easier.
.vimrc:
- ctrl+p plugin to find files in project.
- :bn / :bp / :b<number> to switch between files (buffers)
- :bd to close file
I've mapped 11 to go to previous file, 22 to go to next file, 33 to close file.
Just '*' does this, no need for alt.
There's also `vt` + some character to select text from the cursor to the next occurrence of that character.
For instance:
Delele first 20 lines:
<ESC>:0,20d
Delete lines from current location to end of file:
<ESC>:,$d
Search and replace from regrex:
<ESC>:0,$s/regex/replacement/g
(from cursor next 20 lines) <ESC> d20d
When deleted is on your copy buffer, just prest p tu paste it from cursor.
Delete from the current line to a line with a particular line number you already see on the left if you have line numbering enabled.
dG
is a lot faster.
Copy lines 0 through 20 to the current line:
<ESC>:0,20t.
I end up using variants of this and the above multiple times every work day.
Assigning different functions to the arrow keys helps when learning vim navigation
`=` for re-indenting a selection to your current shift-width settings etc. (or `gg=G` to just do the whole file)
A quicker alternative to Shift-V, arrows, then `=` to reformat a paragraph with `fmt` is this:
That works anywhere inside the paragraph like with your `gqip`, but it uses the smarter `fmt` paragraph formatter.Select your keystroke to taste. I chose F2 so long ago that I forget why that was a good idea at the time.
Ctrl-Z only gets converted to SIGTSTP if the terminal has ISIG set. For example, try `stty -isig; sleep 5` and try to suspend or interrupt the sleep command.
:sh ENTER
or for more control over which shell, or to run a shell with any arguments, or even to run any command w/ or w/o args:
:!bash
:!bash -o vi
:!bash some_script.sh
:!some_command some args
and after any of those, just press Ctrl-D or type exit ENTER to return to the vim session.
Very fast and easy to do. I use it all the time.
You can do :wq return, or the abbreviated form of :x return, or as you say, ZZ
<ESC>:w !sudo tee %
Pipes the content of the file to a tee process as root and lets tee save it under the current file name. Vim will tell you the file has changed on disk and ask if it should reload it afterwards.
IMO, it's better not to do that in the first place (which, of course, only makes your tip all the more useful!)
:tabe newfile to open a new file.
Then, add the following to your ~/.vimrc (on MacOS)
map <Esc>f <Esc>:tabn<cr> map <Esc>b <Esc>:tabp<cr>
Now you can switch between tabs using option + arrow keys
Any tips on how to just rip recoding mode completely out of my vim?
removes the recording mode and throws a simple error when you use :Q instead of :q
will make :Q behave like :q
To do something like scan through a JSON or XML document and split a 'name' field/tag into 'firstName'/'lastName', Vim is the tool I'd reach for.
I think of it as 'cursor-oriented programming', but what I do with it isn't really programming, it's generally of the pattern of 1. Find start of pattern 2. Transform pattern 3. GOTO 1
Or at worst, the decorate/sort/undecorate pattern.
But it boils down to repeatedly applying a macro.
Example:
(1) Select block with `C-v`
(2) Hit colon `:`
(3) Write norm and enter any sequence as you would be in normal mode: `norm I# ` (prepend selection with a comment sign)
(4) Admire the result.