13 votes
Accepted

How to delete a multi-line match?

The feasibility of deleting parts of lines is elusive and a better vimmer than I will have to explain that. If anyone cares to donate an explanation I'd be happy to add it to this answer. But it's ...
user avatar
12 votes

How to delete till the end of the previous line?

Well, you can combine the "backward search" motion and the delete operator: d?$<Enter>
user avatar
  • 9,340
12 votes
Accepted

How to delete till the end of the previous line?

Try d0kJx Deletes backwards to the beginning of the line, moves up, then joins the two lines and then removes the space in between the two joined lines. You don't have to leave normal mode with this....
user avatar
11 votes
Accepted

How to write each line into separate file?

Try a global command: :g/^/exe ".w! line".line('.').".txt" :g/^/ Do a command for every line (you can adjust this regular expression if you only want to save certain lines, i.e. . for non-empty ...
user avatar
  • 7,466
10 votes
Accepted

Sort every few lines

The easy way: join the lines in the logical blocks before sorting. In detail: mark the lines join the indented lines to the lines above: :'<,'>s/\n /^A/ mark the lines again: gv sort them: :'&...
user avatar
  • 3,879
10 votes
Accepted

delete entire line after first whitespace on every line

The two commands below will reduce every line to the first sequence of non-whitespace character followed by a space: from 0.453945 -2.14126e-54 3.40152e-49 101325 214.355 to 0.453945 <-- space ...
user avatar
  • 33.1k
10 votes
Accepted

How can I add text to the end of words on each line?

You can also use :%s/\w\+/`&` to convert from Lorem ipsum dolor sit amet to `Lorem` `ipsum` `dolor` `sit` `amet` :% range to the next command (whole buffer) s is a substitute command :s/...
user avatar
  • 10.4k
8 votes
Accepted

How to create multi-line abbreviations

yes, you can do with the ^M character in your abbrev, for example: iabbrev Doc #^M# function :^M#^M# returns :^M# will be transformed into: # # function : # # returns : # To get ^M you need to ...
user avatar
  • 15.2k
8 votes
Accepted

Visual Block Mode: How to insert text to multiple lines after line ending?

It could be done with virtualedit=ALL: set virtualedit=all select visually vertical part of the text A\<ESC> return set virtualedit=block See the screen:
user avatar
  • 10.4k
7 votes
Accepted

How can I sort multiline blocks in vim?

Peter Rincker's SortGroup is the go-to script for this kind of work. https://gist.github.com/PeterRincker/582ea9be24a69e6dd8e237eb877b8978 Somewhat differently to vim's :sort, it takes a pattern ...
user avatar
  • 12.8k
6 votes
Accepted

How to store all occurrences of a multiline search in a register?

It turns out something along these lines works (although there might well be better ways) -- using a substitute-without-substituting command instead of a global command: Starting with the answer here:...
user avatar
  • 141
6 votes

Shuffle multiline folds

There is a :folddoclosed command which iterates over all closed folds (just like :g command for the whole buffer) and executes one or more commands on them. So I would close all folds, convert each ...
user avatar
6 votes
Accepted

Paste append multiple lines

This would be very easy with a macro, but you've stated don't want to use one, so here's a few alternative methods. I think they're all more complicated than a simple macro-based solution, but they ...
user avatar
  • 28.3k
5 votes
Accepted

Visual scrolling/visual <C-e> and <C-y> across wrapped lines?

I've released a vim plugin that pretty closely matches this behavior. Instead of scrolling by exactly N lines, this maps <C-u>, <C-d> (half window) and <C-j>, <C-k> (quarter ...
user avatar
  • 1,257
5 votes
Accepted

multi-line insert after indentation whitespace

There are a few ways you could do this. The way I would probably do it is this: :%norm I-<space> Note that the <space> should be a literal space, not the text <space>. This simply ...
user avatar
  • 16.8k
5 votes
Accepted

Repeating multi-lines and increment numbers in specific sections?

A macro is certainly capable of doing this. Starting with the first three lines as a template entry 100/1/1 fields value 1 other section 1 is valid Place your cursor on the first line and ...
user avatar
  • 7,466
5 votes

delete entire line after first whitespace on every line

If you wanted to delete entire line starting with a space, This pattern will be useful. :g/^\s.*/d g -global ^ - start of the line \s - space .* - anything after that d - delete If you want the ...
user avatar
  • 3,082
5 votes

Replace capitalized letters in matching lines

To obtain this result you could use :substitute, for example with the cursor on the first line: :s/\u\+/{\0}/g \u\+ one or more uppercase character: equivalent to [A-Z]\+, see:h \u \0 whole matched ...
user avatar
  • 1,516
5 votes

Visual Block Mode: How to insert text to multiple lines after line ending?

The short answer is: In Visual Block mode select lines to their ends ($) and use A to append some text at the end of each line. The longer answer: gg to go the beginning of the first line. CTRL-V (...
user avatar
  • 196
5 votes

How can I delete lines fit a pattern to a register or a new file?

Use a Dedicated Register to Which You Append Note delete overwrites the unnamed register "" for each match. Therefore, only the last line matching the pattern is still available in "". Also the ...
user avatar
  • 4,050
5 votes

Paste append multiple lines

I am going to suggest using Ctrl + v (<C-v>) to make a block. It sounds as if putting the second block after the first doesn't do what you want. However, if you put the first block in front of ...
user avatar
  • 236
5 votes

How can I add text to the end of words on each line?

You can use the :normal command, which allows you to run a sequence of Normal mode commands. When given a range, it repeats the sequence for every line. (It also starts at the beginning of each line, ...
user avatar
  • 26.3k
5 votes
Accepted

How to delete line endings in Vim?

You can remove line endings by joining lines together. There are a few commands that can accomplish this, for instance the J Normal-mode command will join the current line and the next, or you can use ...
user avatar
  • 26.3k
4 votes

How to delete till the end of the previous line?

With visual mode: hvk$d Explanation h - Move one to the left v - Start visual mode k$ - Move to the end of the previous line d - Delete selection
user avatar
4 votes
Accepted

How to replace two-line match with its first backreference pattern?

I can't use first backreference (\1) instead of hardcoded string Well, you haven't captured anything in the search so \1 has no value in the replacement. You can use a matched text later in the ...
user avatar
  • 10.4k
4 votes

Sort every few lines

Idea: Let's join the n number of lines under case to a single line. Follow the process for all cases. Then, use sort to sort the cases. Then, cut those lines back and indent them. Steps: 1) Join n ...
user avatar
  • 3,082
4 votes
Accepted

Yanking lines from a multiple selection

What happens if you run: :let @a="" :MSExecNormalCmd "Ay after you have your blocks visually selected? ... the idea (even if the above doesn't work) is still to yank-append each visually selected ...
user avatar
  • 9,340
4 votes
Accepted

How to change text in other line when the pattern is found?

:/baa/,/quz/s/baa/bar/ works, but that will only work with the first occurrence of baa. If you want to replace all occurrences of baa with bar that come before quz: ,/quz/s/baa/bar If your scenario ...
user avatar
  • 6,380
4 votes
Accepted

Keep the full visual selection after indenting in easy mode vim

Think of it like this. aaaa bbbb cccc If you select these 3 lines, then each line has 4 characters selected. Now, you indent them and use gv to reselect the whole thing. Here is the new text: <...
user avatar
  • 16.8k
4 votes
Accepted

How can I stagger text in vim?

vimscript option For the result: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 Next "commands" solves it: :for i in range(6)|call setline(i, repeat(' ', (i-1)*2).getline(i))...
user avatar
  • 10.4k

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