20

I'd like to write a visually selected area to a file.

For example:

prefix [sample text
more text] suffix

[] is the selected block which should be taken, resulting in a new file containing:

sample text
more text

The normal w command uses lines so '<,'>w! /tmp/test would include the prefix/suffix (and `<,`>w! /tmp/test is not allowed).

laktak
  • 2,933
  • 13
  • 27

3 Answers3

13

First, copy (also called 'yank' in vim parlance) your visual selection to the register of your choice. For example, to copy to register 'z', make your visual selection, and then type "zy.

In the case you give, you could do this without moving the cursor, by putting the cursor on the first square brace, and then typing v%"zy.

Breakdown: v -> start visual selection; % -> move to matching brace; "z -> use register 'z'; y -> yank (copy) the selection (in this case, into register 'z').

Once you have copied your visual selection to register 'z', go to the command line, and type

call writefile(getreg('z', 1, 1), "some-file")

If you want to append the text, rather than overwrite, you can use the "a" flag as follows

call writefile(getreg('z', 1, 1), "some-file", "a") 
jamessan
  • 11,045
  • 2
  • 38
  • 53
muru
  • 24,838
  • 8
  • 82
  • 143
3

You can also copy your content and create a new window

Ctrl-w n ........... create a new window
p .................. paste

Then you can save as you want

SergioAraujo
  • 1,165
  • 11
  • 12
2
  1. Vim help says (:h 10.3):

Note: When using Visual mode to select part of a line, or using CTRL-V to select a block of text, the colon commands will still apply to whole lines. This might change in a future version of Vim.

  1. The plugin vis (#1195) by Charles Campbell offers the command :B {cmd} to apply a {cmd} only to the visual (block) selection.
Hotschke
  • 4,740
  • 26
  • 37
  • I've just tried to implement this approach to make a mapping so that copies the selected text to the Mac clipboard: vnoremap <C-c> :B w !pbcopy<CR><CR>. This doesn't work. Am I using the vis syntax incorrectly? – Alex Roberts Aug 05 '21 at 18:47
  • 1
    Blockwise yanking to clipboard should be possible without a colon command with "+y. No plugin necessary. Are you using neovim? – Hotschke Aug 06 '21 at 06:29
  • That worked, thank you! (Yes, I am using neovim.) I didn't know about this feature. Is there a similar mapping for the inverse procedure, to paste from the clipboard? Or is that something I should add to my .vimrc myself? – Alex Roberts Aug 07 '21 at 20:08
  • Or I suppose one can simply enter INSERT mode and press Command-V (on a Mac). But perhaps there is a more vimish way to do this? – Alex Roberts Aug 07 '21 at 20:17
  • 1
    Pasting from clipboard should be possible with "+p in normal mode and in insert mode with <c-r>+. If you like standard macOS shortcuts Cmd+x, Cmd+c, Cmd+v in normal and insert mode, have a look into https://github.com/macvim-dev/macvim/blob/master/runtime/macmap.vim#L48-L57 Afaik, they do not exist in vim/neovim by default. A quick test of the MacVim mappings in VimR (neovim GUI) worked for me. – Hotschke Aug 08 '21 at 09:25
  • That worked ("+p I mean). Thank you! – Alex Roberts Aug 08 '21 at 18:23