In VIM, is it possible to use the selected text in the substitute clause without retyping it? - vim

Let's say I have a word selected in visual mode. I would like to perform a substitution on that word and all other instances of that word in a file by using s//. Is there a way to use the highlighted text in the s/<here>/stuff/ part without having to retype it?

Sure. If you selected the word, just "y"ank it, and then type:
:%s/<ctrl-r>"/something else/g
Where is pressing ctrl key with r key, and " is just " character.
All keypresses:
y:%s/<ctrl-r>"/what to put/g<enter>

If you searched for your text before you can use
CTRL-R /
to insert the last search item in your search and replace string.
You can check this page for other similar tricks:
http://www.vim.org/htmldoc/insert.html

You don't have to yank the word, place your cursor on the word and then:
:%s/<C-r><C-w>/bar/g

Another way to access register contents from the command line is via # variables. So if you yank text into the default register, it'll be in a variable called #".
:exe '%s/' . #" . '/stuff/'
Here's a mapping to make this easy to type:
vmap <Leader>s y:exe '%s/' . #" . '//g'<Left><Left><Left>
Now you can highlight something in visual mode, type \s, type your replacement and hit Enter. depesz's version also makes a good mapping (almost exactly as he typed it):
vmap <Leader>s y:%s/<c-r>"//g<Left><Left>

Related

vim command for adding Parentheses efficiently

