I am new to UNIX programming and had the following problem with UNIX vi editor.
Can you please tell me the UNIX command required to delete text delimited by { and } where both characters occur after the current cursor position. Thanks.
If you're on the same line :
f{d%
f{ moves you to the next { character
d% deletes everything to the matching bracket
If you're on a different line, use /{ to search for that character
And if you want to delete ALL text delimited like this :
:%s/{.*}//g
(replaces all instances of anything between brackets with nothing)
Related
I'm using let names = fs.readFileSync(namefile).toString().split("\n"). Whenever I do
for(const name of names) {
console.log(`First Name: ${name.split(" ")[0]} Last Name: ${name.split(" ")[1]}
}
the last name part always has \r at the end, how do I make it not add the \r?
fs.readFileSync doesn't add anything to the end of lines,
instead the file that you're trying to read is using CRLF line endings, meaning that each line ends with the \r\n sequence.
Really your file looks something like this:
line1\r\nline2\r\nline3\r\n
But your text editor will hide these characters from you.
There are two different ways you can fix this problem.
Change the type of line endings used in your text editor
This is IDE specific but if you use Visual Studio Code you can find the option in the bottom right.
Clicking on it will allow you to change to LF line endings, a sequence where lines are followed by a single \n character.
Replace unwanted \r characters
Following on from your example we can use .replace to remove any \r characters.
let names = fs.readFileSync(namefile)
.toString()
.replace(/\r/g, "")
.split("\n")
More on line endings
I would like to know how to delete :
all of the contents inside a php tag using vim
<?php i want to delete this ?>
If your opening & closing PHP tags (<?php ... ?>) are in the same line, you can do it this way.
Put your cursor at the first character after PHP opening tag.
Type v/ ?[enter]d in normal mode.
The 2nd point means to enters visual mode ('v') from the first character, searches ('/') for the [space]? (' ?') pattern (exactly before the PHP closing tag), and then delete it ('d').
Use di< or di> to delete all the characters inside <>. The cursor should be inside of the <>.
Use ci< or ci> to delete and be in insert mode.
Helpful but Optional Explanation:
It is better to start with text-objects. Excerpt from :h text-objects, given below, suggest two forms i and a
This is a series of commands that can only be used while in Visual mode or
after an operator. The commands that start with "a" select "a"n object
including white space, the commands starting with "i" select an "inner" object
without white space, or just the white space. Thus the "inner" commands always select less text than the "a" commands.
text-objects are useful to other character pair like (), {}, etc. For example, it is useful while changing
if ( i == true ) {
}
to
if (_) {
}
by using ci( or ci).
I have about 256 lines in a text file that look like /*0*/L"", I want to remove the last , and then put the remaining as a function argument code.append(/*0*/L""); I tried doing it with vim but I don't have much experience in it. how can we place something within something else in vi or vim?
:%s#\v(/\*0\*/L""),#code.append(\1);#
:%s : substitute all lines
# : alternative separator
\v : use very magic mode (see :h magic)
(/\*0\*/L""), : capture the regex, excluding the trailing comma
\1 : insert first captured group
this line would do the substitution on all lines in your buffer, only if the line ending with comma. No matter you had /*0*/L"", or /*123*/L"",
%s/\v(.*),$/code.append(\1)/
if you want to shrink the sub on certain pattern, change the .* part in the above cmd to fit your needs.
how to transform text block to get it in one line
for example
Hello
Sublime
Text
Editor!
should become
Hello Sublime Text Editor!
??
Thanks!
You don't need a regular expression to do this. First, select the text:
Then hit CtrlJ on Windows/Linux or ⌘J on OS X to join the lines together:
This can be done for example by running a Perl regular expression replace searching for (?:[\t ]*\r?\n[\t ]*)+ and using a single space character as replace string.
(?:...) is a non marking group.
[\t ]* finds 0 or more tabs or spaces.
\r?\n finds an optionally present carriage return and a line-feed.
And the + means 1 or more times of the regular expression in the non marking group which in other words finds optionally existing trailing whitespaces at end of a line, the line termination and optionally existing whitespaces at beginning of next line.
This is my question.
In vim editor I want to select all the words between double quotes through out the whole file and i want to replace the selected words by preceding that with gettext string. Please anybody tell me vim command to do this.
for ex:
if the file contains
printf("first string\n");
printf("second string\n");
After replacement my file should like this
printf(gettext("first string\n"));
printf(gettext("second string\n"));
You should be able to do:
s/\".\{-}\"/gettext\(\1\)/g
try this in vim:
:%s/\(".*"\)/gettext(\1)/g
Here \( and \) is being used to group the text and \1 is then used to put 1st backreference back along with gettext function.
in command mode:
:%s!"\([^"]*\)"!gettext("\1")!g
the % is for whole document, [^"]* for anything except quotes, and the g at the end for all occurence in the line (default is only the first one). The separator char can be anything not in the regexp... I often use ! rather than / (more convenient when dealing with path e.g.)