in vim, with
gUw
make the word uppercase, with
guw
make the word lowercase.
how I can convert in one map the upper to lower and the lower to upper?
If you're trying to invert case, you can use ~. Normally this works only on the selection (e.g. visual mode), but if you want it to be more useful, then :set tildeop so you can do ~w or whatever movement command you like.
You can use the tilde operator with global commands too: g~w
The advantage being, that you can then use the . operator to repeat the operation :)
Related
Vim provides a nice way to work on text objects, eg. I can type ciw and I will be changing the whole word no matter where in it my cursor is actually positioned.
Is there something similar for signed numbers? Ie. both positive and negative? Positive numbers are a word anyway, so I can use iw but what if they start with a minus? Ideally I want to have something that can be later repeated (with .) on other numbers in the file, no matter if they are positive or negative.
Concrete example:
Change -100 to 20.
w in Vim means "word", and "word" is a collection of letters from :h 'iskeyword' buffer local option. Hence an exact interpretation of w or iw depends on current option setting. There's no built-in "text objects" for every situation (although some plugins may provide them). There are only "word" and "WORD" (i.e. anything except spaces).
So your options are:
Use ciW if appropriate (number is surrounded by spaces)
Change iskeyword to include "minus" (it will influence many(!) other commands)
Find an existing plugin or write some custom stuff yourself using :h Operator-pending-mode
Use :h ctrl-a. E.g. 120 Ctrl-A will turn "-100" to "20"
In VIM, it's really easy to change a word of text to use uppercase or lowercase:
# in visual mode
# change word to uppercase
gUw
# change word to lowercase
guw
Is there a simple way to modify the word to use initial caps?
Assuming cursor is at the beginning of the word, use
gUl
(if the word was all-lowercase) or
gUllgue
to explicitly make the first letter capital and other lower case.
It's the same that you used, only instead of w (word motion) you use l (one symbol motion).
If the cursor is somewhere in the middle of the word, prepend b (go to the beginning of the word) to the commands above.
You can map some key to do this if you use it often.
I'd suggest moving to the beginning of the word with whatever motion command(s) you want, then pressing ~. This behavior is affected by the tildeop option, see :help ~ and :help tildeop for more info.
Depending on what your use-case is, any of the following may work.
Use ~ to toggle the case of the letter under your cursor.
Use :s/\<\(\w\)\(\w*\)\>/\u\1\L\2/ to search for a word, upper-case the first letter, and lower-case the rest.
guiwgUl to lower-case the word your cursor is on and then upper-case the first letter.
If you're on the word:
bgUl
If you're at the beginning of the word:
gUl
Unpacking that: b goes back one word (or to the beginning of the word you're on), gU upcases over movement, l moves right one character (which will be the first letter in the word).
Side note:
I have a plugin (well, it's not its main purpose though) that is able to convert names between camel case, underscore separated words, etc. Move the cursor on an identifier, and type :NameConvert lower_camel_case for instance (the command supports completion (<tab>, <c-d>) to display all the possible naming schemes)
To install it, you'll need lh-dev, and lh-vim-lib.
There is a function toupper which you can use to make conversion. Even in substitution you can use that. Like find all sentence beginnings, and convert the first character to upper case, as explained here: search and replace
I am trying to use vim properly - to aid me I've mapped my arrow keys to "" so that I am forced to use {hjlk} to move around.
This is causing me a problem when I want to just surround a character with spaces, eg:
"2+3" is better formatted "2 + 3"
Previously I would have put my cursor over the + and typed:
i[space][arrow-right][space][Esc]
That's 5 presses.
To do this without the arrow I seem to need to put the cursor over the + and go:
i[space][Esc]lli[space][Esc]
That's 8 presses.
I can convert the "li" into an "a" which reduces it to 7 presses:
i[space][Esc]la[space][Esc]
Short of writing this into a macro is there a better way of doing it? Is there some magic vim command which will allow me to do it in less than even 5 presses - and some way to generalise it so that I can do it to entire words or symbols, eg if I want to convert 3==4 to 3 == 4?
Personally, I think it makes most sense to destroy what you want to surround, and then repaste it.
c w "" ESC P
Obviously, you can replace both the object and the quotes with whatever you like. To change just one character + to be [space]+[space], you would do
s [space] [space] ESC P
on the +
The first thing that jumps to mind after reading just the title is surround.vim which is an excellent script to do all kinds of useful things along the lines of what you've described.
To solve your specific problem, I would probably position the cursor on the + and:
s[space]+[space][esc]
To change 3==4 into 3 == 4, I might position the cursor on the first =, and:
i[space][esc]ww.
i have been wondering about this as well. i tried with surround.vim, but the naive approach
S<space>
(after making a visual selection) does not work since the space is already taken up as a modifier for adding space to other surrounding character pairs. S<space><cr> adds a ^M in the output. Ss almost works but inserts a space only before.
after asking at tpope/surround.vim on github:
S<space><space>
in visual mode works. alternatively, from normal mode, ysl<space><space> works for a single character
Hah! I've been trying to figure out how to surround a block in spaces for quite a while and I finally found the right combination.
Using surround.vim you say surround selector space space.
So for this specific case I would use visual mode (a good trick for operating on single characters under the cursor BTW) thus: "vs " <- four key presses!
I also have a habit of typing things like argument lists without spaces. With this technique you can just navigate to the second argument using w and say "vws " to visually select a word and surround with spaces.
I prefer visual select mode generally. Also the alternate surround syntax "ysw " excludes the word final comma that is caught by "vw".
You could create a macro with one of the described actions and call it everytime you need it (Like amphetamachine proposed while I was writing) or you could simply search & replace:
:%s/\(\d\)\(+\|-\)\(\d\)/\1 \2 \3/g
You probably have to execute this command two times because it will only find every second occurence of +/-.
EDIT:
This will replace everything without the need to be called twice:
:%s/\d\#<=+\|-\d\#=/ \0 /g
Try positioning your cursor over the '+' and typing this:
q1i[space][right arrow][space][left arrow][esc]q
This will record a quick macro in slot 1 that you can re-use whenever you feel like it, that will surround the character under the cursor with spaces. You can re-call it with #1.
There is also the more versatile one:
q1ea[space][esc]bi[space][right arrow][esc]q
Which will surround the word under the cursor ("==" counts as a word) with spaces when you hit #1.
You could set up a mapping like this (press enter in visual mode to wrap spaces):
:vnoremap <CR> <ESC>`<i<SPACE><ESC>`>la<SPACE><ESC>h
This method allows you to use . to repeat the command at the next +.
Put your cursor over the + and type:
s[SPACE][CTRL-R]"[SPACE][ESC]
I know this is and old thread, but this might be useful to someone. I've found that the map (map it to anything else you want!)
noremap <leader>ss diwi<SPACE><C-R>"<SPACE><ESC>B
works ok both for turning 'a+b' into 'a + b' (when used over the '+' char) and for turning 'a==b' into 'a == b' (when used over either the first or the second '=' sign).
I hope it's useful to someone.
Use case: I've just entered insert mode, and typed some text. Now I want to make it uppercase.
It can be done via gUmotion. However, I can't find the motion over the text entered in the recent input session. It's somewhat strange and the concept of such motion is buggy (where to move if you've deleted text, for example?), but it may solve my problem.
Or, are there other ways of making uppercase the text you've recently inputted?
The motion you're looking for is:
`[
(backtick, open-square-bracket). To do a simple motion, you'd use:
gU`[
However, you'll find that the last character probably won't be included due to the way the motion works (I could be wrong). A simple solution would then be to do:
v`[U
Which is to say "go to visual mode, select from the current position to the start of the last changed text, make it upper case". For more information, see:
:help '[
:help mark-motions
Note the difference in :help mark-motions between a backtick and a single-quote.
Type the word in the lower case in the
vim.
Then press Esc key.
Then move the cursor to starting
character of the typed words.
Then press the ~ key.
It will replace the lower case to
upper case.
If the input is upper case it will
replace the lower case.
You can also use the "inner word" motion along with gU
After typing a word press <Esc> and type gUiw. This should work without having to switch to visual mode.
I simply select the text in visual mode and use ~ to change the case, U to uppercase or u to lowercase the selected text.
Edit: See comments below.
Sublime Text Vintage Mode
For those using Vintage Mode in Sublime Text.
Uppercase: g U
Lowercase: g u
Swap case: g ~
More info here.
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:$