Google a word in Vim and display content of first result - vim

Is there a mapping to Google the marked word in Vim and display the first result (NOT the search result)?
This is what I've tried so far:
nmap <c-l> :ConqueTerm lynx -accept-all-cookies
-nopause 'google.com/search?q="something"'<cr>10<down>'
Obviously, 10<down> won't work. Also, "something" should be replaced by marked word.

Solved. This is the line:
vnoremap <leader>go :<c-u>exec "!lynx -accept-all-cookies -nopause -cmd_script=~/.vim/lynx_script/lynx_google 'google.com/search?q=" . #* . "'"<cr>
Vim must be compiled with +x11 for register #* to work.
<c-u> will clear the leading '<,'> of visual mapping.
The cmd_script of lynx is simply key Down Arrow followed by key ^J for enter.

Related

why is `ctrl -h` not work in insert mode?(version gvim 8.1.1)

In "Practical Vim" (second edition), the writer says <c-h> can delete back a character. I've tried in git bash, and it worked. However, it doesn't work in gvim in win10 as expected.
When I enter something in insert mode, I can use <c-h>, <c-w> and <c-u> before I leave insert mode. When I switch to insert mode without entering anything, <c-h>, <c-w> and <c-u> don't work.
Note that <c-h> is not mapped (:map <c-h> prints No mapping found).
Vim, by default, does only allow to delete characters that you typed since you entered insert mode. It does not allow to delete chars before the point where you started inserting. This behavior is inherited from the original Vi.
To change that, Vim has the option 'backspace'. It configures how <Backspace>, <Del>, <c-w> and <c-u> work. Add the following to your _vimrc and Vim will behave as you expect:
set backspace=indent,eol,start
Then you can backspace over autoindent, start of insert and end-of-lines.
See :help 'backspace'.

vim vnoremap: E488 Trailing characters

I'm using vim to program and I just want to make a shortcut for comment.
Here is how I set in .vimrc:
vnoremap <F7> :%s/^/\/\//g
I just want to add // in front of each selected line. However, when I press <F7> and press Enter in visual mode, I get an error:
E488 Trailing characters
Note that when you press F7 it just simulates pressing all the keys in the string. As soon as it presses : it gets into a state
:'<,'>
When it then types in all the rest of your command it gets into:
:'<,'>%s/^/\/\//g
Which is meaningless (% after '<,'> doesn't make sense). If you just remove % from your command, it will already work. Even better, add <CR> at the end so that you don't need to press Enter:
vnoremap <F7> :s/^/\/\//g<CR>

Can't map Home button in vimrc

I want to map the Home button so vim goes to the first non blank character in vim. But mapping the home button doesn't do anything? If I map another key, then it works correctly.
See below my vimrc file:
map <Home> 0w
imap <Home> <ESC>0wi
The above doesn't work. While the following works (Ctrl-F for example)
map <C-f> 0w
imap <C-f> <ESC>0wi
Isn't there a way to map Home key to this? I really need it, because I got used to this when working with Notepad++, Sublime text 2, Visual Studio,...
I also tried the following, with no results. When using another key, it works again...
http://vim.wikia.com/wiki/Smart_home
From Vim FAQ (also available through this nice plugin):
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?
1) First make sure, the key is passed correctly to Vim. To determine if
this is the case, put Vim in Insert mode and then hit Ctrl-V (or
Ctrl-Q if your Ctrl-V is remapped to the paste operation (e.g. on
Windows if you are using the mswin.vim script file) followed by your
key.
If nothing appears in the buffer (and assuming that you have
'showcmd' on, ^V remains displayed near the bottom right of the Vim
screen), then Vim doesn't get your key correctly and there is nothing
to be done, other than selecting a different key for your mapping or
using GVim, which should recognise the key correctly.
This way you can check if the home key you are pressing is the same that Vim understand as <Home>.
Another possibility is that some other mapping is interfering with this one. You could try the following:
noremap <Home> 0w
inoremap <Home> <ESC>0wi
Edit:
It seems the problem is that your terminal is sending a home keycode that Vim isn't recognizing as <Home>.
I believe that the best solution is make Vim recognize that key correctly, so you can move your .vimrc to other terminals/systems without changes.
From :h xterm-end-home-keys:
On some systems (at least on FreeBSD with XFree86 3.1.2) the codes that the
<End> and <Home> keys send contain a <Nul> character. To make these keys send
the proper key code, add these lines to your ~/.Xdefaults file:
*VT100.Translations: #override \n\
<Key>Home: string("0x1b") string("[7~") \n\
<Key>End: string("0x1b") string("[8~")
If that doesn't work, you could try :set t_kh=^V^[[1~. If it work you can enclose it on a check of your terminal type.
Additional information can be found at :h terminal options
Edit 2:
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?
:
:
3) If the key is seen, but not as itself and not as some recognizable
key, then there is probably an error in the terminal library for the
current terminal (termcap or terminfo database). In that case >
:set term?
will tell you which termcap or terminfo Vim is using. You can try to
tell vim, what termcode to use in that terminal, by adding the
following to your vimrc: >
if &term == <termname>
set <C-Right>=<keycode>
endif
where <termname> above should be replaced by the value of 'term'
(with quotes around it) and <keycode> by what you get when hitting
Ctrl-V followed by Ctrl-Right in Insert mode (with nothing around
it). <C-Right> should be left as-is (9 characters). Don't forget that
in a :set command, white space is not allowed between the equal sign
and the value, and any space, double quote, vertical bar or backslash
present as part of the value must be backslash-escaped.
Now you should be able to see the keycode corresponding to the key
and you can create a mapping for the key using the following command: >
:map <C-Right> <your_command_to_be_mapped>
For more information, read
:h map-keys-fails
:h map-special-keys
:h key-codes

