[vim] Simple Vim commands you wish you'd known earlier

I'm learning new commands in Vim all the time, but I'm sure everyone learns something new once in a while. I just recently learned about this:

zz, zt, zb - position cursor at middle, top, or bottom of screen

What are some other useful or elegant commands you wish you'd learned ages ago?

This question is related to vim

The answer is


:Te[xplore]

Tab & Explore (does a tabnew before generating the browser window)


:b [any portion of a buffer name] to switch buffers. So if you have two buffers, "somefile1.txt", and "someotherfile2.txt", you can switch to the second with simply ":b 2.t<enter>". It also supports tab completion, although it's not required.

Speaking of tab completion, the setting :set wildmode=full wildmenu is also very helpful. It enables complete tab completion for command-mode, as well as a very helpful ncurses-style menu of all the possible matches when using it.


^X-F completes using filenames from the current directory. No more copying/pasting from the terminal or painful double checking.

^X-P completes using words in the current file

:set scrollbind forces one buffer to scroll alongside another. e.g. split your window into two vertical panes. Load one file in each (perhaps different versions of the same file). Do :set scrollbind in each. Now when you scroll in one, both panes will scroll together. Ideal for comparing files.


Typing a line number followed by gg will take you to that line.


For obsessive Vim configuration have a look at https://github.com/jmcantrell/dotfiles-vim


:q!

I wish I knew that before I started vi for the first time.


I know this is not completely Vim. But I find the cscope integration really good, and it helps me a lot when hacking the Linux kernel.

  • Ctrl + \, g to reach the definition of a function

  • Ctrl + \, s to find all the usages of a function, macro, or variable.


I'm surprised no-one's mentioned Vim's windowing support. Ctrl + W, S is something I use nearly every time I open Vim.


Some of my latest additions to my Vim brainstore:

  • ^wi: Jump to the tag under the cursor by splitting the window.
  • cib/ciB: Change the text inside the current set of parenthesis () or braces {}, respectively.
  • :set listchars=tab:>-,trail:_ list: Show tabs/trailing spaces visually different from other spaces. It helps a lot with Python coding.

Opening multiple files using tabs

:tabe filepath

Navigating between open files

gt and gT or :tabn and :tabp

Save the open session so that you can get back to your list of open files later

:mksession session_file_name.vim

Open a created session

vim -S session_file_name.vim

Close all files at once

:qa

Another command I learned recently

autocmd

It allows you to run a command on an event, so you could for example run the command make when you save a file using something like:

:autocmd BufWritePost *.cpp :make

ma
move cursor down
:'a,.!program

This will take all text between where you set the a mark (ma) to the current line (.), run it through program, and replace the contents of the marked region with the results. You can even use it to see the contents of a directory (for example) by making a blank line, then with cursor sitting on that line,

:.!ls

Oh, and you can set marks like this for a-z (i.e. ma), and

'a

will jump you to the position you marked as "a."

/ searches forward, and ? repeats search backwards without having to resupply search pattern.

Groovy stuff. vi is highly underrated. Once you get the hang of it, you won't ever want to use the IDE supplied editors.


:%s,/search,/replace,

You can use other characters than / (slash) to separate patterns for substitution. This way you don’t have to escape the slashes of file paths.


q<letter> - records a macro.

and

@<same-letter> - plays it back.

These are by far the most useful commands in Vim since you can have the computer do a whole lot of work for you, and you don't even have to write a program or anything.


:x #(Save and Quit a File)

Same as :wq or ZZ


ZZ (works like :wq)

And about the cursor position: I found that a cursor which always stays in the middle of screen is cool -

set scrolloff=9999

qx will start recording keystrokes. You can do pretty much any editing task and Vim remembers it. Hit q again when you're finished, and press @x to replay your keystrokes. This is great for repetitive edits which are too complex to write a mapping for. You can have many recordings by using a character other than x.


Build and debug your code from within Vim!

Configuration

Not much, really. You need a Makefile in the current directory.

To Compile

While you're in Vim, type :make to invoke a shell and build your program. Don't worry when the output scrolls by; just press Enter when it's finished to return to Vim.

The Magic

Back within Vim, you have the following commands at your disposal:

  1. :cl lists the errors, warnings, and other messages.
  2. :cc displays the current error/warning message at the bottom of the screen and jumps to the offending line in your code.
  3. :cc n jumps to the nth message.
  4. :cn advances to the next message.
  5. :cp jumps to the previous message.

There are more; if you're interested, type :help :cc from within Vim.


gv starts visual mode and automatically selects what you previously had selected.


The most recent "wow" trick that I learnt is a method of doing complicated search-and-replace. Quite often in the past, I've had a really complicated regexp to do substitutions on and it's not worked. There is a better way:

