Surround parentheses with its content by extra parentheses - search

I have lines that I want to convert from
(variable=value)
to
((variable=value))
How should I go about doing that from the vim command line?

May be you can use following substitute command
:%s/(.*)/(&)/g
where
.* - all strings of characters of any length and
& - the whole matched pattern

This is commonly done with the surround.vim plugin: First select the single-parentheses block with va(, then surround with another set of parens via S(.

One possible solution (if you have only this pattern in the line):
:.s/.*/(&)
.s ................... current line substitute
.* ................... everything
( .................... open paren
& .................... all pattern searched
) .................... close paren
Or
:norm! I(^[A)
OBS: The simbol ^[ should be typed with Ctrl-vCtrl-[.
Don't forget you can repat the last command : in the current line by typing #: and in the subsequent lines ##.
In normal mode With no plugins you can do this (if the pattern does not repeat a lot):
ca( ................ start changing the pattern (text goes to default register ")
( .................. start typing open parenthesis
Ctrl-r" ............ insert default register
) .................. close parenthesis
OBS: This action is repeatable by typing .

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

In Vim, how do I delete everything but keep what is in double quotes?

I have multiple lines of plain texts and each line is mixed with phrases with/without double quotes. I want to delete everything including quotes but keep what's in double quotes.
Example:
this is some test = "key.properties";
properties = "value.properties";
Result should be:
key.properties
value.properties
:%s/.*\"\(.*\)\".*/\1
Explanation
REPLACE, ANY_STRING, QUOTES, CAPTURE_ANY_STRING, QUOTES, ANY_STRING WITH
:%s/ .* \" \( .* \) \" .* . /\1
If your cursor is at the start of the line, you can do:
f"lyi"Vp
f"l move cursor to first " then left one more space
yi" yank everything within the "s to the anonymous register
Vp select the whole line in visual mode and paste the anonymous register over it
To apply to multiple lines, use the normal command.
:%normal 0f"lyi"Vp
: enter command mode
% set the range as the whole file
normal execute the following key strokes as if they were entered in normal mode
0 move cursor to start of line
f"lyi"Vp see above

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 ;.

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

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.

Terminal command line quick navigation

I would like to move between the command line arguments in a fast way. For example, if I have the following command line:
> do_something_with /very_long_path/to_a_very_long_directory/ more_args
^ ^
I would like to skip the whole path (jump between the ^ symbols). I'm already familiar with word mode (Alt+B and Alt+F) but in some scenarios it's not enough to navigate quickly between the arguments.
In bash, you can set the cursor to the previous given character using the following features:
character-search and character-search-backward features.
ctrl+], (resp. alt+ctrl+]) + searched_character
In your example, you can search backward for a space.
> do_something_with /very/long/path/\ with_spaces\ directory/ more_args
^ ^
Unfortunately, this will not work so well with paths like:
> do_something_with /very_\ long_path/to_a_\ very_long_directory/ more_args
As a sidenote, you can use ctrl+a and ctrl+e to go at the beginning / end of a line.
There are (quote from manual)
shell-forward-word ()
Move forward to the end of the next word. Words are delimited by non-quoted shell metacharacters.
and
shell-backward-word ()
Move back to the start of the current or previous word. Words are delimited by non-quoted shell metacharacters.
I have bound them to Ctrl+Alt+F and Ctrl+Alt+B by adding this to my .inputrc:
"\e\C-f": shell-forward-word
"\e\C-b": shell-backward-word
For vi/vim users ctrl+] + char can be used to quickly navigate to the first occurance of a given char. Which is equivalent to f + char in vi/vim.

Resources