6

I'd like to use an external Perl or Python script to change a selection of text in Vim into title case. As a user of these scripts, you can select the small words which are not capitalized.

However, I want to apply the filter only on a part of a line, not the complete line. Does anyone know how to do this?

Example line in LaTeX source code:

\item the title case in latex and ...

Should become

\item The Title Case in Latex and ...

The following command does not work:

:{visual}!{filter}
doubleDown
  • 8,048
  • 1
  • 32
  • 48
Hotschke
  • 9,402
  • 6
  • 46
  • 53
  • +1 Nice first question. One improvement you could make is instead of writing the link text, you could embed the link with the link tool (glob icon). For example, if you edit your post, you could select "wikipedia" in the second sentence, click the link icon above the edit box, and paste the link address in there. – Codie CodeMonkey Dec 07 '11 at 08:22
  • As a new user I was limited to create only two links. So thanks for your upvote and editing of the question. – Hotschke Dec 07 '11 at 09:05
  • There is also a question on [tex.sx](http://tex.stackexchange.com/questions/4809/how-can-i-force-text-to-be-displayed-in-title-case) The drawback is that the proposed tex-based solution with the package [stringstrings](http://www.ctan.org/tex-archive/macros/latex/contrib/stringstrings) makes the compilation of the tex file extremely slow. – Hotschke Dec 07 '11 at 09:18
  • There is now a new latex package called [`titlecaps`](http://ctan.org/pkg/titlecaps). – Hotschke Jul 01 '13 at 18:28

3 Answers3

3

All ex commands work linewise (due to vi/ex history). Therefore, it is not possible to use a filter only for selected words, only linewise.

This is documented in the helpfile of vim (version 8.0.x) under :h 10.3:

Note:
When using Visual mode to select part of a line, or using CTRL-V to select a block of text, the colon commands will still apply to whole lines. This might change in a future version of Vim.

To jump directly to this help section try to use :helpg colon\ commands.*apply.

For reference: A list of ex commands can be shown via :h ex-cmd-index.

related sx.questions are:

Hotschke
  • 9,402
  • 6
  • 46
  • 53
1

Finally, I've found a plugin for what I had in mind:

vis - Extended Visual Mode Commands, Substitutes, and Searches vimscript #1159 (github mirror)

:'<,'>B !titlecase

Hotschke
  • 9,402
  • 6
  • 46
  • 53
1

This example is partially working, but does not capitalize the last word in the visually selected text. Idea was to reduce work-load by staying in Vim. Get this to work on the last word in the visual selection and you are there. :) Per updated specs, pass "\\|" delimeted list of small words, with first letter capitalized.

" Visually select some text
":call title_case_selection:()
" and probably want to map it to some abbreviation
"

function title_case_selection:( list_of_words_bar_delimited )
    let g:start_column=virtcol("'<") - 1
    let g:end_column=virtcol("'>") + 1
    let g:substitution_command=':s/\%>'.g:start_column.'v\<\(\w\)\(\w*\)\>\%<'.g:end_column.'v/\u\1\L\2/g'
    call feedkeys ( g:substitution_command )
    call feedkeys ("\<cr>", 't')
    let g:substitution_command=':s/\%>'.g:start_column.'v\<\('.a:list_of_words_bar_delimited.'\)\>\%<'.g:end_column.'v/\L\1/g'
    call feedkeys ( g:substitution_command )
    call feedkeys ("\<cr>", 't')
endfunction

"abba zabba is a very yummy candy! <- Visually select this line

:call title_case_selection:("Is\\|A")

kikuchiyo
  • 3,391
  • 3
  • 23
  • 29
  • Is it possible to adjust your vim function to allow users to select small words which are not capitalized. Sorry, that my original question wasn't clear about this. I still hope that vim offers the asked feature in general. – Hotschke Dec 06 '11 at 19:55
  • Anyhow, there are already vim scripts. [titlecase.vim](http://www.vim.org/scripts/script.php?script_id=439) just performs a full capitalization. Thus the script name is misleading and does not solve it. [cream-capitalization](http://www.vim.org/scripts/script.php?script_id=242) actually does what I want. A smaller issue of this plugin is bugging me: doing a visual selection with v$ cream-capitalization moves the selection into a new line. This is annoying. You have to move the cursor one step left. However, I am interested whether my question could be solved by using an external command. – Hotschke Dec 07 '11 at 09:12
  • If you want, you can map an abbreviation to v$h, like `vmap $ $h` so you don't have to physically press h after $, when in Visual mode. Now v$ will do what you want. Sounds like this and cream-capitalization should be all you need. Good luck! – kikuchiyo Dec 07 '11 at 17:30