:set incsearch             " I have this in .vimrc
/my complicated regexp     " Highlighted as you enter characters
:%s//replace with this/    " You don't have to type it again

The "trick" here (for want of a better word) is the way that you can use the search to create the regexp (and 'incsearch' highlights it as you enter characters) and then use an empty pattern in the substitution: the empty pattern defaults to the last search pattern.

Example:

/blue\(\d\+\)
:%s//red\1/

Equivalent to:

:%s/blue\(\d\+\)/red\1/

See:

:help 'incsearch'
:help :substitute

^P and ^N

Complete previous (^P) or next (^N) text.

^O and ^I

Go to previous (^O - "O" for old) location or to the next (^I - "I" just near to "O").

When you perform searches, edit files, etc., you can navigate through these "jumps" forward and back.

Marks

Press ma (m- mark, a - name of mark). Later to return to the position, type `a.


This always cheers me up:

:help 42

I wish I'd known basic visual block mode stuff earlier. Even if you don't use Vim for anything else, it can be a big time saver to open up a file in Vim just for some block operations. I'm quite sure I wasted a ton of time doing this kind of thing manually.

Examples I've found particularly useful, when, say, refactoring lists of symbolic constant names consistently:

Enter Visual Block mode (Ctrl + Q for me on Windows instead of Ctrl + V)

Move the cursor to highlight the desired block.

Then, I whatever text and press Esc to have the text inserted in front of the block on every line.

Use A instead of I to have the text inserted after the block on every line.

Also - simply toggling the case of a visual selection with ~ can be a big time saver.

And simply deleting columns, too, with d of course.


Comment out a range of lines:

  1. First set a bookmark at the beginning of range: ma

  2. Go the the last line in range

  3. The command is:

    :'a,.s/^/# /
    

    Assuming # is your comment character.


The asterisk key, *, will search for the word under the cursor.

[+Tab will take you to the definition of a C function that's under your cursor. (It doesn't always work, though.)


Taking xcramps' suggestion one step further, I can't tell you how many times I've used:

:%!sort

to sort a list of items in a file.

Details:

:range!command

will execute a shell command on the specified range of lines. A range is usually specified as start,end

Examples:
1,3 specifies the first 3 lines
'a,'b selects the text between bookmarks a and b
.,$ selects the entire document (. = first line; $ = last line)
% is a shortcut for .,$ and also selects the entire document.

Feel free to mix and match numbers, bookmarks, ., and $.


I really wish I'd known that you can use CtrlC instead of Esc to switch out of insert mode. That's been a real productivity boost for me.


cw

Change word - deletes the word under the cursor and puts you in insert mode to type a new one. Of course this works with other movement keys, so you can do things like c$ to change to the end of the line.

f + character

Finds the next occurrence of the character on the current line. So you can do vft to select all the text up to the next "t" on the current line. It's another movement key, so it works with other commands too.


I would have to say that one of my favorites is putting the help window in a new tab:

:tab help <help_topic>

This opens up help in a new tab and, as somebody that loves Vim tabs, this is ridiculously useful.


In our software shop, variable declarations need to be sorted. In the language that we use, multiple variables can appear on the same line.

new var1,var2,var3,etc

It is a real pain to go through and visually attempt to sort each and every variable. The block highlighting, and sort command in Vim are my friends here:

  1. Move the cursor to the first variable to be sorted.
  2. Issue the v command to enter visual mode.
  3. Move the cursor to the end of the last variable to be sorted in my case I enter $ to go to the end of the line).
  4. Execute the !sort command to tell Vim to sort the highlighted text.

This will only work if their exists a 'sort' command on the underlying system.


Press % when the cursor is on a quote, parenthesis, bracket, or brace to find its match.


vimcryption

vim -x filename.txt

You will be asked for a passphrase, edit and save. Now whenever you open the file in vi again you will have to enter the password to view.


^r^w to paste the word under the cursor in command mode.

It is really useful when using grep or replace commands.


set confirm allows you to quit Vim gracefully with :q. You don't need to use ZZ or other heavy-handed mechanisms which blindly save or discard all changes.


  1. Don't press Esc ever. See this answer to learn why. As mentioned above, Ctrl + C is a better alternative. I strongly suggest mapping your Caps Lock key to escape.

  2. If you're editing a Ctags compatible language, using a tags file and :ta, Ctrl + ], etc. is a great way to navigate the code, even across multiple files. Also, Ctrl + N and Ctrl + P completion using the tags file is a great way to cut down on keystrokes.

  3. If you're editing a line that is wrapped because it's wider than your buffer, you can move up/down using gk and gj.

  4. Try to focus on effective use of the motion commands before you learn bad habits. Things like using 'dt' or 'd3w' instead of pressing x a bunch of times. Basically, any time that you find yourself pressing the same key repeatedly, there's probably a better/faster/more concise way of accomplishing the same thing.


^y will copy the character above the cursor.


gi switches to insertion mode, placing the cursor at the same location it was previously.


Until [character] (t). It is useful for any command which accepts a range. My favorite is ct; or ct) which deletes everything up to the trailing semicolon / closing parentheses and then places you in insert mode.

Also, G and gg are useful (Go to bottom and top respectively).


I created this reference of my most used command for a friend of mine:

select                                   v
select row(s)                            SHIFT + v
select blocks (columns)                  CTRL  + v
indent selected text                     >
unindent selected text                   <
list buffers                             :ls
open buffer                              :bN (N = buffer number)
print                                    :hardcopy
open a file                              :e /path/to/file.txt
                                         :e C:\Path\To\File.txt
sort selected rows                       :sort
search for word under cursor             *
open file under cursor                   gf
  (absolute path or relative)
format selected code                     =
select contents of entire file           ggVG
convert selected text to uppercase       U
convert selected text to lowercase       u
invert case of selected text             ~
convert tabs to spaces                   :retab
start recording a macro                  qX (X = key to assign macro to)
stop recording a macro                   q
playback macro                           @X (X = key macro was assigned to)
replay previously played macro *         @@
auto-complete a word you are typing **   CTRL + n
bookmark current place in file           mX (X = key to assign bookmark to)
jump to bookmark                         `X (X = key bookmark was assigned to
                                             ` = back tick/tilde key)
show all bookmarks                       :marks
delete a bookmark                        :delm X (X = key bookmark to delete)
delete all bookmarks                     :delm!
split screen horizontally                :split
split screen vertically                  :vsplit
navigating split screens                 CTRL + w + j = move down a screen
                                         CTRL + w + k = move up a screen
                                         CTRL + w + h = move left a screen
                                         CTRL + w + l = move right a screen
close all other split screens            :only

*  - As with other commands in vi, you can playback a macro any number of times.
     The following command would playback the macro assigned to the key `w' 100
     times: 100@w

** - Vim uses words that exist in your current buffer and any other buffer you may have open for auto-complete suggestions.

I often make functions for editing tasks and store them in my .vimrc file, so I can find them again.

For example, reading .NET callstacks that have been converted into a single line:

function! FixCallStacks()
:%s;\[NLN\];\r;g
:%s;\[TAB\];\t;g
endfunction

Tabbed interface

Apart from split windows, you also can have tabbed windows. In the escape mode, type tabnew. You can open multiple tabs like this. To navigate between tabs, type tabn. This will move to the next tab. To move to a tabbed window, type tabn2 to move the second tab and so on.

To close a tab, type tabc, tabclose, or just close.

If you are in a terminal emulator (basically terminal in GUI) then you can try doing set mouse=a. Once that's done, you can click inside the editor with your mouse. And this will also help you navigate between tabs by clicking, and also closing by clicking on the close button at the right side.

Align your code - Full file

Just type G=gg in the escape mode.

Fold your code

Say you have a function that is completed. You want to minimise (or fold) that part of code so that you can free up some space and reduce clutter. Just select the code. Then, type fold.

This will fold the code up. If you want to expand the code, just go there, and type zo. To fold again, type zc.


:qall and :wqall to close all the split screens.


You can use a whole set of commands to change text inside brackets / parentheses / quotation marks/ tags. It's super useful to avoid having to find the start and finish of the group. Try ci(, ci{, ci<, ci", ci', ct depending on what kind of object you want to change. And the ca(, ca{, ... variants delete the brackets / quotation marks as well.

Easy to remember: change inside a bracketed statement / change a bracketed statement.


I just discovered this one while browsing the Vim help:

:help index

This takes you to a single (searchable!) page with all the commands for all the modes.

So if you know that the command you're trying to remember/learn starts with or involves a certain keystroke, you can search for that and flip through all the possibilities. Or you can just browse the mode you're interested in to see/learn editing possibilities.

I can't count the number of times I've done just :help Ctrl + R or whatever and gotten just the first thing, which is of course never the one you want. This is much better than :helpgrep IMO.

And into the .vimrc it goes:

nnoremap <silent> <F1> :help normal-index<CR>
inoremap <silent> <F1> <C-O>:help insert-index<CR>

:help still gets you to the default F1 page.


Use Vim bindings on the command line in Bash:

    set -o vi

In other, readline-using programs, hit Ctrl + Alt + J to switch from Emacs to Vim bindings.


:shell to launch a shell console from Vim. Useful when for example you want to test a script without quitting Vim. Simply hit ^d when you done with the shell console, and then you come back to Vim and your edited file.