Vim delete lines using regex and adding them to registry - vim

I have these mappings:
vnoremap <leader>dab "hy:v/<c-r>h/d<cr>
vnoremap <leader>daa "hy:g/<c-r>h/d<cr>
Which removes multiple lines by regex search, however, the lines are just deleted, and I'd like them to go on a registry so I can pase them following running these commands.
Can this be achieved by this regex search and delete?
EDIT:
I can see that they are added to different registers, Can I make the ex-command be saved to a single register?

Explaining my solution vnoremap <leader>daa "hyqeq:g/<c-r>h/d E<cr>:
"hy stores the visual selection into #h
qeq is equivalent to removing the content of #e
d E after g// is the command :d but it uses the uppercase register #E, which means it will append new content into it, instead of overwriting
However, this has some drawbacks:
Using <c-r>h into the mapping can break the command, e.g. if the selection contains slash characters; you can avoid a few unpleasant cases by adding \V in the pattern, but it's far from perfect: vnoremap <leader>daa "hyqeq:g/\V<c-r>h/d E<cr>
An extra empty line is prepended into the #e register

Related

extra space added when going into insert mode in an abbreviation

I'm trying to create an abbreviation which replace typed text by what I want but also move the cursor and enters insert mode.
the ab is as following:
:abbreviate MSG `MSG(("")); <Esc>F"i
everything works fine except when entering insert mode, I have to extra spaces before the cursor.
I've tried then to add <BS><BS> but it's leading to delete the first quote. Same thing if I'm putting only one <BS> (which is really strange, it seems the second <BS> has no effect at all...)
I guess I'm missing something but I can't figure out what...
Thanks for your help !
A citation from Vim help system (:help abbreviations):
An abbreviation is only recognized when you type a non-keyword
character. This can also be the <Esc> that ends insert mode or the
<CR> that ends a command. The non-keyword character which ends the
abbreviation is inserted after the expanded abbreviation. An
exception to this is the character <C-]>, which is used to expand an
abbreviation without inserting any extra characters.
Example:
:ab hh hello
"hh<Space>" is expanded to "hello<Space>"
"hh<C-]>" is expanded to "hello"
So if you press <Space> after entering MSG a space is inserted after expanding your abbreviation.
To avoid adding a needless space you can invoke the abbreviation with pressing <C-]> after entering MSG or you can try elaborate a mapping like this:
:inoremap MSG `MSG(("")); <C-O>F"
But IMHO such a mapping is very inconvenient.
Another option may be to use use one of the many abbreviation plugins like this (first shown by Google).

Show result instead of X with incomplete mapping in vim

In my file at ~/.vim/ftplugin/tex/insert.vim I have the following mappings
; <Nul> is ctrl-space
imap <Nul> \,
imap <Nul><Nul> ~
imap <Nul><Nul><Nul> \enspace
imap <Nul><Nul><Nul><Nul> \quad
imap <Nul><Nul><Nul><Nul><Nul> \qquad
This allows me to insert spaces of different width by pressing ctrl-space, with each press making the whitespace longer. When I type the first ctrl-space, I get an X and it waits for me to see if I wanted to input more ctrl-spaces or if I'm done.
I would like to know if there's a way to show what the space will be, either input it directly to the buffer and change it if it's pressed again, or put it in the status bar until I'm done.
A way I think this could be possible was to check if the chars before the cursor are \, and if they are replace it with the next space, and if it's not, check the rest of the spaces I can input and finally input a \, if none of the spaces are there. I just don't know how to accomplish such a keymap.
Your current map solution is based on the mapping timeout. Until that happens, Vim is waiting for more input, and nothing has been inserted yet. In order to later revise what got inserted, you need to completely replace the approach.
This could be implemented with :map <expr> <Nul>, checking what's before the cursor, and then returning <BS> characters to wipe that followed by the replacing text.
With the mjbrownie/swapit plugin, you'll get that "toggling through alternatives" functionality (using <C-a> / <C-x>) for free: Just put the following into ~/.vim/ftplugin/tex/swapit.vim:
SwapList spaces \, ~ \enspace \quad \qqad
You can keep your original <Nul> mappings to quickly insert when you know what you want (or for the initial insert), and use swapit only to revise later on.

Open a file under cursor with a line break in vim

