42

Note: I'm assuming you're using the a register for the entirety of this answer, but you can use any register Note2: <1b> is Esc; you can insert this with Ctrl+v and then Esc. It may also show up as ^[ (depending on the display setting). It's not a mapping; so it makes sense that :map doesn't work. It's recorded in a register, and you can see & ...


35

Your options are: Use the black hole register _. For example, to delete a line you would type "_dd. This deletes without affecting the clipboard. Explicitly name a register for the original yank. For example, to yank a line into a named register you would type "ayy then to paste you would type "ap Use the yank registers to retrieve the original yank ...


18

The black hole register is used in the same situations as /dev/null: when you do an action that normally outputs something but you have no use for that output. Vim's default behavior is to "cut", not "delete". In most cases it doesn't matter but, sometimes, users may actually need to "delete". That's where the black hole register comes handy. Is this ...


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


15

Use an uppercase register when yanking (copying): "Ay "A says to append to the a register, as opposed to "a which would replace the contents of the a register. Once you've copied everything into the register, you can then paste it all at once with: "ap


14

This peekaboo plugin is pretty good for for previewing registers. When you press " or @ in normal mode, or ctrl-r in insert mode, a split will open to show you the register contents. Preview from the plugin repo:


13

You can use visual selection to select the text to replace, and then paste over it as follows: vi(p vi( selects Baz (because it is enclosed in parenthesis), p pastes Fnord over it. However, this will yank Baz to the register after the paste, which might not be what you want.


12

In short, there is not a cleaner way to wipe registers so completely that they disappear from :reg. Rather than murk around with ~/.viminfo, I tend to "softclear" registers when I'm really and truly done with them by setting them to be blank. To clear the a register, for instance, I type qaq to set the a register to an empty string. Equivalently, :let @a=''...


11

Ex commands work on the buffer contents; for register (or variable) contents, you need to use a corresponding Vimscript function (if it exists). For :substitute, the equivalent is substitute() (that was easy, right?) So: :let @t = substitute(@t, '_', 'test', 'g')


11

x doesn't put things on the clipboard, it puts them into a register. If you don't tell it which register to put it into, it puts it into the unnamed register, which of course overwrites the previous contents. So, instead, tell it which register to use: " REGISTER x, where REGISTER is any single letter. You then use the same quote-register prefix to p to ...


11

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


11

Yep, just like in insert mode: Ctrl-R followed by the register name, or " for the default one (i.e. Ctrl-R "). See :h c_CTRL-R.


11

If you include the flag y in the cpoptions option; set cpoptions+=y then yanks may be repeated using .. See :help cpo-y: *cpo-y* y A yank command can be redone with ".".


10

This used to work in older Vim 7.3 versions when you :set clipboard=unnamed (see this Reddit discussion, as a result of a bug, which I've reported here. That thread contains a patch by Aryeh Leib Taurog which introduces a 'regone' option. This has made it into the (overly long) todo list, but it's unlikely to be incorporated soon. So, you currently only ...


10

Unless you specify a register, p (or P) will use the last register that was filled. The unnamed register "" contains the most recently yanked or deleted text (unless another register was specified, e.g. with "ay), but these also go into "0 for yanks or "1 for deletes/changes. See registers. If the last text was yanked with y you can paste it from the 0 ...


10

After you've done your Visual selection run this: y:@"<CR> y copies the selection to the unnamed register (") since we didn't explicitly name a register. Then :@" executes the contents of that register as Ex commands. See help :@ and https://stackoverflow.com/questions/20262519/vim-how-to-source-a-part-of-the-buffer


9

To get something to work with... qajjjq Will start record a macro into the a register. You can see many of your current registers (used for macros, yanking, deleting, etc.) with the :reg command, or you can specify a register to display by providing its name. For example, to show register a: :reg a yields --- Registers --- "a jjj You can append to ...


9

This can be done pretty easily with the global command. I would try this: :let @a='' :g/^function/y A First we must call :let @a='' to make sure that the 'a' register is empty. Then, the global command is used to apply a command to every line matching a certain regex. In this case, the regex is ^function, and the command is y A (the ex command, not the ...


8

You can check if Vim is built with clipboard support with: :echo has('clipboard') If the output is 1, the following should be enough to achieve your goal: "*y in source Vim "*p in destination Vim You can make it all a lot more seamless by synchronizing the clipboard register and the default register with this setting in your vimrc: set clipboard^=...


7

A few <expr> mapping should be able to do the job here. The basic structure would be function! ClipboardOrXclip(command, register) if a:register !~ '[+*]' || has('xterm_clipboard') || has('gui_running') " Just return the original command if the clipboard is accessible " or it's not a register that should be handled by xsel ...


7

Macros are just text. Open a new empty buffer and put your macro there. You can duplicate the lines, change one thing here and something else elsewhere then yank a line and use it right away with @". No need to pollute your registers, especially the number registers which serve an entirely different purpose. -- edit -- Your question is easier to ...


7

It is - to quote from :h quote-number: Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). An exception is made for the delete operator with these movement commands: |%|, |(|, |)|, |`|, |/...


7

nnoremap only affects [n]ormal mode and will not affect [v]isual mode. You need vnoremap for that: vnoremap d "_d


6

You can assign to the clipboard with the special + register: :let @+ = expand('%:p') If you want to make this easier, you could create a command, so you only have to type :CopyBuffer: :command! CopyBuffer let @+ = expand('%:p') and/or map it to a key: :nnoremap <Leader>c :let @+=expand('%:p')<CR> The post "How can I copy text to the system ...


6

There's the repeat() function: repeat({expr}, {count}) Repeat {expr} {count} times and return the concatenated result. Example usage: :let @d = '<td></td>' :let @r = '<tr>' . repeat(@d, 3) . '</tr>' :let @t = '<table>' . repeat(@r, 2) . '</table>' :echo @r <tr><td></td><td></td><td&...


6

Using getreg and setreg To copy from q to registers 1–5: let src=getreg('q', 1) for i in range(1, 5) call setreg(i, src) endfor To copy to a more general list: let src=getreg('q', 1) for i in ['a', 'c', 'e'] call setreg(i, src) endfor Using execute To copy from q to registers 1–5: for i in range(1, 5) execute 'let @' . i . '=@a' ...


6

Here is the full list of settings which should be added to your .vimrc if you want to prevent any leakage of sensitive files. set nobackup set nowritebackup set noundofile set noswapfile set viminfo="" set noshelltemp set history=0 set nomodeline set secure I recommend you create a new configuration file called .vimrc_secure and run Vim with vim -u ~/....


6

If you don't use a specific register, the text you yank systematically goes into register 0 where it will stay until you yank something else. This means that you can still use what you yanked, even if you cut stuff in the mean time. Paste from register 0 in normal mode: "0p. Insert content of register 0 in insert mode: <C-r>0. See :help registers.


6

In insert mode, the left arrow key actually exits insert mode, moves the cursor, then re-enters insert mode. That breaks your change into two changes: inserting () and inserting foo. This behavior is discussed under :help ins-special-special The solution is to remap the left arrow key to include CTRL-G U. The example in the Vim Reference Manual is hard to ...


6

The alternate file is stored in a register, "#. Change you line to: let @#='/path/to/alternate/file' For more help see: :h registers :h quote# :h :let-@


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