I've read some posts about copying and inserting via block/visual selection but I think something is wrong on my side. If I copy the word hello, then move the cursor onto the second " of the next line, block select all " and press shift+p in order to paste hello between the ", it pasts hello on each line but removes the second ".
origin:
"hello"
""
""
""
result:
"hello"
"hello
"hello
"hello
I think you are doing it the wrong way in visual block mode. If I understand you correctly, you want to insert here inside the quotes in line 2, 3, 4, right?
This is the correct way:
Copy the word here.
Go to first " in line 2 and press Ctrl-V to start visual block mode.
Press 2j to also select line 3 and line 4.
Press A (note, this is capital A!)
Now we are in insert mode, press Ctrl-R, followed by ", then press <ESC> to leave insert mode.
After these steps, you should get what you want. For more info, open neovim and try read :h v_b_A.
I suggest trying to load neovim with no config file:
nvim -u NONE
The paste the text you want to change and test to se what happens.
In normal mode you can use: "+p, whereas on insert mode you can try:
Ctrlr+
Related
I am trying to comment many lines in vim in my python script using an Insert in Visual Block mode (Ctrl-V, highlight all the lines, Shift-I, type "# ", Esc twice), but when it does this it will remove all the tabs from the first line in the block and only insert the "# " on the first line. It will not effect any of the other lines highlighted. In short, it only effects the first line and incorrectly deletes the tabs of the one line that is effected. Does anyone know why this is and/or what I am doing wrong?
I am aware I can use this command :s/^\(\t*\)/\1# but I would like to do this with Visual Block.
Before:
if(r.is_redirect == False):
with open(filename,'wb') as output:
output.write(r.content)
Ctrl-V, j, j, Shift-I, "# ", Esc, Esc
After:
# if(r.is_redirect == False):
with open(filename,'wb') as output:
output.write(r.content)
EDIT: Add proper indentation on Before code
I want to wrap some code :
myObj.text;
with a function call where the code is passed as an argument.
console.log(myObj.text);
I've thought about using surround.vim to do that but didn't manage to do it.
Any idea if it's possible ? I
With Surround in normal mode:
ysiwfconsole.log<CR>
With Surround in visual mode:
Sfconsole.log<CR>
Without Surround in normal mode:
ciwconsole.log(<C-r>")<Esc>
Without Surround in visual mode:
cconsole.log(<C-r>")<Esc>
But that's not very scalable. A mapping would certainly be more useful since you will almost certainly need to do it often:
xnoremap <key> cconsole.log(<C-r>")<Esc>
nnoremap <key> ciwconsole.log(<C-r>")<Esc>
which brings us back to Surround, which already does that—and more—very elegantly.
I know and use two different ways to accomplish this:
Variant 1:
Select the text you want to wrap in visual mode (hit v followed by whatever movements are appropriate).
Replace that text by hitting c, then type your function call console.log(). (The old text is not gone, it's just moved into a register, from where it will be promptly retrieved in step 3.) Hit <esc> while you are behind the closing parenthese, that should leave you on the ) character.
Paste the replaced text into the parentheses by hitting P (this inserts before the character you are currently on, so right between the ( and the )).
The entire sequence is v<movement>c<functionName>()<esc>P.
Variant 2:
Alternatively to leaving insert mode and pasting from normal mode, you can just as well paste directly from insertion mode by hitting <ctrl>R followed by ".
The entire sequence is v<movement>c<functionName>(<ctrl>R")<esc>.
You can use substitution instruction combined with visual mode
To change bar to foo(bar):
press v and select text you want (plus one more character) to surround with function call (^v$ will select whole text on current line including the newline character at the end)
type :s/\%V.*\%V/foo\(&\)/<CR>
Explanation:
s/a/b/g means 'substitute first match of a with b on current line'
\%V.*\%V matches visual selection without last character
& means 'matched text' (bar in this case)
foo\(&\) gives 'matched text surrounded with foo(...) '
<CR> means 'press enter'
Notes
For this to work you have to visually select also next character after bar (^v$ selects also the newline character at the end, so it's fine)
might be some problems with multiline selections, haven't checked it yet
when I press : in visual mode, it puts '<,'> in command line, but that doesn't interfere with rest of the command (it even prevents substitution, when selected text appears also somewhere earlier on current line) - :'<,'>s/... still works
I got this code snippet from a tutorial and I would like to search and replace all occurrences of “ and ”, with normal double quote ".
How can I efficiently do it in Vim?
{
“runtime”: {
“DDP_DEFAULT_CONNECTION_URL”: “http://127.0.0.1:8100”
},
“import”: [
“meteor-base#1.0.4”,
“mongo#1.1.14”,
“reactive-var#1.0.11”,
“jquery#1.11.10”,
“tracker#1.1.1”,
“standard-minifier-css#1.3.2”,
“standard-minifier-js#1.2.1”,
“es5-shim#4.6.15”,
“ecmascript#0.6.1”,
“shell-server#0.2.1”
]
}
Thank you.
You can show ascii code of character using ga in normal mode.
You can also put arbitrary utf8 code using Ctrl+vuhhhh for characters with code 0000 <= hhhh <= FFFF and Ctrl+vUhhhhhhhh for the rest in insert mode and command mode so when starting in normal mode you need to type, similar to #shash678 answer:
:%s/Ctrl+vu201c/"/g
In command-line mode you can use the following command to find each occurrence of “ and ” (in all lines), and replace them with ":
:%s/[“”]/"/g
EDIT:
To do this, (assuming you have the file open in vim):
1) hit the ESC key to ensure you are Normal mode.
2) hit : , you will notice : has appeared in box at the bottom of vim (that is the command line).
3) type the rest of the command for finding each occurrence of “ and ” (in all lines), and replacing both with ".
4) hit the ENTER key.
5) your done! The command line will say something like made 56 substitutions on 13 lines.
To rephrase #mucka's answer:
:%s/[Ctrl+vu201cCtrl+vu201d]/"/g
The above is similar the command below, but it allows you to retrieve the special chars using hex code with Ctrl+v:
:%s/[“”]/"/g
To show hex code any character in your file, move your cursor over that character in normal mode and execute ga.
highlight the first smartquote before runtime, then hit e twice to select the other.
press y to yank. You now have a yank register with both types of smart quotes in it.
type :%s/[ and then press Ctrl+r and then " to paste the yanked text. Move backwards and remove the word runtime from the search, then move to the end and type ]/"/g and hit Enter
I am writing a simple Pong game in TI-Basic but the editor won't let me insert a line into the code I've already written.
For example
print "Hello world"
<--Where I want to insert the code
print "hello again"
x = 5
If I try to insert code it simply writes over previous code, I cannot create a new line.
Are you coding on the TI-84 calculator? If so, all you have to do is press "2nd" and then "DEL" (to the left of the arrow-pad). You will note the blue text, "INS", above the "DEL" key. Then simply press ENTER to add a new line.
There is a relevant section on page 511 of this TI-83 manual.
Inserting and Deleting Command Lines To insert a new command
line anywhere in the program, place the cursor where you want the new
line, press 2nd INS, and then press
ENTER . A colon indicates a new line.
To delete a command line, place the cursor on the line, press
CLEAR to clear all instructions and expressions on the
line, and then press DEL to delete the command line,
including the colon.
To insert text from a file in the current Vim buffer I use :r filename to insert the text below the cursor or :0r filename to insert in the first line.
How do you insert the contents of a file where [Cursor] is located?
Actual line with some coding [Cursor] // TODO for later version
Line below actual line ...
This inserts the contents of the file whose path is at the cursor position:
:r <cfile>
Insert a line break, read the file, and then take out the line break...
I propose Ctrl-R Ctrl-O = join(readfile('filename','b'), "\n")
Other solution:
Possibly open the other file in another window, use :%yh (h is a register name) and in your original file, in normal mode use "hp or "hP or in insert mode, Ctrl-R Ctrl-O h
To expand on the accepted answer with actual code, since I tried the suggestion and it worked nicely;
Here is an example of it working to insert a little php snippet:
`nnoremap <leader>php a<CR><ESC>:.-1read $SNIPPETS/php<CR>I<BS><ESC>j0i<BS><ESC>l`
In general the syntax is
`nnoremap [KEY SEQUENCE] a<CR><ESC>:.-1read [FILE PATH]<CR>I<BS><ESC>j0i<BS><ESC>l`
Just to break it down:
nnoremap : I'm about to define a new key mapping for normal mode
<leader>php : I would like to trigger the command sequence when this key combination is pressed. In my case <leader> is , so I type ,php to trigger the command.
Now for the command, bit by bit:
a<CR><ESC> : go into insert mode (after the cursor), insert a line break, go back into normal mode.
:.-1read <file><CR> : this enters the desired file on the line above the current line.
I<BS><ESC> : jump to the start of the line, delete line break, return to normal mode.
j0i<BS><ESC>l : Go down one line (to the remainder of the line that you were initially on), jump to the start, enter insert mode, delete the line break, return to normal mode.
l : position the cursor to the right of the pasted file (this just made the command work more like how you expect it to).
note
You have a choice of whether to paste before or after the cursor. I have chosen in this example to paste after the cursor because that is how the p command usually works to paste yanked text. Alternately, to paste before the cursor you should change the a at the start of the command to an i. You could use one of these exclusively, or you could bind them both to different but related key sequences. For example:
`nnoremap <leader>php i<CR><ESC>:.-1read $SNIPPETS/php<CR>I<BS><ESC>j0i<BS><ESC>l`
Could paste text before the cursor,
and :
`nnoremap <leader><leader>php a<CR><ESC>:.-1read $SNIPPETS/php<CR>I<BS><ESC>j0i<BS><ESC>l`
could paste text after the cursor. Or vice versa. I have made 'before the cursor' easier to trigger because I use it more often when pasting in-line.
other note
This solution is mainly useful if you're reading from a file that you expect to use often enough that it's worthwhile setting up a key mapping for it (ie reading a snippet from a file). This doesn't really help if you just want to read in a random files whenever since you won't have the key mapping ready.
Since it is very formulaic, I'm sure it would be possible to define a function that would take the file name as an argument and do the same thing though. I've never written a function in vimscript though so if someone who knows more feels like editing this answer to contain that solution - I urge them to do so!
" put this in your ~/.vimrc
" in insert mode press ,n
"
imap ,n <c-r>=expand("%:p")<cr>
Read more in wikia.