How do you replace a line with forward slashes in it in EX? - vim

I'm running a script for vim EX mode I've tried every escape character and word identifier I can find.
it needs to find the string "/etc/walker" and replace it with "/etc/runner"
% s/\</etc/walker\>/\</etc/runner\>/g
wq
same issue with a script to append at the end of the file. It doesn't do anything. I'm trying to append "/etc/walker"
$
a
\</etc/walker\>
.
wq
what I've tried on regex editors seems to work there but not in EX
Thanks for your help

Try this:
:s#/etc/walker#/etc/runner#
Notice the use of # as a delimiter, that way you don't have to add back slashes.
You could also use:
:s#/etc/walker#/etc/runner#
For appending at the end of the line:
:s#$#/etc/walker#
In EX mode just remove the : at the beginning.

Related

how to visualise and delete trailing newline at the end of file in vim\nvim

Sometimes I need to edit files which should not end with a newline.
However vim\nvim by default do not visualise in any way the newline character at the end of file. Therefore I am not able to:
visually confirm if the file has a newline character at the end or not
remove that character
Are there any setting which would allow me to see the tailing newline character and edit it in the same way as any other characters?
For example, after create 2 files as follows:
echo test > file-with-newline
echo -n test > file-without-newline
opening first one with nvim file-with-newline shows:
test
~
~
file-with-newline
opening second one with nvim file-without-newline shows:
test
~
~
file-without-newline
Navigating with the cursor to the end of line in either case yields the same result (the cursor stops after last visible character: t). There is no way to tell if the newline is there or not, let alone remove it using familiar commands used to remove ordinary characters (or newlines within the file).
You can enable the option :help 'list':
:set list
to show that "newline character" as a $ at the end of the line (among other things):
Note, however, that the option doesn't make the character "editable" in any way.
if the file has a newline character at the end or not
:set eol?
endofline
remove that character
:set noeol nofixeol
:update

vim command to add a semi colon at the end of all lines in a file beginning with a pattern

I would like to insert a semi-colon at the end of all the lines beginning with some pattern(MATCH_) in a file.
Whats the command to use.
This can be done with the :g command:
:g/^<pattern>/normal A;
A bit of an explanation:
This finds all lines starting with (due to the ^) the specified <pattern> and then executes the command normal A; which switches to normal mode, then does A to append to the line, followed by typing ;.

What is the shorthand for the first argument of the previous comment in bash? last is '$!'

What is the special character which indicate first ?
if we do
$ vi .bashrc
$ source !$
this !$ will replaced by .bashrc
because ! means previous line(am I correct?), $ means last word (for sure)
then what is first?
I want to insert some string in every line in vi editor using
:%s/find-key-word/replaced-keyword/g
in here, if I put
:%s/$/example/g
in vi editor, it will append in all lines with example.
I want to insert all in front of all string every line.
I know I can use visual block (ctrl+v) and select all front lines and insert (shift+i) insert some word and escape(esc) will do the same... but I want to do in one shot..
please let me know how to do..
Thanks in advance
There are two questions, so you are getting two kinds of answers :)
The bash command history has only a passing similarity to the vi regular expression syntax.
^ is the beginning of line in vi. $ is the end of line in vi.
!!:0 is one way of accessing the first word of the previous command in bash
!$ is one way of accessing the last word of the previous command in bash
To indicate beginning of line, the symbol used is:
^
See an example:
$ cat a
hello!
this is me
testing some
stuff
$ sed 's/^/XXX/' a
XXXhello!
XXXthis is me
XXXtesting some
XXXstuff
The character you are looking for is ^.
For example, :%s/^/example/g will prepend all lines with the string example.
In bash, !^ refers to the first argument of the previous command, and !$ the last argument.

Merge the current line with the line above in Vim

I have a pretty long list, like so:
itemOne
itemTwo
,itemThree
itemFour
itemFive
,itemSix
,itemSeven
Each line that starts with a comma needs to be merged with the line above, like so:
itemOne
itemTwo,itemThree
itemFour
itemFive,itemSix,itemSeven
How can I do this?
An efficient way is to use the following :global command:
:g/^,/-j!
You could do it with:
:%s/\n,/,/g
Try this substitution:
:%s#\n\ze,##
Explanation:
%s# begin a substitution on all lines.
\n match the newline character.
\ze, set the end of the match before the comma so the comma will not be replaced.
## replace with nothing (to remove the newline character).

How can I save each match in a different file with Vim

I would like to save the output of g/pattern1/,/pattern2/ to a file (for each match, a different file).
e.g.
def
.......
end
def
.......
end
you would end up with a file for each "def...end".
Tried using tempname() like so:
g/pattern1/,/pattern2/exe 'w ' . tempname() but this fails with no range allowed for exe
also tried
g/pattern1/,/pattern2/w `tempname()`
to get tempname() evaluated but this failed with a "too many filenames" error.
What am I missing? Can this be done by using global and other commands, or would you need vimscript to do it?
g/pattern1/,/pattern2/execute "w ".fnameescape(tempname())<CR>
Use execute whenever you want to insert variable into command-line if it is a mapping. If it is not, try using
g/pattern1/,/pattern2/w <C-r>=fn<Tab>e<Tab>te<Tab>)<CR><CR>
Here fn<Tab> with wildmode=longest,list:full will expand to fname, fnamee<Tab> will expand to fnameescape(, te<Tab> will expand to tempname(), so this is a short way to input <C-r>=fnameescape(tempname())<CR>. You can omit fnameescape if you are sure that tempname will not return filename with special characters.
And note that backticks will not execute vimscript function, they execute shell command, so `tempname()` tries to call tempname() in a shell and substitute filename with the result of this call. According to the help, you should have written `=tempname()`.
Try :g/pattern1/normal! :.,/pattern2/w `tempname()`^M with ^M entered as CTRL-V then ENTER

Resources