vim: replace all characters up to a given token - string

Using vim I would like to replace all characters up to a certain one with another character, say a blank space - without affecting the layout/number of characters in the line. Here's an example:
Before:
real(kind=R12), intent(out) :: my_var
After replacing , intent(out) with blanks (i.e. starting from ,, and going up to )):
real(kind=R12) :: my_var
I know about r to replace one character, and about nr to replace n characters, but I would like to know whether I can accomplish my task without first having to count the characters I want to replace.
Thanks a lot for your replies!

Visual mode is probably the shortest way here:
vt:r
v enter visual mode
t: select till :
r (note space after r) replace selected region with spaces.

In command mode type 'df?' to delete up to that (?) character. Then 'i' to go back to insert.
For example if the following sentence is in your view:
The wizard quickly jinxed the gnomes before they vaporized.
and you enter dfs
You will be left with:
before they vaporized.

I know about r to replace one character
Did you know that R will keep you in that replace mode? So you could hit R and then hold Space until you've replaced everything you want.
However, I'd still go with Thor's answer. Visual mode allows you to use the efficient text navigation methods in vim without having to count out characters.
But if you disagree, there's always EasyMotion.

You can use regular expression here (use (.*?) to reference all values up to a token).
For instance:
The regex: (.*?)foo will get rid of everything up to foo.

Related

Motion to move to last of specific character on a line

Say I have a line that looks like this:
(Type)obj.method());
Is there a motion that will go to the last of a specific character on a line? I'd like to do
^c (motion to ')' character) (insert text) <Esc>
and transform that line to
obj.otherMethod());
I know I can use #f or #t, but I'd rather not count the parens.
As I understand your question, you don't just want to go to the last instance of a character, which you could achieve by going to the end of the line and searching backward. You want to clear text from your current caret position to the last instance of a character, right?
I'd typically use a pattern search to complete a motion to a desired character when there may be n of the same character in between. In your example, you can clear from the current caret position to the last ) by using c/);Enter, since only the last instance of o is followed by ;. You could precede this with a v instead of c, for example, if you wanted to select everything in between.
Using the same example, you could move to the 3rd o with /odEnter.
It may seem tedious at first, but in practice you are probably looking directly at the spot where you want be, so you can already see the additional characters you need and you only need to increase you specificity until you get the match, and you'll have immediate visual feedback as long as you have set incsearch.
Note: If your line did not have a semicolon at the end, you could move to the last paren by using )\n to search for the next ) followed by a line break.
You could use visual mode:
v$F)c
But in this case you're really just inserting some new text and changing the case:
f.aotherEscl~

Vim - Delete til last occurrence of character in line

I'm trying to figure out how to dt or df the last occurrence of a character in a string.
For example, let's say I have the following line:
foo not.relevant.text.bar
If I f df. I expectedly get foo relevant.text.bar but I would like to get foo bar. Using f 3df. is not an option as I don't know how many of that character will be in the string. Additionally, I may want to get foo .bar (f 3dt.), or if the line ends with a dot, I may want to get foo .. I want to always find the last one regardless of how many there are.
Is this possible without a regex? I suppose I could use a regex but I was hoping there was a simple vim command that I'm missing. I find myself trying to do something like this often.
one way without using regex, without counting "dot" (could be other letters)... see if others have better way..
foo[I]not.relevant.text.bar ([I] is cursor)
you could try:
lmm$T.d`m
or in this format, may look better?
lmm$T.d`m
this will do the job. you could create a mapping if you use that often.
EDIT
I add a GIF animation to show it works. :)
note
I typed #= in normal mode after moving my cursor to the right starting point (by f(space)), to display the keys I pressed in command line.
You can use my JumpToLastOccurrence plugin. It provides ,f / ,F / ,t / ,T commands that do just that.
I would use f df...
It is not necessarily shorter to type, but I find it easier to use "repeat last command" than counting in advance the number of word/sentence I want to delete.
Then you can adjust the number of . you type to adjust the length of the string you want to delete.
For your example: ET.dB
foo not.relevant.text.bar
And it works, as long as the cursor is anywhere within the text following "foo".
Strip Path from Path+Filename: ET/dB
I use it for stripping a pathname of all but the trailing filename.
Strip the path from /some/long/path/filename.ext leaving only the filename.
Just as long as:
The cursor is anywhere within the bold word
There are no spaces in that word
E Go to the end (since there are no spaces - also works if not the last thing on the line)
T/ Find the last / (stop just after it, so it will be deleted, as well)
dB Delete to the beginning of the word
In visual mode:
$F.d^
The $ goes to the end of the current line, F searches backward for a period and d^ deletes till the beginning of the line.

Vim jump back over spaces inside of curled braces

