What Vim command(s) can be used to quote/unquote words?
VimQuotingVim Problem Overview
How can I quickly quote/unquote words and change quoting (e.g. from '
to "
) in Vim? I know about the surround.vim plugin, but I would like to use just Vim.
Vim Solutions
Solution 1 - Vim
surround.vim is going to be your easiest answer. If you are truly set against using it, here are some examples for what you can do. Not necessarily the most efficient, but that's why surround.vim was written.
- Quote a word, using single quotes
ciw'Ctrl+r"'
ciw
- Delete the word the cursor is on, and end up in insert mode.'
- add the first quote.Ctrl+r"
- Insert the contents of the"
register, aka the last yank/delete.'
- add the closing quote.
- Unquote a word that's enclosed in single quotes
di'hPl2x
di'
- Delete the word enclosed by single quotes.hP
- Move the cursor left one place (on top of the opening quote) and put the just deleted text before the quote.l
- Move the cursor right one place (on top of the opening quote).2x
- Delete the two quotes.
- Change single quotes to double quotes
va':s/\%V'\%V/"/g
va'
- Visually select the quoted word and the quotes.:s/
- Start a replacement.\%V'\%V
- Only match single quotes that are within the visually selected region./"/g
- Replace them all with double quotes.
Solution 2 - Vim
> Quote a word, using single quotes
>
> ciw'Ctrl+r"'
It was easier for me to do it this way
ciw '' Esc P
Solution 3 - Vim
Here's some mapping that could help:
:nnoremap <Leader>q" ciw""<Esc>P
:nnoremap <Leader>q' ciw''<Esc>P
:nnoremap <Leader>qd daW"=substitute(@@,"'\\\|\"","","g")<CR>P
If you haven't changed the mapleader variable, then activate the mapping with \q"
\q'
or \qd
. They add double quote around the word under the cursor, single quote around the word under the cursor, delete any quotes around the word under the cursor respectively.
Solution 4 - Vim
The macro ways !
-
press q and q for recording into q register (we use "q" as shortcut to remember "quotes").
-
press shift + b move cursor to front of current word
-
press i type ' (a single quotes)
-
press esc then press e to move to end of word
-
press a then press ' again to surround the word with quotes.
-
press esc to get into normal mode.
-
finally press q to record it into
q
register.
How to use
- Move cursor to desired word.
- Press @q to surround a word with quotes.
- Press @@ if you want repeat it into another word.
You can alter step 4
with anything you like {a line, a word until found some character, etc}.
Make recorded macro persistent
- open .vimrc
- go to end of file
- change to insert mode. type this to make it persistent:
let @q='ctrl + r ctrl + r q'
-
save and quit
-
open your files, go to some words
-
now press
@q
if you do it correctly, magic things should appear in your words.
You can apply this to other macros you loved.
Solution 5 - Vim
In addition to the other commands, this will enclose all words in a line in double quotes (as per your comment)
:s/\(\S\+\)/"\1"/
or if you want to reduce the number of backslashes, you can put a \v
(very-magic) modifier at the start of the pattern
:s/\v(\S+)/"\1"/
Solution 6 - Vim
If you use the vim plugin https://github.com/tpope/vim-surround (or use VSCode Vim plugin, which comes with vim-surround pre-installed), its pretty convinient!
add
ysiw' // surround in word `'`
drop
ds' // drop surround `'`
change
cs'" // change surround from `'` to `"`
It even works for html tags!
cst<em> // change surround from current tag to `<em>`
check out the readme on github for better examples
Solution 7 - Vim
To wrap in single quotes (for example) ciw'<C-r>"'<esc>
works, but repeat won't work. Try:
ciw'<C-r><C-o>"'<esc>
This puts the contents of the default register "literally". Now you can press .
on any word to wrap it in quotes. To learn more see :h[elp] i_ctrl-r
and more about text objects at :h text-objects
Source: http://vimcasts.org/episodes/pasting-from-insert-mode/
Solution 8 - Vim
For users of VSCodeVim, Neovim and Macvim, you can do
vwS"
- You can replace
"
with whatever you would like to wrap by. - You can replace
w
with any other selection operator
Solution 9 - Vim
I don't know any builtin vim command for this, but using r"f'r"
to change from ' to " and r'f"r'
to change from " to ' works if you stand on the first ' or ". The command r'
replaces whatever character is under your cursor with ', and f"
moves you forward to the next ".
Solution 10 - Vim
Adding Quotes
I started using this quick and dirty function in my .vimrc
:
vnoremap q <esc>:call QuickWrap("'")<cr>
vnoremap Q <esc>:call QuickWrap('"')<cr>
function! QuickWrap(wrapper)
let l:w = a:wrapper
let l:inside_or_around = (&selection == 'exclusive') ? ('i') : ('a')
normal `>
execute "normal " . inside_or_around . escape(w, '\')
normal `<
execute "normal i" . escape(w, '\')
normal `<
endfunction
So now, I visually select whatever I want (typically via viw
- visually select inside word) in quotes and press Q for double quotes, or press q for single quotes.
Removing Quotes
vnoremap s <esc>:call StripWrap()<cr>
function! StripWrap()
normal `>x`<x
endfunction
I use vim-textobj-quotes so that vim treats quotes as a text objects. This means I can do vaq
(visually select around quotes. This finds the nearest quotes and visually selects them. (This is optional, you can just do something like f"vww
). Then I press s
to strip the quotes from the selection.
Changing Quotes
KISS. I remove quotes then add quotes. For example, to replace single quotes with double quotes, I would perform the steps:
- remove single quotes:
vaqs
, 2. add new quotes:vwQ
.
- http://vim.wikia.com/wiki/Wrap_a_visual_selection_in_an_HTML_tag code from here was modified and used as part of my answer
- https://github.com/beloglazov/vim-textobj-quotes vim-text-obj-quotes
Solution 11 - Vim
Here are some simple mappings that can be used to quote and unquote a word:
" 'quote' a word
nnoremap qw :silent! normal mpea'<Esc>bi'<Esc>`pl
" double "quote" a word
nnoremap qd :silent! normal mpea"<Esc>bi"<Esc>`pl
" remove quotes from a word
nnoremap wq :silent! normal mpeld bhd `ph<CR>
Solution 12 - Vim
VIM for vscode does it awsomely. It's based one vim-surround if you don't use vscode.
> Some examples: > > "test" with cursor inside quotes type cs"' to end up with 'test' > > "test" with cursor inside quotes type ds" to end up with test > > "test" with cursor inside quotes type cs"t and enter 123> to end up > with <123>test > > test with cursor on word test type ysaw) to end up with (test)
Solution 13 - Vim
wrap all words with quotes:
s/\(\w\+\)/"\1"/g
before:
aaa,bbb,ccc
after:
"aaa","bbb","ccc"
Solution 14 - Vim
I am using the vim-surround
command, almost vim in nature.
> Surround.vim is all about "surroundings": parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.
Plugin 'tpope/vim-surround'
for example, To remove the delimiters entirely, press ds"
.
More details are here:
https://github.com/tpope/vim-surround
Solution 15 - Vim
I'm using nnoremap in my .vimrc
To single quote a word:
nnoremap sq :silent! normal mpea'<Esc>bi'<Esc>`pl
To remove quotes (works on double quotes as well):
nnoremap qs :silent! normal mpeld bhd `ph<CR>
Rule to remember: 'sq' = single quote.
Solution 16 - Vim
Visual mode map example to add single quotes around a selected block of text:
:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
Solution 17 - Vim
how about this?
:%s/\'/"/g
Solution 18 - Vim
I wrote a script that does this:
function! WrapSelect (front)
"puts characters around the selected text.
let l:front = a:front
if (a:front == '[')
let l:back = ']'
elseif (a:front == '(')
let l:back = ')'
elseif (a:front == '{')
let l:back = '}'
elseif (a:front == '<')
let l:back = '>'
elseif (a:front =~ " ")
let l:split = split(a:front)
let l:back = l:split[1]
let l:front = l:split[0]
else
let l:back = a:front
endif
"execute: concat all these strings. '.' means "concat without spaces"
"norm means "run in normal mode and also be able to use \<C-x> characters"
"gv means "get the previous visual selection back up"
"c means "cut visual selection and go to insert mode"
"\<C-R> means "insert the contents of a register. in this case, the
"default register"
execute 'norm! gvc' . l:front. "\<C-R>\"" . l:back
endfunction
vnoremap <C-l> :<C-u>call WrapSelect(input('Wrapping? Give both (space separated) or just the first one: '))<cr>
To use, just highlight something, hit control l, and then type a character. If it's one of the characters the function knows about, it'll provide the correct terminating character. If it's not, it'll use the same character to insert on both sides.
Surround.vim can do more than just this, but this was sufficient for my needs.