By default, I think my vimrc setttings enable the auto-wrapping. However, sometimes, I would like to see text lines are not wrapped. Is there any command I toggle the text line wrapping enabled and disabled? This would avoid me to exit and to change settings.
I think what you want is:
:set wrap!
This will toggle line wrapping.
More about using ! (bang) to alter commands can be found at:
:help :_!
In your vimrc, create a function such as this:
:function ToggleWrap()
: if (&wrap == 1)
: set nowrap
: else
: set wrap
: endif
:endfunction
Then map a key (such as F9) to call this function, like so:
map <F9> :call ToggleWrap()<CR>
map! <F9> ^[:call ToggleWrap()<CR>
Whenever you press F9, it should toggle your wrapping on and off.
:set nowrap
There is also the linebreak option that controls whether wrapped text is broken at word boundaries or not.
Add the following to have CTRL+W toggle wrapping. You can change it to some other key if you don't want w to be it.
map <C-w> :set wrap!<CR>
The quickref suggests (no)wrap
I happen to like tpope’s unimpaired plugin, where yow will toggle wrap settings.
For those who want to change the text instead of just visual effect, for example in git commit, just press qt in a roll and press enter. This will properly wrap the current paragraph your cursor is in. The paragraph is only delimited by blank lines. or you can select some area to press qt.
I found this by total accident.
Related
While working with some Vim scripts, I found myself typing
:help {subject}
quite a bit. I want CTRL+] (jump to definition of keyword under cursor) functionality, but instead of running :tag {ident} I want it to do :help {subject}, where {subject} is the word under the cursor.
Just press K. If you have set a global 'keywordprg', you need to unset it (or set it to the special :help value) in ~/.vim/after/ftplugin/vim.vim:
:setlocal keywordprg=:help
The simplest solution is
nnoremap K :help <C-r><C-w><CR>
To check the documentation of the keyword under your cursor, you can press Ctrl+] to go to its documentation.
If you have enable mouse support in nvim with the following options:
set mouse=a
you can double click the keyword to go to its documentation.
By the way, to go back to previous position in the help file, press Ctrl+O or Ctrl+T.
The above is also true for Neovim.
References
https://vimhelp.org/
I know it's possible to stop Vim from highlighting on any search, but is there a way to make it highlight on regular searches but not, for instance, substitutions?
I will often highlight a block of text then do something like:
:s/^/#/
to comment out the whole block. But then I have an ugly yellow bar running up and down the left side of my screen and I have to :noh every time to clear it.
I want highlighting to remain on regular /searches. Is this possible?
This doesn't answer your question fully but for me having the same problem it helped: I added this command to easily deactivate the highlighting after a search or a search-and-replace
nnoremap <esc> :noh<return><esc>
(from Vim clear last search highlighting)
My solution: put this in your vimrc file
set nohlsearch
noremap * :set hlsearch<CR>:nohlsearch<CR>*
noremap / :set hlsearch<CR>:nohlsearch<CR>/
noremap ? :set hlsearch<CR>:nohlsearch<CR>?
nnoremap <F4> :set invhlsearch<CR>
inoremap <F4> <ESC>:set invhlsearch<CR>gi
nnoremap <CR> :set nohlsearch<CR>
What it does:
Searches always enable highlights.
Use Enter to turn off highlights until the next search, but not the next substitute. Pressing Enter again will not turn them back on though.
Use F4 to turn off highlights until the next search, but not next substitute. Pressing F4 again will toggle the highlights for the last search pattern --- search OR substitute --- if there was one.
Some notes on highlights that are never really explained well:
When the 'hlsearch' option is on, all future searches/substitutes will turn on "highlight visibility". The current "highlight visibility" is not really an option you can directly query or set independently, but you can independently turn OFF highlight visibility with the command :nohlsearch (this is not the same as :set nohlsearch, because the next search will enable visibility again).
In addition, whenever you run the command :set hlsearch there are two effects: It sets the option AND it makes vim forget if you've ever typed :nohlsearch. In other words, changing 'hlsearch' (either on or off) will force the current "highlight visibility" to logically match.
Can someone help me to find a solution for the following inconvenience? I would like horizontal scrollbar to appear whenever I set the nowrap option, and vice versa when I set it back to wrap.
Currently I use these settings individually to ease my work:
nnoremap <silent> <F3> :if &guioptions=~#'b'<Bar>set guioptions-=b<Bar>else<Bar>set guioptions+=b<Bar>endif<CR>
map <F2> :set nowrap! <CR>
Is there a way to toggle them both at the same time, in concordance?
Let us construct a single command for switching both options accordingly
at once. First of all, it should toggle the wrap option anyway:
:set wrap!
Then, guioptions should be changed depending on whether wrapping is
enabled at the moment of command's execution. If text is wrapped, the
bottom scrollbar should be shown in preparation for wrap to be
disabled:
:set guioptions+=b
Alternatively, if text wrapping is turned off, it should hide the bottom
scrollbar:
:set guioptions-=b
In order to make one command out of the above three, we can use the
expression mapping
:nnoremap <silent><expr> <f2> ':set wrap! go'.'-+'[&wrap]."=b\r"
which turns into the sequence of keystrokes
:set wrap! go+=bEnter
when the wrap option is set (and evaluates to one), or into
:set wrap! go-=bEnter
otherwise (when &wrap evaluates to zero).
Is there a way to indent a selection of lines in Vim, like we have in text editors where we select a bunch of lines and press tab (or shift tab) to indent/unindent the selected lines?
I am talking about general indentation and not related to code indentation.
You can select a set of lines with visual line mode (via Shift + V), and then type
>
and, to dedent,
<
You can also add numeric arguments. Find out you didn't indent enough? Hit gv to re-select your previous selection.
While typing in normal mode, try out Ctrl + T or Ctrl + D to indent or dedent.
Use visual mode as Peter suggests. You can also use X>> where X is the number of lines you want to indent. E.g. 5>> indents five lines from current line and down.
I use the following mappings to indent/unindent:
vmap <TAB> >gv
vmap <S-TAB> <gv
Use TAB to indent and shift-TAB to unindent the visually selected lines.
If a block is selected Vim indents/unindents what is right of the start of
the block.
As suggested by the other answers you can use >. Alternatively, you can automatically correctly indent your code by selecting the set of line in visual mode (using shift+V), and then using =, or using == to indent the current line.
There's a Vim Cast on this topic: Indentation commands
I like Vim Casts. They are informative and pleasant to watch.
I want to be able to click (or cmd+click on my Mac) on a function name in Gvim (or vim with set mouse=a) and have it run the command :tag to follow a ctag, but I don't know how to include a mouse click in a Vim mapping.
A good place to start:
:help click
:help mouse
Perhaps something like this will work:
:map <RightMouse> :tag <CR>