I want to set up a key mapping to open an absolute path filename highlighted in visual mode that may have a line break in the middle.
Most files I have are formatted in the following way, and I would like to highlight the path between the single quotes and open that file (formatted for MSC NASTRAN for those curious):
INCLUDE '/directory/directory/directory/directory/
directory/filename'
What's difficult is yanking that whole section results in a ^M newline character in the middle of the path. My approach was the following (walking through the :map command below):
Yank the path from selection (stored to #0)
Create new register #f where empty string was substituted for the newline char from #0
Open file path in #f
map <F8> y \| :let #f=substitute(strtrans(#0),'\^#','','g') \| :e <C-R>f<CR>
The problem is that if it's run a second time on a new file path, vim uses the previous path stored in the #f register before updating #f
Any ideas to fix it or another approach? Clearing the register with :let #f='' at the end didn't seem to work either.
I also don't have admin rights, since this is a work computer, so I don't think I can install plugins, but I'm still new to vim, any thoughts appreciated. Thanks!
An auxiliary register is unnecessary; use :exec in order to evaluate an expression and use the result as an argument to a command. I'm not sure whether strtrans() is somehow advantageous, but replacing \n directly seems to work. Either way, make sure to use fnameescape(). Also, use noremap in order to avoid recursive mappings unless you need them. If this particular mapping is intended only for visual mode, xnoremap is even better.
xnoremap <F8> y \| :exec "e" fnameescape(substitute(getreg('"'),'\n','','g'))<CR>

How can I delete empty lines in motion in Vim

I know how to delete blank lines with commands.
We may visually select the text block first, and then run commands like
s/^$/d to delete all blank lines or %s/^\(\s*\n\)\+/\r to keep only one blank lines.
Can I perform the above using motion, so that I can just press some keys to perform the "delete-motion" without having to enter command-line mode?
Creating a new operator for this is a good idea, but it can be tough to get right.
The operator-user plugin makes that task easy.
Once you have installed operator-user, all you need to do is add two lines to your vimrc, one to define the operator, and one to define your personal mapping to it:
call operator#user#define_ex_command('delete-blanks', 'g/^$/d')
map _ <Plug>(operator-delete-blanks)
This creates a new operator _. Change it to whatever you like best.
Now you can do _3} or _G or _23k to delete the blank lines contained in the motion. Text objects _a}, doubling of the operator 4__, and Visual mode V7j_ are all also supported, as befits a proper operator.
You could use operatorfunc. For example:
Define a function like this in your .vimrc:
function! DeleteEmptyLines(type)
if a:type == 'line'
silent execute ".,'\"g/^$/d"
endif
endfunction
And a mapping:
nnoremap <silent> ,d :set operatorfunc=DeleteEmptyLines<CR>m"g#
,d performs now just like an operator and accepts a (line-based) motion. You can, for example, insert ,d5j or ,dG in normal mode to delete empty lines in the next 5 lines or to the end of file.
You can find more information on how to extend this functionality here:
http://learnvimscriptthehardway.stevelosh.com/chapters/33.html
and of course::h operatorfunc and :h map-operator.
From :h map-operator:
"An operator is used before a {motion} command. To define your own operator
you must create mapping that first sets the operatorfunc option and then
invoke the g# operator. After the user types the {motion} command the
specified function will be called."
well, using motions I don't think you can only delete blank lines.
But you can do it using a mapping:
:nmap <Leader>db :g/^$/d<CR>
The motions help you move one word, one paragraph away... And before the motion you use an operator (d, c...).
So what you'd want is to create a new operator that deletes blank lines within the given motion (or selection). What I gave you is close to that, but you'd have to invent a new operator (and I don't think there's many unbound keys left).
Other vimmers may correct me, but I think the easiest way to create such operators would be to define a map for each motion and bind it to a function.
There isn't a motion that can combine with a delete such that it only deletes blank lines. A motion denotes all the text between the initial and final position, without any gaps. That includes motions which search using regular expressions.

How do I insert a linebreak where the cursor is without entering into insert mode in Vim?

