How can I remove one character from some position ? For example, I have string hello, remove third char and get helo.
I have tried to use next expression, but it doesn't work.
.s/\%3c//g
The \%c special atom is a zero-width match; i.e. it adds a restriction (on the byte count of the match) without consuming the character. To do this, append another atom that consumes it, e.g. . for any character match. Then, your substitution will work as expected:
.s/\%3c.//g
In normal mode also known as command mode but not to confuse with command-line mode, you can type:
03lx
0 to go to the start of the line.
3l to move 3 characters forward.
x to remove the character under the cursor.
Related
This is test
There are two tabs (\t) in this line. I want to get rid of the part from the beginning to the first tab key, which is "This ", and I used the following pattern:
:s/.\{-}\t//g
It says it can't find the pattern. If I use the following, both tabs are replaced, which isn't what I want. Why doesn't the first pattern work?
:s/.*\t//g
Your first attempt does not work because you are matching the fewest number of any character followed by a tab. The fewest number of any character is zero (0). So both of your tabs match without any other characters.
Based on the comments, the above explanation was incorrect.
Here is one possible solution.
:s/^[^\t]*\t//
This goes from the beginning ^, capturing any number of non-tab characters [^\t]* until it reaches a tab \t.
Your pattern /.\{-}\t didn't work because of the g flag in the :s command. This flag enables global matching so it matches twice. Just remove the flag and it will work. In addition, when deleting something you can omit the replacement part in :s:
:s/.\{-}\t
The full :s/.\{-}\t// is fine as well. Note that in either case it should not say "pattern not found" as you described. If you see that message, there is something else different between your example and your actual text.
I need to search through a larger text file.
This is an example of what I'm searching through.
https://pastebin.com/JFVy2TEt
recipes.addShaped("basemetals:adamantine_arrow", <basemetals:adamantine_arrow> * 4, [[<ore:nuggetAdamantine>], [<basemetals:adamantine_rod>], [<minecraft:feather>]]);
I need to look for lines that match a specific part in the first argument.
For example the "_arrow" part in the above line.
And erase everything that doesn't match on the "_arrow" in the first argument.
And the arguments differ across all of them.
And also with different names in the place where "basemetals:adamantine" is in the above line.
And since the further arguments are all different I can't wrap my head around on how to include the end only when the first thing matches.
Edit: The end goal being to ease sort my 3k+ line text file.
basic, blacksmith, carpenter, chef, chemist, engineer, farmer, jeweler, mage, mason, scribe, tailor
I think what you're trying to do is filter your text file by removing lines that don't fit a set criteria. I've chosen the Atom text editor for this solution (because I'm running Windows OS and can't install gedit, and I want to ensure you have a working example).
To remove only lines that don't have a first argument ending in _arrow, one could do (?!recipes\.addShaped\("[^"]+_arrow")recipes.+\r?\n? and replace with nothing.
As a note: this task is made more difficult by Atom's low regex support. In a more well-supported environment, my answer would probably be ^recipes\.addShaped("[^"]+(?<!_arrow)").+\r?\n? (with multiline mode).
Also, please read "What should I do when someone answers my question?".
Regex explained:
(?! ) is a negative lookahead, which peeks at the succeeding text to ensure it doesn't contain "_arrow" at end of the first argument.
\. is an escaped literal period
[^"] is a character class that signifies a character that is not a ".
+ is a quantifier which tells the regex to match the preceding character or subexpression as many times as possible, with a minimum of one time.
. is a wildcard, representing any character
\r?\n? is used to match any kind of newline, with the ? quantifier making each character optional.
Everything else it literal characters; it represents exactly what it matches.
I have a data source that contains a bunch of city names, but mixed in are quite a few state abbreviations that shouldn't be there..
Is there a way in VIM to delete each line than contains two characters or less?
Here it is:
:g/^\a\{1,2}$/d
Explanation
delete each line that ... → that calls for a :global command (which defaults to % range, the entire buffer); the executed command is :delete.
two characters or less → in a regular expression, any character is matched by ., to restrict this to 1 or 2 the \{n,m} multi is used. This still needs to be anchored via ^ and $ to the beginning and end of the line, so that additional characters don't make this match. Oh, and if you also want to remove completely empty lines, change this to .\{,2}. See :help /\{ for details.
more robust "characters": . will match any character, i.e. also whitespace. To avoid unwanted matches, it's best to restrict this as much as possible. If your state abbreviations are only alphabetic, you can use the \a atom instead of . The available character classes start in the help at :help /\i.
I've been learning how to do more complex search and replace functions in VIM, and I ran across a use case like this:
:%s/$/|/g
This supposedly finds the end of every line and replaces it with a vertical pipe. When I was first learning this, though, I assumed you would have to add the end-of-line character in the replacement string to get the expected results. i.e.,
:%s/$/|$/g
Why does it work without it and still preserve the line break? Shouldn't it be replacing the line's terminating character with your string and removing it in the process?
The same thing could be asked with the beginning-of-line character, ^.
Anchor $ does not include the newline character. In fact it is a zero-width token. It matches the empty character just before the first newline in your string. And hence the result.
Similarly, ^ matches an empty character before the first character in your string.
See http://www.regular-expressions.info/anchors.html for more details.
I want to convert the 5 space indentation in a python file to 4 space indentation. I want the command to do the following
remove a single space in all the lines which starts with a space followed by characters.
I issued the command %s/^\ [a-zA-Z]*// which seems to work. Later i figured out that the command should actually be
remove a single space in all the lines which starts with a space followed by any number of spaces followed by characters.
However still i am not able to figure out how the command(above) is working. It should basically report error for the following stating pattern not found but still it works.
class H:
def __init__():
hell()
It's working because * means "match zero or more of the previous atom". In your case, it's matching zero. You probably wanted to use \+ instead which means "match one or more of the previous atom".
In actuality, you could have just dropped the * entirely because just a space followed by a single character would have matched what you were originally searching for. There are better regular expressions for what you're trying to accomplish, but that's not what you're asking here.
Edit (clarification):
Your regex as it stands (^\ [a-zA-Z]*) translates to:
^: From the start of the line
\: Match a space
[a-zA-Z]: Followed by a letter
*: Zero or more times (of the previous atom - a letter)