Lets say, I have wrote this piece of LaTeX in vim:
A_{\vec B}_
^
Cursor
The cursor is at the marked position. Now I want to add a \vec in front of the A.
I tried <Esc>Bi\vec <Esc>WW but I want B and W to jump over spaces which are inside of curled braces.
Any ideas how to do that?
Edit: Maybe I should've wrote, that I wanted to map this to a shortcut. A and B are just Examples for Strings which do not include spaces.
SOLUTION: I think I have found, what I was searching. With ? you can do a backward search with a regular expression.
You can't make B or W treat a braced string as a Word, but you can use % to jump from one end of a braced string to the other.
There are several ways to achieve a similar job. W and B only stop at whitespaces, as far as I know, so you can jump twice between words with 2B, or set insert mode at the beginning with I, or search for a letter, with f, F, t or T. Also 0 and $ to go to the beginning or end directly. There are many possibilities. I hope one of them can be useful instead.
FAi\vecESC$
Does it assuming that B is really at the end of the line

Command in VIM to replace something by the same character many times

Is there a vim command to replace something (a word, an inner object, a motion, etc) by a character but as many time as there are character to replace.
Exemple:
The cursor is at the beginning of the word foo
I want X and replace foo by XXX.
(I don't want to have to count the number of letter, so 3rX is not a valid answer)
I don't want a script or a mapping, I just want to know if there is already a command to achieve that.
I think this works:
verX
Of course the 'e' could be a different movement command.
If you have the cursor at the beginning of a word then you can do the following:
v Go in visual mode
w Select the word ( here you can use other motions too)
r replace with
X here you puts the character you want to use.
verX in normal mode to replace a word with Xs
Similar to Colin's answer, but using the "inner word" selection lets you be less picky about the cursor position:
viwrX
visual inner word replace X

Command to surround a character with spaces in vim

I am trying to use vim properly - to aid me I've mapped my arrow keys to "" so that I am forced to use {hjlk} to move around.
This is causing me a problem when I want to just surround a character with spaces, eg:
"2+3" is better formatted "2 + 3"
Previously I would have put my cursor over the + and typed:
i[space][arrow-right][space][Esc]
That's 5 presses.
To do this without the arrow I seem to need to put the cursor over the + and go:
i[space][Esc]lli[space][Esc]
That's 8 presses.
I can convert the "li" into an "a" which reduces it to 7 presses:
i[space][Esc]la[space][Esc]
Short of writing this into a macro is there a better way of doing it? Is there some magic vim command which will allow me to do it in less than even 5 presses - and some way to generalise it so that I can do it to entire words or symbols, eg if I want to convert 3==4 to 3 == 4?
Personally, I think it makes most sense to destroy what you want to surround, and then repaste it.
c w "" ESC P
Obviously, you can replace both the object and the quotes with whatever you like. To change just one character + to be [space]+[space], you would do
s [space] [space] ESC P
on the +
The first thing that jumps to mind after reading just the title is surround.vim which is an excellent script to do all kinds of useful things along the lines of what you've described.
To solve your specific problem, I would probably position the cursor on the + and:
s[space]+[space][esc]
To change 3==4 into 3 == 4, I might position the cursor on the first =, and:
i[space][esc]ww.
i have been wondering about this as well. i tried with surround.vim, but the naive approach
S<space>
(after making a visual selection) does not work since the space is already taken up as a modifier for adding space to other surrounding character pairs. S<space><cr> adds a ^M in the output. Ss almost works but inserts a space only before.
after asking at tpope/surround.vim on github:
S<space><space>
in visual mode works. alternatively, from normal mode, ysl<space><space> works for a single character
Hah! I've been trying to figure out how to surround a block in spaces for quite a while and I finally found the right combination.
Using surround.vim you say surround selector space space.
So for this specific case I would use visual mode (a good trick for operating on single characters under the cursor BTW) thus: "vs " <- four key presses!
I also have a habit of typing things like argument lists without spaces. With this technique you can just navigate to the second argument using w and say "vws " to visually select a word and surround with spaces.
I prefer visual select mode generally. Also the alternate surround syntax "ysw " excludes the word final comma that is caught by "vw".
You could create a macro with one of the described actions and call it everytime you need it (Like amphetamachine proposed while I was writing) or you could simply search & replace:
:%s/\(\d\)\(+\|-\)\(\d\)/\1 \2 \3/g
You probably have to execute this command two times because it will only find every second occurence of +/-.
EDIT:
This will replace everything without the need to be called twice:
:%s/\d\#<=+\|-\d\#=/ \0 /g
Try positioning your cursor over the '+' and typing this:
q1i[space][right arrow][space][left arrow][esc]q
This will record a quick macro in slot 1 that you can re-use whenever you feel like it, that will surround the character under the cursor with spaces. You can re-call it with #1.
There is also the more versatile one:
q1ea[space][esc]bi[space][right arrow][esc]q
Which will surround the word under the cursor ("==" counts as a word) with spaces when you hit #1.
You could set up a mapping like this (press enter in visual mode to wrap spaces):
:vnoremap <CR> <ESC>`<i<SPACE><ESC>`>la<SPACE><ESC>h
This method allows you to use . to repeat the command at the next +.
Put your cursor over the + and type:
s[SPACE][CTRL-R]"[SPACE][ESC]
I know this is and old thread, but this might be useful to someone. I've found that the map (map it to anything else you want!)
noremap <leader>ss diwi<SPACE><C-R>"<SPACE><ESC>B
works ok both for turning 'a+b' into 'a + b' (when used over the '+' char) and for turning 'a==b' into 'a == b' (when used over either the first or the second '=' sign).
I hope it's useful to someone.

Resources