As I move over to vim as my full-time text editor, I am constantly reminded how much there is to know about vim, and how very, very little I currently know.
BUT each week I get a little bit better.
Here’s what I’ve learned this week.
Smooth screen scroll
For years, I’ve been
k‘ing (cursor up/down) and
ctrl-u‘ing (half-page up/down) my way through files.
This week I learned that
ctrl-y will “smooth scroll” the screen up/down one line at a time, without moving the cursor.
Simple? Sure, but also rather useful.
Use normal mode to edit many lines
Needing to perform an action across multiple lines of code is fairly common occurrence.
My preferred way to do this has quickly become to select multiple lines in visual mode (
k‘ing to highlight lines) then running a command in
To run the command
A, across each selected line, type:
This command will:
- Move to the end of each line
- Enter insert mode
- Write a comma
- Exit insert mode
Set syntax of a buffer
Sometimes you want to open up a tab and work with some throwaway code. This tip lets you highlight syntax without saving the file to disk: http://stackoverflow.com/questions/3853028/how-to-force-vim-to-syntax-highlight-a-file-as-html
html above could be replaced with
Replace tabs with spaces
I really, really dislike hard tabs in source code. So anytime I find a file containing them, my first priority is to replace them with spaces.
Vim’s search/replace within a buffer is still a bit shakey for me, so I really enjoyed this tip: http://stackoverflow.com/questions/426963/replace-tab-with-spaces-in-vim
expands hard tab characters as defined by your
I also use this snippet in my
~/.vimrc to easily spot hard tabs:
Remove trailing whitespace on save
Trailing whitespace at the end of a line is sloppy. It can lead to frustrating git diffs that serve no purpose, and is just bad practice.
Adding the following to your
~/.vimrc will remove trailing whitespace with every save!
Working on a rails project I commonly have several tabs open with multiple split screens on each tab. Until now I’ve been
:q‘ing to close each buffer.
Another simple trick I learned this week closes all buffers with a single command.
When it’s time to go home for the night, running:
will close all open buffers at once.
Sometimes it’s the little tricks that help the most.
Awesome plugin: ctrl-p
Fuzzy file finders have become my go-to way for opening files across a project path using sublime text, so I was happy to find an even better plugin for vim.
The Ctrl-P plugin allows you to type
ctrl-p then a few characters that fuzzy find a file somewhere in your working directory.
For instance typing
controlluser will populate a list of matching files as you type with:
app/controllers/users_controller.rb highlighted. (Change the highlighted file in the list with
ctrl-k to select the file above/below respectively.)
What makes this setup better than fuzzy finding in other text editors is that once highlighted, files can be opened in several ways. I can open the file in:
- The current buffer by pressing
- A new horizontal split with
- A new vertical split with
- A new tab with
If a file is already open in any tab/split, Ctrl-P will take you to that buffer.
So, that’s what I’m committing to memory this week. If you would like to share tips, hit me up on twitter @vormwald!
Image credit: Ted Naleid