87

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 can specify the number after :bd to remove it. Also :bd + space + tab allows completion using buffer name.


73

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 / up. Alternatively, you can use the :move command, i.e. :move +1 / :move -2; this doesn't clobber the register, and you can also move entire ranges. You can ...


64

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 foo bar baz You can tell Vim the maximum number of windows to open by putting an integer after o or O options, the following example will open at most two ...


49

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 visible in any window on any tab), you'll have to add a function to your vimrc. Some people have posted possible functions to here and here I'm not sure exactly ...


49

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 cursor to the right place before pasting (so in summary it's Vxp/Vdp). When moving multiple lines in visual mode, then you've to use :m '>+1 (to move one ...


43

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 program, such as python or bash. In your case, you would want to do something like: :w !bash or :w !sh These commands literally just write the text in your ...


38

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. Similarly, :bdelete (or :bd) removes the buffer, but leaves it in memory and keeps marks and option settings. As per the comment by Tom Hale, the Vim documentation ...


35

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 windows are closed. See also: How to convert all windows into horizontal, vertical or tab splits? at Vim SE How to cancel splitted windows? at stackoverflow SE Is ...


35

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's scriptease.vim provides the :Scriptnames command which will load :scriptnames into the quickfix list and :copen. If you find yourself redirecting many ...


34

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. The ex is a standard command-line editor (similar to ed). There is also vipe (a Vim command pipe editor) should be used which is part of moreutils package ...


26

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.


24

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 & stdout; % expands to the current filename.. You prefix this with sudo for root permissions. You're not really saving the file with Vim, rather, you're ...


23

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 asks you to confirm if that is what you really wanted. To avoid the confirmation step, you can type silent before your command, or just specify a forwards range: ...


22

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 :help regexp for details. If you run the command on a line like: foo bar baz You get: foo bar baz If you have hlsearch enabled and you don't want to clutter ...


22

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 alternative file or if providing a count to a certain buffer number. See :h CTRL-6 :b - jump to a specific buffer. Takes buffer number or partial filename. See :h :b :...


20

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 window you can simply exit it as any other window, notably :q written correctly.


19

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-mode, q to end your macro. remove the ESC from your q register (*): you can paste it in a buffer, remove the ^[ character, select the whole stuff again (...


18

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 the command-line (also for / searches etc.) when you feel the need for more "editing power", or directly from normal mode via the dedicated q:, q/, etc. ...


18

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 over [into register x]. Alternatively you can select your text in visual mode and you can do : :'<,'>normal d2w


18

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 validate the command, simply use enter. Therefore, after having added something like: cnoremap <c-q> <c-f>i<c-o>q in your .vimrc, you can use &...


17

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 line) P will paste above the current line


17

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 achieve the same job. The / search is for the current buffer only, whereas :[vim]grep search through a pattern of files, allowing you to search in an entire ...


16

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 completions, but on the second Tab press, you can cycle between the entries (and Shift+Tab to cycle in the reverse). This applies to all completions done by ...


16

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 work around that "limitation". Indeed, you can repeat the last substitution with :& or :&& (the former will not preserve the original flags, the ...


15

This behaviour is controlled by the showcmd option. Try: :set showcmd and see if that brings it back.


15

Seeing '<,'> in the command line when you press : indicates that you currently have a visual range selected (e. g. with v, V, or Ctrl-v), and vim is helpfully prefixing the markers for 'beginning of selection' through 'end of selection' in order to apply those limitations to the scope of the command you are presumably about to enter. See :h visual-...


14

While you cannot define custom commands starting with lowercase characters (like the built-in ones), nothing prevents you from doing the opposite, defining user commands that mirror the built-in ones. :command! -nargs=* -complete=option Set set <args> If you do this for additional commands, just ensure the number of arguments / taken :range / bang ([!...


14

This is happening because the OS's vi is ahead of Homebrew vi in the PATH. While you could fix it by putting /usr/local/bin ahead of /usr/bin in the PATH, that would be a security hole since Homebrew gives ownership of that directory to your user. That permission change from the macOS default means that even an extremely unsophisticated malware could use ...


14

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. Anyway Vim has a built-in auto completion feature for the command line it is controlled with the wildmenu and wildmode options. The first step is to create ...


14

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


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