Is possible to insert a line break where the cursor is in Vim without entering into insert mode? Here's an example ([x] means cursor is on x):
if (some_condition) {[ ]return; }
Occasionally, I might want to enter some more code. So I'd press i to get into insert mode, press Enter to insert the line break and then delete the extra space. Next, I'd enter normal mode and position the cursor before the closing brace and then do the same thing to get it on its own line.
I've been doing this a while, but there's surely a better way to do it?
For the example you've given, you could use rEnter to replace a single character (the space) with Enter. Then, fspace. to move forward to the next space and repeat the last command.
Depending on your autoindent settings, the above may or may not indent the return statement properly. If not, then use sEnterTabEsc instead to replace the space with a newline, indent the line, and exit insert mode. You would have to replace the second space with a different command so you couldn't use '.' in this case.
A simple mapping to break the line at the cursor by pressing Ctrl+Enter:
:nmap <c-cr> i<cr><Esc>
essentially enters 'insert' mode, inserts a line break and goes back to normal mode.
put it in your .vimrc file for future use.
Here's how to create a macro that inserts a newline at the cursor whenever you press 'g' while not in insert mode:
From within vim, type:
:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]
Where:
[Ctrl+V] means hold the Ctrl key and press 'v'
[Enter] means press the Enter key
[Esc] means press the Esc key
You'll see the following at the bottom of your vim window until you press the final Enter:
:map g i^M^[
Explanation:
[Ctrl+V] means "quote the following character" -- it allows you to embed the newline and escape characters in the command.
So you're mapping the 'g' key to the sequence: i [Enter] [Escape]
This is vim for insert a newline before the cursor, then exit insert mode.
Tweaks:
You can replace the 'g' with any character that's not already linked to a command you use.
Add more to the command, e.g. f}i^M^[O -- This will find the } and insert another newline, then escape from insert mode and Open an empty line for you to enter more code.
You can add the command to your .vimrc or .exrc file to make it permanent. Just omit the colon from the beginning, so the command starts with "map"
Enjoy!
If you're usually expanding a one line block to three lines, try substitution. Change the opening bracket into bracket/return, and the closing bracket into return/bracket.
The command for substituting bracket/return for bracket looks like this:
:s/{/{\r/
Since you want to use this often, you could map the full sequence to an unused keystroke like this:
:map <F7> :s/{/{\r/ ^M :s/}/\r}/ ^M
Where you see ^M in the sequence, type [Ctrl-V], then press enter.
Now with your cursor anywhere on your sample line, press the mapped key, and the carriage returns are added.
Check :help map-which-keys for advice on selecting unused keystrokes to map.
Assuming you're okay with mapping K to something else (choose a different key of your liking), and using marker ' as a temporary marker is okay why not do this?
:nmap K m'a<CR><Esc>`'
now pressing K in normal mode over the character after which you want the line break to occur will split the line and leave the cursor where it was.
Basically, when you split a line you either want to just insert a carriage return, or in the case that you're on a space, replace that with a carriage return. Well, why settle for one or the other? Here's my mapping for K:
"Have K split lines the way J joins lines
nnoremap <expr>K getline('.')[col('.')-1]==' ' ? "r<CR>" : "i<CR><Esc>"
I use the ternary operator to condense the two actions into one key map. Breaking it down, <expr> means the key map's output can dynamic and in this case hinges on the condition getline('.')[col('.')-1]==' ' which is the long winded way to ask vim if the character under the cursor is a space. Finally, the familiar ternary operator ? : either replaces the space with linebreak (r<CR>) or inserts a new one (i<CR><Esc>)
Now you have a lovely sister key map to the J command.
Vim will automatically kill any whitespace to the right of the cursor if you break a line in two while autoindent (or any other indentation aid) is enabled.
If you do not want to use any of those settings, use s instead of i in order to substitute your new text for the blank rather than just inserting. (If there are multiple blanks, put the cursor on the leftmost and use cw instead.)
In fact you need the following combined operations:
Press v to enter Visual Mode
Select the line you want to split
Press : to enter in Command Mode
s/\s/\r/g
Done
If you have the input:
aaa bbb ccc ddd
and want to output
aaa
bbb
ccc
ddd
You can use the command
f r<ENTER>;.;.
o ESC command will do it for you.
Set this key mapping in your vimrc
:map <C-m> i<CR><Esc>h
Then press Ctrl+m if you want to use it in your vim.
IMHO, the built-in mapping gs is not a useful mapping (put vim to sleep), one could use this for splitting:
nmap gs i<CR><ESC>
In Vrapper you can use gql which will split a line without entering insert mode, but may not always maintain indentation.
I found this to be the most faithful implementation of what I'd expect the opposite behaviour to J
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
It does the simplistic new line at cursor, takes care of any trailing whitespace on the previous line if there are any present and then returns the cursor to the correct position.
i <cr> <esc> - this is one of the most common solutions suggested, it doesn't delete non-whitespace characters under your cursor but it also leaves you with trailing whitespace
^mw - goto start of new line and create a mark under w
gk - go up one line
:silent! s/\v +$//<cr> - regex replace any whitespace at the end of the line
:noh<cr> - Clear any search highlighting that the regex might have turned on
`w - return the the mark under w
Essentially combines the best of both r<esc><cr> and i<cr><esc>
Note: I have this bound to S which potentially overwrites a useful key but it is a synonym for cc and since I don't use it as often as I do splits I am okay with overwriting it.
This mapping will break up any one-line function you have. Simply put your cursor on the line and hit 'g' in normal mode:
:map g ^f{malr<CR>`a%hr<CR>`a
This assumes that you have a space after the opening brace and a space before the closing brace. See if that works for you.

Resources