vim linebreak after hyphen with gq - vim

Is there some way to configure vim's gq function so that it allows line breaking after a hyphen (in a compound word)? e.g.
twentieth-
century
And by the way, I'm not on my own laptop, but on one of the lab, which runs Windows, so any solution not using programs like par or fmt get bonus points :-) (though I'm also interested in solutions using these tools if this is not possible to do this using only vim -- at least I would be able to do it on my laptop).
Thanks in advance.

For the ASCII hyphen (0x2d), this isn't possible. If you're able to use Unicode, you can use the identically-looking Unicode variant (U+2010, cp. http://en.wikipedia.org/wiki/Dash) instead. This can be inserted via Ctrl-V (Ctrl-Q on most Windows installations of Vim), followed by u2010. Or define a digraph for it:
:digraph -- 8208 " hyphen, U+2010
With
:set formatoptions+=m
Vim will
Also break at a multi-byte character above 255.
Voila! If you need to persist the text as ASCII, you could even write mappings / a wrapper around gq that :substitutes the hyphens back and forth.

Related

Accents in vim: Cannot write "â"

I can't get "â" to be written. I can write "Â" though (carrot + capital A).
Any other accent can be written as in any other text editor.
Any suggestions?
Thank you in advance.
You may want to look at the :digraph comamnd in Vim. It will show you the combinations to use with <C-k> to make accented characters. In your case, you want <C-k> followed by a>.
Note: <C-k> means "Control + k" whereas a> means the letter "a" followed by a ">" (greater than sign).
If you are using a latin keyboard layout and are unable to directly type the accented character, check if there is any mapping using it:
:verbose imap â
If so, just remap the command to another key.
<C-K>a^ works for me in Vim 7.3.
You could use digraphs, as pointed out on other answers. But this kind of diacritical character is very common on some languages. If that is true for you, you could set the keymap option:
:set keymap=accents
The list of characters added by this option can be seen in $VIM\keymap\accents.vim.
That being said, this should be working without this option. It is possible that you are with some problem with the value your 'enconding' option, as mentioned here.
First look at digraphs, as mentioned before.
But just to be thorough, and because I haven't seen it mentioned yet, note that any unicode character at all can be inserted via <C-v>uXXXX<cr> (where XXXX is the hexadecimal code point number of the character.) More on this at :help i_^v
For a list of code point values for different characters, try:
https://en.wikipedia.org/wiki/List_of_Unicode_characters
Or use a handy Perl script called unum, which lets you search characters by name, and other fun stuff.
EDIT: markup fix

how to get the digraph key or unicode from a special character

I am configuring tmux & powerline. I would like to change the default separators in tmux status line. I managed to figure out where to do it, however I could not get the special character that I want.
I want to type, in Vim, a left/right pointing triangle that spans the whole line-height, but the only thing I could find is a small triangle (unicode : 25B6,25BA,25C0,25C4...)
There is a big right pointing triangle already in a powerline configuration file, which I could copy and paste, but I want to know its unicode and want a left one. Is there a way to get the unicode from the symbol in Vim or anywhere else?
You can get the codepoint value of a character in Vim by positioning the cursor on the character in question and typing either ga or :ascii.
You can either use ga in command mode or :ascii on the command line (even though it says ascii, it shows information for general encodings)
You may add a permanent preview of current character in your status line (see :h statusline), eg.:
:let &statusline = &statusline . "\ [%03b\ 0x%B]"

vim how to search for URL

How would you search for the following string in vim?
http://my.url.com/a/b/c
I've tried (a la Very No Magic)
:/\Vhttp://my.url.com/a/b/c
But it gives me:
E492 not an editor command: /\Vhttp://my.url.com/a/b/c
You would think there'd be a simple way to search a string literally... I'm not too interested in slash escaping every slash, or writing a complicated search, because I have to rapidly search different URLs in a text file.
I'm not sure why you get not an editor command since I don't. The simplest way to search without having to escape slashes is to use ? instead, e.g.
:?http://my.url.com/a/b/c
" or since the : is not necessary
?http://my.url.com/a/b/c
This does search in the other direction, so just keep that in mind
another way to search forward (from the position of your cursor) without escaping is use :s command.
you could do:
:%s#http://my.url.com/a/b/c##n
then press n to navigate matched text forward, N backwards
If you want to know how many matches in the buffer, use gn instead of n
note that, I said "without escaping", I was talking about the slash, if you want to do search precisely, you have to escape the period. .. since in regex, . means any char.
Can also set the search register directly.
:let #/='\Vhttp://my.url.com/a/b/c'
Then you can use n and N like normal.
Use MacVim (or GVim). Open the non-regex GUI search using ⌘f (or ctrlf on Windows). This is the recommended way to do a non-regex search in Vim. GUI Vim has many improvements over terminal vim like this one and I would highly suggest using it full time if you aren't already.
Searching in vim is just /, not :/. You can search for that string escaping only the slashes: /http:\/\/my.url.com\/a\/b\/c

VIM equivalent for (something like) 6xi?

There's a command in VIM where you can say how many chars to replace, and VIM will put a "$" at that many characters out, and you can type in the replacement for those characters. The original and new text can be different lengths. What's the command for this?
The nearest I can think of is '6s'; that deletes the next 6 characters and leaves you in insert mode, but it doesn't show a '$' at the end - it just removes the material.
It is the behavior of c command when 'cpoptions' contains $ (so it is a default behavior for vi and some configurations of Vim).
The command is s. In your example, it would be 6s.

How to convert all text to lowercase in Vim

How do you convert all text in Vim to lowercase? Is it even possible?
I assume you want lowercase the text. Solution is pretty simple:
ggVGu
Explanation:
gg - goes to first line of text
V - turns on Visual selection, in line mode
G - goes to end of file (at the moment you have whole text selected)
u - lowercase selected area
If you really mean small caps, then no, that is not possible – just as it isn’t possible to convert text to bold or italic in any text editor (as opposed to word processor). If you want to convert text to lowercase, create a visual block and press u (or U to convert to uppercase). Tilde (~) in command mode reverses case of the character under the cursor.
If you want to see all text in Vim in small caps, you might want to look at the guifont option, or type :set guifont=* if your Vim flavour supports GUI font chooser.
use this command mode option
ggguG
gg - Goto the first line
g - start to converting from current line
u - Convert into lower case for all characters
G - To end of the file.
Similar to mangledorf's solution, but shorter and layman friendly
:%s/.*/\L&/g
Many ways to skin a cat... here's the way I just posted about:
:%s/[A-Z]/\L&/g
Likewise for upper case:
:%s/[a-z]/\U&/g
I prefer this way because I am using this construct (:%s/[pattern]/replace/g) all the time so it's more natural.
Toggle case "HellO" to "hELLo" with g~ then a movement.
Uppercase "HellO" to "HELLO" with gU then a movement.
Lowercase "HellO" to "hello" with gu then a movement.
For examples and more info please read this:
http://vim.wikia.com/wiki/Switching_case_of_characters
use ggguG
gg: goes to the first line.
gu: change to lowercase.
G: goes to the last line.
Usually Vu (or VU for uppercase) is enough to turn the whole line into lowercase as V already selects the whole line to apply the action against.
Tilda (~) changes the case of the individual letter, resulting in camel case or the similar.
It is really great how Vim has many many different modes to deal with various occasions and how those modes are neatly organized.
For instance, v - the true visual mode, and the related V - visual line, and Ctrl+Q - visual block modes (what allows you to select blocks, a great feature some other advanced editors also offer usually by holding the Alt key and selecting the text).
If you are running under a flavor of Unix
:0,$!tr "[A-Z]" "[a-z]"
I had a similar issue, and I wanted to use ":%s/old/new/g", but ended up using two commands:
:0
gu:$

Resources