what is the vim command can put something into Parentheses efficiently and then I can use . to repeat it? ?
for example, data['max'] to (data['max'])
visually select your text:
viW
change it with the opening parenthesis, followed by the selected text, followed by the closing parenthesis:
c(<C-r><C-o>")<Esc>
If you are confident with text-objects, this can be done in one step:
ciW(<C-r><C-o>")<Esc>
which can be repeated with ..
I'd recommend using two plugins for this. Tim Pope's vim-surround and vim-repeat.
Just follow the links and install those plugins.
Inorder to put parenthesis around a word, just do ysiw)
Otherwise, select text in visual mode, press S(Capital S) and type in paranthesis
You can use a map for that.
:nmap \. I(<ESC>A)
You can put this line in your ~/.vimrc file.
When you press \ and . in normal mode, it will add a opening bracket at the start of the line and closing bracket at the end of the line.
Assuming that you are using a language like Tcl and surround the hash element with a bracket,
You can try this by keeping the cursor anywhere on the hash name,
: nmap \. bi(<Esc>f]li)
This will surround the expected one with circular braces.
You can use a map in vimrc:
xnoremap <leader>a <ESC>`>a)<ESC>`<i(<ESC>
And use <leader> and a to add parentheses efficiently

Using Ack.vim on visual selection

Currently I have this mapping in my ~/.vimrc
noremap <Leader>a :Ack <cword><cr>
which enables me to search for a word under the cursor.
I would like to search for a current visual selection instead, because sometimes words are not enough.
Is there a way I can send visual selection to ack.vim?
You can write a visual-mode map that yanks the highlighted text and then pastes it verbatim (properly escaped) onto the vim command-line:
vnoremap <Leader>a y:Ack <C-r>=fnameescape(#")<CR><CR>
This solution uses the <C-r>= trick that allows you to enter a kind of second-level command-line, which allows you to enter any vimscript expression, which is then evaluated, and the result is stringified and pasted onto the (original, first-level) command-line where the cursor is.
A slight disadvantage of this approach is that it commandeers the unnamed register, which you may not want.
While bgoldst's answer should work just fine, you could also consider my fork of ack.vim: https://github.com/AndrewRadev/ack.vim
It comes with a working :Ack command in visual mode, and a few other extras that I've summarized at the top of the README.
At the time of this writing this is the default behaviour of Ack.
Just do the following:
move your cursor on any word in normal mode (for instance, hit Esc button to enter in normal mode, you know...)
type :Ack with no argument
it will search for the word under the cursor
Usually I select text during a search in a file (for instance put cursor inside word and type * repeateadly) the type :Ack to look for that word in other files of the project.

macvim: how to paste several times the same yanked word?

Each time i copy a word and want to replace it for several words, i do:
yank the word
enter visual mode, select the word to be replaced and paste the yanked word.
After this process, the replaced word will be yanked and cannot continue replacing new words bceause i lost the first yanked word. So, i must copy again the first yanked word.
Could anybody guide to me on how to achieve my goal in an efficient way? It could be enough if my yanked word would not get changed.
I would suggest explicitly using a register for your yank and paste.
"ayw or however you chose to yank your word.
"ap to paste.
In this case I've used the a register but you could use whichever suits you.
It has been answered before: Vim: how to paste over without overwriting register.
Overall, crude vnoremap p "_dP mapping will almost get you there, but it won't work well in a few edge cases (e.g. if a word you're replacing is at the end of the line).
The superior approach is to use this crazy-looking snippet (I wish I knew Vimscript at least half as good as the author of this):
" replace visual selection without overwriting default register
function! RestoreRegister()
let #" = s:restore_reg
return ''
endfunction
function! s:Repl()
let s:restore_reg = #"
return "p#=RestoreRegister()\<cr>"
endfunction
vnoremap <silent> <expr> p <sid>Repl()
Personally, I'd favour doing :s/word/replacement words/gc.
Alternatively, you could use "_de to delete the word to be replaced. "_ says use the "black hole" buffer to prevent losing the existing default buffer contents.
Perhaps a bit better than this is to yank the replacement words into an alternative named buffer (e.g. "a3ye), then you can delete the work to be replaced (de) and paste the named buffer "ap.
One addition to #Randy Morris answer: instead of specifying register explicitly in both cases, you can specify it only in the second one, see :h quote0 («Numbered register 0 contains the text from the most recent yank command...»). In this case using a register is better (as it is much easier to type), but if you say you are replacing words, you may want to use ciw<C-r>0 and then one . for each other word you want to replace.
I use this mapping to replace the currently selected text with default register without yanking it:
vnoremap <leader>p "_dP
I dont use yank, but ciw and then repeat with .
For instance:
Go to somewhere inside the word you want to replace.
Do ciw <type new word> Esc
Go to somewhere inside the next word you want to replace.
Press . to repeat the last replace.
Advanced:
You can also first find the word with /<word> and then use ciw <new word>.
Then you dont have to move to the word yourself before pressing . but you can just use n to go to the next (or N to go to the previous).

How do I insert a linebreak where the cursor is without entering into insert mode in Vim?

Is possible to insert a line break where the cursor is in Vim without entering into insert mode? Here's an example ([x] means cursor is on x):
if (some_condition) {[ ]return; }
Occasionally, I might want to enter some more code. So I'd press i to get into insert mode, press Enter to insert the line break and then delete the extra space. Next, I'd enter normal mode and position the cursor before the closing brace and then do the same thing to get it on its own line.
I've been doing this a while, but there's surely a better way to do it?
For the example you've given, you could use rEnter to replace a single character (the space) with Enter. Then, fspace. to move forward to the next space and repeat the last command.
Depending on your autoindent settings, the above may or may not indent the return statement properly. If not, then use sEnterTabEsc instead to replace the space with a newline, indent the line, and exit insert mode. You would have to replace the second space with a different command so you couldn't use '.' in this case.
A simple mapping to break the line at the cursor by pressing Ctrl+Enter:
:nmap <c-cr> i<cr><Esc>
essentially enters 'insert' mode, inserts a line break and goes back to normal mode.
put it in your .vimrc file for future use.
Here's how to create a macro that inserts a newline at the cursor whenever you press 'g' while not in insert mode:
From within vim, type:
:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]
Where:
[Ctrl+V] means hold the Ctrl key and press 'v'
[Enter] means press the Enter key
[Esc] means press the Esc key
You'll see the following at the bottom of your vim window until you press the final Enter:
:map g i^M^[
Explanation:
[Ctrl+V] means "quote the following character" -- it allows you to embed the newline and escape characters in the command.
So you're mapping the 'g' key to the sequence: i [Enter] [Escape]
This is vim for insert a newline before the cursor, then exit insert mode.
Tweaks:
You can replace the 'g' with any character that's not already linked to a command you use.
Add more to the command, e.g. f}i^M^[O -- This will find the } and insert another newline, then escape from insert mode and Open an empty line for you to enter more code.
You can add the command to your .vimrc or .exrc file to make it permanent. Just omit the colon from the beginning, so the command starts with "map"
Enjoy!
If you're usually expanding a one line block to three lines, try substitution. Change the opening bracket into bracket/return, and the closing bracket into return/bracket.
The command for substituting bracket/return for bracket looks like this:
:s/{/{\r/
Since you want to use this often, you could map the full sequence to an unused keystroke like this:
:map <F7> :s/{/{\r/ ^M :s/}/\r}/ ^M
Where you see ^M in the sequence, type [Ctrl-V], then press enter.
Now with your cursor anywhere on your sample line, press the mapped key, and the carriage returns are added.
Check :help map-which-keys for advice on selecting unused keystrokes to map.
Assuming you're okay with mapping K to something else (choose a different key of your liking), and using marker ' as a temporary marker is okay why not do this?
:nmap K m'a<CR><Esc>`'
now pressing K in normal mode over the character after which you want the line break to occur will split the line and leave the cursor where it was.
Basically, when you split a line you either want to just insert a carriage return, or in the case that you're on a space, replace that with a carriage return. Well, why settle for one or the other? Here's my mapping for K:
"Have K split lines the way J joins lines
nnoremap <expr>K getline('.')[col('.')-1]==' ' ? "r<CR>" : "i<CR><Esc>"
I use the ternary operator to condense the two actions into one key map. Breaking it down, <expr> means the key map's output can dynamic and in this case hinges on the condition getline('.')[col('.')-1]==' ' which is the long winded way to ask vim if the character under the cursor is a space. Finally, the familiar ternary operator ? : either replaces the space with linebreak (r<CR>) or inserts a new one (i<CR><Esc>)
Now you have a lovely sister key map to the J command.
Vim will automatically kill any whitespace to the right of the cursor if you break a line in two while autoindent (or any other indentation aid) is enabled.
If you do not want to use any of those settings, use s instead of i in order to substitute your new text for the blank rather than just inserting. (If there are multiple blanks, put the cursor on the leftmost and use cw instead.)
In fact you need the following combined operations:
Press v to enter Visual Mode
Select the line you want to split
Press : to enter in Command Mode
s/\s/\r/g
Done
If you have the input:
aaa bbb ccc ddd
and want to output
aaa
bbb
ccc
ddd
You can use the command
f r<ENTER>;.;.
o ESC command will do it for you.
Set this key mapping in your vimrc
:map <C-m> i<CR><Esc>h
Then press Ctrl+m if you want to use it in your vim.
IMHO, the built-in mapping gs is not a useful mapping (put vim to sleep), one could use this for splitting:
nmap gs i<CR><ESC>
In Vrapper you can use gql which will split a line without entering insert mode, but may not always maintain indentation.
I found this to be the most faithful implementation of what I'd expect the opposite behaviour to J
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
It does the simplistic new line at cursor, takes care of any trailing whitespace on the previous line if there are any present and then returns the cursor to the correct position.
i <cr> <esc> - this is one of the most common solutions suggested, it doesn't delete non-whitespace characters under your cursor but it also leaves you with trailing whitespace
^mw - goto start of new line and create a mark under w
gk - go up one line
:silent! s/\v +$//<cr> - regex replace any whitespace at the end of the line
:noh<cr> - Clear any search highlighting that the regex might have turned on
`w - return the the mark under w
Essentially combines the best of both r<esc><cr> and i<cr><esc>
Note: I have this bound to S which potentially overwrites a useful key but it is a synonym for cc and since I don't use it as often as I do splits I am okay with overwriting it.
This mapping will break up any one-line function you have. Simply put your cursor on the line and hit 'g' in normal mode:
:map g ^f{malr<CR>`a%hr<CR>`a
This assumes that you have a space after the opening brace and a space before the closing brace. See if that works for you.

How do I specify "the word under the cursor" on VIM's commandline?

I want to write a command that specifies "the word under the cursor" in VIM. For instance, let's say I have the cursor on a word and I make it appear twice. For instance, if the word is "abc" and I want "abcabc" then I could type:
:s/\(abc\)/\1\1/
But then I'd like to be able to move the cursor to "def" and use the same command to change it to "defdef":
:s/\(def\)/\1\1/
How can I write the command in the commandline so that it does this?
:s/\(*whatever is under the commandline*\)/\1\1
While in command-line mode, CTRL+R CTRL+W will insert the word under the cursor.
See the help for c_CTRL-R for a listing of all the other special registers:
:help c_CTRL-R
<cword> is the word under the cursor (:help <cword>).
You can nmap a command to it, or this series of keystrokes for the lazy will work:
b #go to beginning of current word
yw #yank to register
Then, when you are typing in your pattern you can hit <control-r>0<enter> which will paste in your command the contents of the 0-th register.
You can also make a command for this like:
:nmap <leader>w :s/\(<c-r>=expand("<cword>")<cr>\)/
Which will map hitting '' and 'w' at the same time to replace your command line with
:s/\(<currentword>\)/
yiwP
yiw: Yank inner word (the word under the cursor). This command also moves the cursor to the beginning of the word.
P: Paste before the cursor.
You can then map the e.g.: < ALT > - D to this command:
:nmap < ALT >-D yiwP
Another easy way to do this is to use the * command.
In regular mode, when over a word, type
*:s//\0\0<Enter>
* makes the search pattern the current word (e.g. \<abc\>).
:s// does a substitution using the current search pattern, and \0 in the replacement
section is the matched string.
You can then repeat this behaviour, say over word "def", by either typing the same again, or by typing
*#:
#: just repeats the last ex command, without a need for an <Enter>, in this case the substitution.
You can also record a quick macro to do this using the q command
qd*:s//\0\0<Enter>q
Then repeat it to your hearts content by typing
#d
when over a word you want to double. As this is only one character less than the prior solution, it may not be worth it to you - unless you will be doing other ex-commands between the word-doubling, which would change the behaviour of #:
You need to escape the backslashes within the mapping. You can also include the substitution string within the mapping.
:nmap <leader>w :s/\\(<c-r>=expand("<cword>")<cr>\\)/\\1\\1<cr>
ywPx
will do what you describe.
ywPxw
will also advance the cursor to the next word.
#user11211 has the most straightforward way to duplicate the word under cursor:
yiwP
yank inner word (moves cursor to start of word), paste (before cursor).
eg. straigh[t]forward ----> straightforwar[d]straightforward
[] is cursor
To elaborate...
You probably want to have the cursor following your duplicated word:
yiwPea
straigh[t]forward ----> straightforwardstraightforward[]
NOTE:
yiw
is yank inner word (without whitespace)
yaw
is yank all word (including trailing whitespace).
yawPea
is therefore duplicate word including whitespace, and position cursor.
straigh[t]forward ----> straightforward straightforward[]
" count word (case sensitive)
nmap <F4> :%s/\(<c-r>=expand("<cword>")<cr>\)//gn<cr>

Resources