74

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.


55

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 ...


42

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 ...


40

You can use the option -o to open the files in horizontal splits or -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 windows no matter how ...


36

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 ...


35

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 ...


33

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 ...


30

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 ...


27

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 ...


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 ...


24

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 ...


23

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.


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: ...


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.


18

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 ...


17

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. ...


17

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 (...


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

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 ...


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

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


13

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


13

You can use the :TOhtml command that ships with Vim to create an HTML representation of the (syntax highlighted) buffer (or parts of it). Then, your only requirement on the target system would be a browser to display the captured file. See :help :TOhtml for details. If you insist on outputting ANSI escape sequences, you could probably modify the plugin to ...


13

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


13

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


12

I use the following mapping in my .vimrc, which I find useful: cnoremap w!! w !sudo tee % It's easy to remember, because w is "write," w! is "force write," and w!! is "super-duper-force write." :P


12

You can use hard numbers in your range: :200,300command Or relative numbers: :-27,+46command Or manual marks: :'a,'bcommand Or automatic marks: :'[,'>command Or searches: :?foo?,/bar/command Or line shortcuts: :.,$command Or any combination of the tricks above: :?foo?,+46command :'a,$command ...


12

Use the vim-merginal plugin (fugitive extension). It offers interactive TUI for: Viewing the list of branches Checking out branches from that list Creating new branches Deleting branches Merging branches Rebasing branches Solving merge conflicts Interacting with remotes(pulling, pushing, fetching, tracking) Diffing against other branches Renaming branches ...


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