2022 Developer Survey is open! Take survey.
116 votes
Accepted

How can I easily move a line?

First, you do not need to yank and delete; the latter will also put the deleted contents into the (default or specified) register. Therefore, ddp / ddkP are common commands to move a line one down / ...
user avatar
  • 16.6k
105 votes
Accepted

Close and unload a buffer without exiting Vim

There is command to do exactly that: :bdelete or just :bd. By default it will unload current buffer. To unload other buffer, first get the list of all buffers with :buffers command, and after that you ...
user avatar
  • 1,881
83 votes
Accepted

How to open multiple files in split mode?

You can use the option -o to open the files in horizontal splits or -O (letter "O") to open vertical splits. The following commands open a window for each file specified: vim -o *.cpp vim -O ...
user avatar
  • 4,452
78 votes

How can I easily move a line?

The easiest is: :m+ or :m-2 which is abbreviation for :move as Ingo suggested. Or using visual mode (V) by cutting the line (d/x) then paste it (p - below cursor, P above cursor) after you moved your ...
user avatar
  • 16.5k
68 votes
Accepted

Close all split files, except the one currently focused?

If you want to close all windows(splits) except the current one: :only If you want to close all tabs except the current one: :tabonly If you want to delete all hidden buffers (files open but not ...
user avatar
  • 2,818
57 votes
Accepted

Dump the output of internal vim command into buffer

You can use :redir to redirect the output to a variable, register, or file. Example of redirecting to the unnamed register: :redir @">|silent scriptnames|redir END|enew|put Alternatively Tim Pope'...
user avatar
49 votes
Accepted

Execute current buffer as bash script from vim

I wrote a pretty extensive answer about this over on stack-overflow. The basic idea is that since the write command is about writing not saving, you can write the text in your buffer into an external ...
user avatar
  • 16.5k
46 votes

Close all split files, except the one currently focused?

Press Ctrl+w,o to quickly close all split windows, but current one. Alternatively use the command: :on (:only). :on[ly][!] Make the current window the only one on the screen. All other ...
user avatar
  • 16.5k
42 votes

Close and unload a buffer without exiting Vim

You can completely wipe out a buffer using the :bwipeout (or :bw) command. This completely removes the buffer from memory, including any marks, option settings, etc. that you have added to it. ...
user avatar
  • 521
38 votes
Accepted

How to go to previous open buffer from Vim command line?

The jumplist is not the best way to do buffer navigation. Vim has a great number of buffer navigation tools which are probably better used for this task. <c-^>/<c-6> - jump to the ...
user avatar
36 votes
Accepted

How to edit content from the standard input?

You have to use - in place of the file name on the command line: echo This is example. | vim - The above command will open an unnamed buffer filled with the text read from the standard input.
user avatar
  • 4,452
36 votes
Accepted

How to edit files non-interactively (e.g. in pipeline)?

To edit file non-interactively using ex (vi is the visual mode for ex), you can use +{command} or -c {command} parameters which allows you to execute the vi commands after the first file has been read....
user avatar
  • 16.5k
29 votes

How can I easily move a line?

If I want to swap one line with the line above I usually do the following ddkP Explanation dd will delete the line and add it to the default register. k will move up a line (j would move down a ...
user avatar
  • 391
27 votes
Accepted

Execute normal command over range

You can do this with the normal command : :1,10normal d2w This is because the d operator doesn't accept a range, but only a motion : :h d ["x]d{motion} Delete text that {motion} moves ...
user avatar
  • 15k
26 votes
Accepted

How do I use relative line numbers in command line mode?

Assuming your lines span from 15 to 10 lines above the current one, you can achieve what you requested using relative line numbers: :-10,-15m. Unfortunately when specifying a backwards range, Vim ...
user avatar
  • 3,392
24 votes
Accepted

How to save a file for which I have no write permissions?

The trick is to use an external call to sudo: :w !sudo tee % How this works: :w !<command> executes <command> with the contents of the buffer as stdin. tee duplicates stdin to a file &...
user avatar
24 votes
Accepted

How do I stop recording while in command line?

Another solution is when in command line mode, use Ctrl-f, and press q to stop recording. Ctrl-f allows you to open the command line window, in which you can edit the command in normal mode. To ...
user avatar
  • 356
23 votes

How to split a line into multiple lines efficiently?

You could simply replace any consecutive space on the line with a newline using the command :s/\s\+/\r/g. \s\+ mean one or more whitespace characters, and \r is a newline, see :help :substitute and :...
user avatar
  • 4,452
21 votes
Accepted

What is the '[Command Line]' that sometimes comes up when I try to quit, and how can I quickly exit it?

You have invoked it by pressing q: which opens a new window and allows you to write an Ex command. That is why it's called the Command Line You can read up on it by typing :help q:. As it is a ...
user avatar
  • 461
20 votes

How do I stop recording while in command line?

One way to do it: start your macro as usual (e.g. qq ... other chars) when you reach the cmdline-mode, write all the characters you want you want to end the macro here. Hit ESC to get out of cmdline-...
user avatar
  • 9,260
20 votes
Accepted

When to use :grep and :vimgrep in vim?

As the doc say: Vim has two ways to find matches for a pattern: Internal and external. In a nutshell, :vimgrep uses Vim's grep mechanism to read and find in files. :grep uses an external commands to ...
user avatar
  • 15k
19 votes
Accepted

How to work with command-line mode the vi way?

There's the command-line (that you know), and there's the command-line window (the command-line in a special buffer, which you can edit just like any other). You enter this either via <C-f> from ...
user avatar
  • 16.6k
19 votes
Accepted

Easiest way to switch git branches

To summarize the answers, here are the ways to accomplish this: Vanilla Terminal Vim in a shell with job control Press CTRL-Z in normal mode will drop you back in to the shell and suspend Vim. After ...
user avatar
  • 1,142
18 votes
Accepted

Bash-like partial tab-complete for filenames in Vim

You can use the wildmode option to control this. If you use :set wildmode=list:longest, you get behaviour similar to bash. I personally prefer to use :set wildmode=list:longest,full; this will list ...
user avatar
18 votes
Accepted

Can I search through 'oldfiles' with a specific name or extension?

Assuming you have Vim 8 you can use :filter :browse filter /pattern/ oldfiles For more help see: :h :browse :h :filter :h oldfiles :h new-items-8
user avatar
17 votes
Accepted

Is there a version of Vim or plugin with a Command Mode auto completion?

I really don't think that having autocompletion or not will help you learning how to use Vim. Your main problem in my opinion is to learn the basics of the editor and then learn more and more commands....
user avatar
  • 45.9k
17 votes
Accepted

How do I use a variable or return value in option, command, or mapping?

The reason your example attempts don't work is that in many locations text is simply seen as a literal string, rather than VimScript. So functions, variables, and the like don't work. For example, if ...
user avatar
  • 20.6k
16 votes

How to work with command-line mode the vi way?

In addition to providing the command-line window, Vim also offers some limited features allowing you to interact with the command-line whilst in command-line mode (without using the arrow keys). ...
user avatar
  • 27.4k
16 votes
Accepted

How to substitute in multiple lines?

Performing a substitution on several consecutive lines is pretty easy: :2,11s/^/word / but a range can't cover non-consecutive lines. With a bit of creativity, though, it is entirely possible to ...
user avatar
  • 32.6k
16 votes

vim doesn't show the command that is being typed

This behaviour is controlled by the showcmd option. Try: :set showcmd and see if that brings it back.
user avatar
  • 3,392

Only top scored, non community-wiki answers of a minimum length are eligible