Just a list of handy Vim commands and tricks that I find helpful. A lot of these tricks I pick up from follow @vimtips on Twitter.
If you use Vim then chances are you've started editing a file as a user that you needed to be user to edit and now you are unable to save it. The following command will let you save you file with root permissions without having to shut down you Vim session and re-edit the file.
:w !sudo tee %
Read another file and insert it into the file you are working on
:r filename
Move a range of lines to another location. This command will move lines 40 through 50 to line 30
:40,50m30
Open current buffer in new tab
CTRL-w s CTRL-w T
Re-tab all files in the buffers list
:tab sball
Remove trailing spaces at the end of a line
:%s/\s\+$//
Move split windows around
Switch vertical split into horizontal
CTRL-w K
Switch a horizontal into a vertical
CTRL-w H
To change everything between your quote (") from inside a " "
ci "
Have you ever wanted to grab a "block" of code. A section of a document that runs through entire document but not include the full line. Vim allows you to do this.
CTRL-q
The navigate what code you want to select with:
- H - Left
- I - Down
- J - Up
- L - Right
Now copy or delete as preferred.
Here is an example of what it looks like
Display each line containing 'search_term' with line numbers
:g/search_term/#
To edit and run a previous command, type q: find the command, edit it, and Enter to execute the line.
When editing a old file in VIM you hate to come across this
This happens when a file has been edited using some other the tools on a Windows machine. To clean this up just run
:%s/\r/\r/g
and your code should magically turn into something more like
That little trick was thanks to @johncongdon