Mapping a key in VIM

I've tried:
:map <F2> :.y" :g/<C-R>"/d<CR>
No luck :(
What this does, yank the current line into register "
Then, globally, delete lines that match exactly the line in the register.
It works dandy when I do it manually.
:vmap <F2> ["]yy<ESC><ESC> :g/<C-R>"/d<CR>
Similar to above - I select a few words, whatever - I make a selection, then yank it to register ". I then globally, delete the lines that match whats in the register.
It works dandy when I do it manually.
What am I doing wrong?
You might try this for the first one:
:nnorempa <F2> :silent exe "g/".getline(".")."/d"<CR>
For the second, something like this if you want to delete only the words:
:vmap <F7> y:silent exe "%s/".#"."//g"<CR>
And this if you want to delete the matching lines:
:vmap <F7> y:silent exe "g/".#"."/d"<CR>
You have remapped F2 to :.y" :etc. You need <cr> not a simple space. If you type :.y" in vim and don't hit ENTER but space, nothing will happen.
So:
:nnoremap <f2> :.y"<CR>:g/<C-R>"/d<CR>
could do it.
Still, warning, if your line contains any of /\*[~$^. this could fail. You could use the expression register in order to escape in-place:
:nnoremap <f2> :.y"<CR>:g/<c-r>=escape(#", '/\*[~$^.')<cr>/d<cr>
Still better, without overwriting your default (") register is:
:nnoremap <f2> :g/^<c-r><c-o>=escape(getline('.'), '/\*[~$^.')<CR>$/d<cr>
which will delete all identical lines. Still note that 'ignorecase' or 'smartcase' matter.
First of all - make sure you're using vim :)
vim --version
Here's the mapping I was going for. As I go through lots of data in log files, this will be incredibly useful. Select the area you want to eliminate, then and all instances of highlight area is done for.
NOTE: This does NOT go through the highlighted text and escape any regex characters. So a /, *, ^ will foul it up.
:map <F2> y:g/<C-R>"/d<CR>
:)

vim: how to select pasted block

Is there a vim command to directly select a block of text which has just been pasted?
ps. I know about gv to reselect a block after exiting visual mode. It doesn't apply to this case.
If you want to select it just after paste (before you change anything else), use
nnoremap <expr> gV "`[".getregtype(v:register)[0]."`]"
. [ and ] marks point to start and end of the last change, v:register is set to the last register used (which is register used for the paste command unless you, for example, yank something), [0] selects only first byte of register type (it is required because for blockwise register it returns <C-v>{width}) and register type is one byte which is just the same as the keystroke you should use in normal mode to invoke visual mode.
I saw this solution somewhere on SO, you may want to search for it in order to get some alternatives.
In my case I have this map:
:nnoremap gp `[v`]
After more research I think the better solution is:
" https://vim.fandom.com/wiki/Selecting_your_pasted_text
nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
I have had the following maps in my vimrc forever:
nnoremap <leader>p `[V`]
nnoremap <leader>[ `[V`]<
nnoremap <leader>] `[V`]>
They do the following:
visually select the recently pasted block
de-indent the recently pasted block
indent the recently pasted block
I probably use the indent ones even more than the selection one.

Resources