mirror of
https://github.com/chenasraf/dotfiles.git
synced 2026-05-18 01:29:06 +00:00
feat: nvim toggle quotes
This commit is contained in:
2
.config/.yamllint.yml
Normal file
2
.config/.yamllint.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
rules:
|
||||
key-ordering: disable
|
||||
60
.config/nvim/lua/casraf/quotes.lua
Normal file
60
.config/nvim/lua/casraf/quotes.lua
Normal file
@@ -0,0 +1,60 @@
|
||||
function ToggleQuotes()
|
||||
local line = vim.api.nvim_win_get_cursor(0)[1]
|
||||
local col = vim.api.nvim_win_get_cursor(0)[2]
|
||||
local line_text = vim.api.nvim_buf_get_lines(0, line - 1, line, false)[1]
|
||||
local quotes = { ["'"] = '"', ['"'] = '`', ['`'] = "'" }
|
||||
|
||||
-- Find the nearest quote char to the left of the cursor position
|
||||
local left_quote_char = nil
|
||||
local i = col - 1
|
||||
while i >= 1 do
|
||||
if quotes[line_text:sub(i, i)] then
|
||||
left_quote_char = line_text:sub(i, i)
|
||||
break
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
|
||||
-- Find the nearest quote char to the right of the cursor position
|
||||
local right_quote_char = nil
|
||||
i = col
|
||||
if left_quote_char then
|
||||
while i <= #line_text do
|
||||
if line_text:sub(i, i) == left_quote_char then
|
||||
right_quote_char = line_text:sub(i, i)
|
||||
break
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
if left_quote_char == right_quote_char then
|
||||
-- Determine the quote type to use for replacement
|
||||
local quote_type = left_quote_char or right_quote_char
|
||||
local next_quote_type = quotes[quote_type]
|
||||
|
||||
-- Replace the quotes in the line text
|
||||
local new_line_text = line_text
|
||||
local replaced_left = false
|
||||
local replaced_right = false
|
||||
for i = col - 1, 1, -1 do
|
||||
local c = line_text:sub(i, i)
|
||||
if not replaced_left and c == quote_type then
|
||||
new_line_text = new_line_text:sub(1, i - 1) .. next_quote_type .. new_line_text:sub(i + 1)
|
||||
replaced_left = true
|
||||
end
|
||||
end
|
||||
for i = col, #line_text do
|
||||
local c = line_text:sub(i, i)
|
||||
if not replaced_right and c == quote_type then
|
||||
new_line_text = new_line_text:sub(1, i - 1) .. next_quote_type .. new_line_text:sub(i + 1)
|
||||
replaced_right = true
|
||||
end
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(0, line - 1, line, false, { new_line_text })
|
||||
else
|
||||
print("No quotes found on line " .. line .. ":" .. col)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
vim.cmd('command! ToggleQuotes lua ToggleQuotes()')
|
||||
@@ -17,8 +17,8 @@ vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", { desc = "Move line up" })
|
||||
vim.keymap.set("n", "J", "mzJ`z", { desc = "Join line" })
|
||||
|
||||
-- insert newlines without insert mode
|
||||
vim.keymap.set("n", "<S-CR>", "m`o<Esc>k``", { desc = "Insert newline below" })
|
||||
vim.keymap.set("n", "<CR>", "m`O<Esc>j``", { desc = "Insert newline above" })
|
||||
vim.keymap.set("n", "<CR>", "m`o<Esc>k``", { desc = "Insert newline below" })
|
||||
vim.keymap.set("n", "<M-Enter>", "m`O<Esc>j``", { desc = "Insert newline above" })
|
||||
|
||||
-- redo
|
||||
vim.keymap.set("n", "U", "<C-r>", { desc = "Redo" })
|
||||
@@ -53,6 +53,81 @@ vim.keymap.set("v", "<leader>{", [[:s/\%V\(.*\)\%V/{\1}/g<Left><Left><CR>]], { d
|
||||
vim.keymap.set("v", "<leader><", [[:s/\%V\(.*\)\%V/<\1>/g<Left><Left><CR>]],
|
||||
{ desc = "Surround selection with angle brackets" })
|
||||
|
||||
vim.keymap.set("n", "<leader>'", ":ToggleQuotes", { desc = "Toggle nearest quote" })
|
||||
|
||||
-- replace nearest quote with double quote
|
||||
-- vim.keymap.set("n", "<leader>'", function()
|
||||
-- -- save original position
|
||||
-- local orig_pos = vim.fn.getpos(".")
|
||||
-- local orig_line = orig_pos[2]
|
||||
--
|
||||
-- -- look for nearest quote before, iterate lines until match found
|
||||
-- local line = vim.fn.line(".")
|
||||
-- local col = vim.fn.col(".")
|
||||
-- local found = false
|
||||
-- local quote_type = ""
|
||||
-- -- print("line", line, "col", col)
|
||||
-- while line > 0 do
|
||||
-- local line_text = vim.fn.getline(line)
|
||||
-- local quote_col = string.match(line_text, [['"`]])
|
||||
-- print("quote_col", quote_col, "line", line, "col", col)
|
||||
-- -- print("line_text", line_text)
|
||||
-- -- print("line", line, "col", col)
|
||||
-- if quote_col ~= -1 then
|
||||
-- vim.fn.setpos(".", { 0, line, quote_col + 1, 0 })
|
||||
-- vim.cmd([["+normal ci"]])
|
||||
-- found = true
|
||||
-- break
|
||||
-- end
|
||||
-- line = line - 1
|
||||
-- end
|
||||
-- -- replace ' with ", " with `, and ` with ' on the found line
|
||||
-- if found then
|
||||
-- local line_text = vim.fn.getline(".")
|
||||
-- -- save the quote type
|
||||
-- quote_type = string.find(line_text, [['"`]])
|
||||
-- local new_text = string.gsub(line_text, [['"`]], {
|
||||
-- ["'"] = [["]],
|
||||
-- ['"'] = [[`]],
|
||||
-- ["`"] = [[']],
|
||||
-- })
|
||||
-- -- print("new_text", new_text)
|
||||
-- vim.fn.setline(".", new_text)
|
||||
-- end
|
||||
--
|
||||
-- -- look for nearest quote of same type after, iterate lines until match found
|
||||
-- -- local end_line = vim.fn.line(".")
|
||||
-- -- local end_col = vim.fn.col(".")
|
||||
-- -- local end_found = false
|
||||
-- -- while end_line < vim.fn.line("$") do
|
||||
-- -- local line_text = vim.fn.getline(end_line)
|
||||
-- -- local quote_col = vim.fn.match(line_text, quote_type, end_col)
|
||||
-- -- if quote_col ~= -1 then
|
||||
-- -- vim.fn.setpos(".", { 0, end_line, quote_col + 1, 0 })
|
||||
-- -- vim.cmd([["+normal ci"]])
|
||||
-- -- end_found = true
|
||||
-- -- break
|
||||
-- -- end
|
||||
-- -- end_line = end_line + 1
|
||||
-- -- end
|
||||
-- -- replace ' with ", " with `, and ` with ' on the found line
|
||||
-- -- if end_found and end_line ~= orig_line then
|
||||
-- -- local line_text = vim.fn.getline(".")
|
||||
-- -- local new_text = string.gsub(line_text, [['"`]], {
|
||||
-- -- ["'"] = [["]],
|
||||
-- -- ['"'] = [[`]],
|
||||
-- -- ["`"] = [[']],
|
||||
-- -- })
|
||||
-- -- vim.fn.setline(".", new_text)
|
||||
-- -- end
|
||||
--
|
||||
-- -- return to original position
|
||||
-- vim.fn.setpos(".", orig_pos)
|
||||
-- -- print("switched quote types")
|
||||
-- end,
|
||||
-- { desc = "Switch between quote types" })
|
||||
|
||||
|
||||
-- comment line
|
||||
vim.keymap.set("n", "<leader>/", ":CommentToggle<CR>", { desc = "Comment line" })
|
||||
|
||||
@@ -86,11 +161,11 @@ vim.keymap.set("n", "<leader>k", "<cmd>lnext<CR>zz", { desc = "Next location lis
|
||||
vim.keymap.set("n", "<leader>j", "<cmd>lprev<CR>zz", { desc = "Previous location list" })
|
||||
|
||||
-- search and replace current word
|
||||
vim.keymap.set( "n", "<leader>s", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]],
|
||||
vim.keymap.set("n", "<leader>s", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]],
|
||||
{ desc = "Search and replace current word" })
|
||||
-- search and replace current selection
|
||||
vim.keymap.set("v", "<leader>s", [["hy:%s/<C-r>h/<C-r>h/gI<Left><Left><Left>]],
|
||||
{ desc = "Search and replace current selection" })
|
||||
{ desc = "Search and replace current selection" })
|
||||
|
||||
vim.keymap.set("n", "<leader>x", "<cmd>!chmod +x %<CR>", { silent = true, desc = "Make file executable" })
|
||||
vim.keymap.set("n", "<leader>X", "<cmd>!chmod -x %<CR>", { silent = true, desc = "Make file not executable" })
|
||||
|
||||
26
aliases.sh
26
aliases.sh
@@ -1,5 +1,7 @@
|
||||
source $HOME/.dotfiles/colors.sh
|
||||
source $HOME/.dotfiles/functions.sh
|
||||
#!/usr/bin/env zsh
|
||||
|
||||
source "$HOME/.dotfiles/colors.sh"
|
||||
source "$HOME/.dotfiles/functions.sh"
|
||||
|
||||
# Aliases
|
||||
alias ".."="cd .."
|
||||
@@ -13,7 +15,7 @@ alias l="ls -A"
|
||||
alias v="nvim ."
|
||||
alias vi="nvim"
|
||||
alias vim="nvim"
|
||||
alias serve="open http://localhost:${PORT:-3001} & python3 -m http.server ${PORT:-3001}"
|
||||
alias serve="open http://localhost:\${PORT:-3001} & python3 -m http.server \${PORT:-3001}"
|
||||
|
||||
# output pipes
|
||||
alias -g H="| head"
|
||||
@@ -30,31 +32,31 @@ alias -g P="2>&1| pygmentize -l pytb"
|
||||
alias arm="arch -arm64"
|
||||
alias x86="arch -x86_64"
|
||||
# [d]ev gi_gen
|
||||
alias dgi_gen="$GOBIN/gi_gen"
|
||||
alias dgi_gen="\$GOBIN/gi_gen"
|
||||
# [g]lobal gi_gen
|
||||
alias ggi_gen="$DOTBIN/gi_gen"
|
||||
alias ggi_gen="\$DOTBIN/gi_gen"
|
||||
# go [i]nstall & run gi_gen
|
||||
alias igi_gen="go install && dgi_gen"
|
||||
|
||||
alias gdiff="git diff"
|
||||
alias gpa="ga . && gc && gp"
|
||||
|
||||
# geneal
|
||||
# general
|
||||
# from https://jarv.is/notes/cool-bash-tricks-for-your-terminal-dotfiles/
|
||||
alias ip4="curl -4 simpip.com --max-time 2 --proto-default https --silent | prepend 'ipv4: '"
|
||||
alias ip6="curl -6 simpip.com --max-time 2 --proto-default https --silent | prepend 'ipv6: '"
|
||||
alias iplocal="ipconfig getifaddr en0 | prepend 'iplocal: '"
|
||||
alias ip="iplocal; ip4; ip6"
|
||||
alias afk="/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend"
|
||||
alias pkgupdate="brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo g em update --system; sudo gem update; sudo gem cleanup; sudo softwareupdate -i -a;"
|
||||
# alias pkgupdate="brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo g em update --system; sudo gem update; sudo gem cleanup; sudo softwareupdate -i -a;"
|
||||
alias pubkey="more ~/.ssh/id_rsa.pub | pbcopy | echo '=> Public key copied to pasteboard.'"
|
||||
alias gundo="git reset --soft HEAD~1"
|
||||
alias unq="sudo xattr -rd com.apple.quarantine"
|
||||
alias scriptls="cat \$(find-up package.json) | jq '.scripts'"
|
||||
alias sync-config="rsync -vtr $DOTFILES/.config/ $HOME/.config/"
|
||||
alias sync-config="rsync -vtr \$DOTFILES/.config/ \$HOME/.config/"
|
||||
alias sf="search-file"
|
||||
alias fnu="find-up"
|
||||
alias ascii-text=". $DOTFILES/scripts/ascii_font/ascii_font.sh"
|
||||
alias ascii-text=". \$DOTFILES/scripts/ascii_font/ascii_font.sh"
|
||||
|
||||
# home
|
||||
alias h="home"
|
||||
@@ -93,10 +95,10 @@ alias trn="tmux rename-session -t"
|
||||
alias trm="tmux kill-session -t"
|
||||
|
||||
# tmux - workspaces
|
||||
alias tn-general="tn-custom $HOME/Dev -s general"
|
||||
alias tn-df="tn-custom -d $DOTFILES -s dotfiles ."
|
||||
alias tn-general="tn-custom \$HOME/Dev -s general"
|
||||
alias tn-df="tn-custom -d \$DOTFILES -s dotfiles ."
|
||||
alias tn-simple-scaffold="tn-prj simple-scaffold"
|
||||
alias tn-acroasis="tn-custom -d $HOME/Dev/acroasis -s acroasis front server shared landing"
|
||||
alias tn-acroasis="tn-custom -d \$HOME/Dev/acroasis -s acroasis front server shared landing"
|
||||
|
||||
if is_linux; then
|
||||
alias md5="md5sum"
|
||||
|
||||
@@ -33,9 +33,12 @@ export PATH="$HOME/bin:/usr/local/bin:$PATH"
|
||||
export PATH="$PATH:/$HOME/.local/bin"
|
||||
|
||||
# guile (?)
|
||||
export GUILE_LOAD_PATH="/usr/local/share/guile/site/3.0"
|
||||
export GUILE_LOAD_COMPILED_PATH="/usr/local/lib/guile/3.0/site-ccache"
|
||||
export GUILE_SYSTEM_EXTENSIONS_PATH="/usr/local/lib/guile/3.0/extensions"
|
||||
# export GUILE_LOAD_PATH="/usr/local/share/guile/site/3.0"
|
||||
# export GUILE_LOAD_COMPILED_PATH="/usr/local/lib/guile/3.0/site-ccache"
|
||||
# export GUILE_SYSTEM_EXTENSIONS_PATH="/usr/local/lib/guile/3.0/extensions"
|
||||
|
||||
# yamllint
|
||||
export YAMLLINT_CONFIG_FILE="$HOME/.config/.yamllint.yml"
|
||||
|
||||
# NVM
|
||||
export NVM_DIR="$HOME/.nvm"
|
||||
|
||||
@@ -101,6 +101,7 @@ zplug "mfaerevaag/wd", as:command, use:"wd.sh", hook-load:"wd() { . $ZPLUG_REPOS
|
||||
zplug romkatv/powerlevel10k, as:theme, depth:1
|
||||
zplug zsh-users/zsh-autosuggestions
|
||||
|
||||
# TODO: use zplug?
|
||||
source "$HOME/.dotfiles/plugins/git.plugin.zsh"
|
||||
|
||||
zplug load
|
||||
|
||||
Reference in New Issue
Block a user