In Vim, is there a way to move the selected text into <current_file>.bak
, appending or prepending? If possible, the backup file should not be displayed.
I envision the workflow to be:
:sbak
<current_file>.bak
The append command will put the cursor after the current position, while the insert command will put the cursor before it. Using the append command is like moving the cursor one character to the right, and using the insert command.
vim Inserting text Insert text into multiple lines at once Press Ctrl + v to enter into visual block mode. Use ↑ / ↓ / j / k to select multiple lines. Press Shift + i and start typing what you want. After you press Esc , the text will be inserted into all the lines you selected.
You can do it in three steps:
:'<,'>w! >>file.bak
to save selected lines to file.bak
(append)You can write a user-defined command Sbak
if you like:
com! -nargs=1 -range Sbak call MoveSelectedLinesToFile(<f-args>) fun! MoveSelectedLinesToFile(filename) exec "'<,'>w! >>" . a:filename norm gvd endfunc
What about
:'<,'> w! >> /YOUR/SELECTIONFILE
:'<,'>d
Is that what you want? If so set up a map
for it, like
map <F2> :'<,'> w! >> /YOUR/SELECTIONFILE<cr>:'<,'>d<cr>
Note this appends to SELECTIONFILE
, and not only the selection, but the whole lines. Also, read :h :w
and :h ++opt
(in which you can learn about the possible options for writing files (e.g.) you can append to a file with different encoding, which really messes things, so don't do that ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With