When I select text with the mouse in Neovim (using "mouse=a"), it selects the it on regular visual mode. How can I make it select the text on select mode instead?
Assistant
In Neovim, when using the mouse in visual mode, the typical behavior is to enter regular visual mode (v) instead of the selection mode. However, you can configure Neovim to use the selection mode, which allows for a more straightforward text selection experience that mimics typical GUI text editors.
To achieve this, follow these steps:
Set up mouse support in your Neovim configuration: You already have in your configuration, but ensure this is properly set in your or file:
User
A different question: how do you do to change to a different mode from command mode?
Assistant
In Neovim (and Vim), you can switch to different modes from Normal mode (the mode you enter after starting Neovim) using specific keys:
To Enter Insert Mode:
Press i to enter Insert mode before the cursor.
User
I mean, is there an scriptable command, such as :visual to change modes from the vim command line?
Assistant
In Neovim and Vim, you can't directly switch to certain modes like Insert or Visual from command mode using a command such as :visual or :insert. However, there are some workarounds and commands that can help you achieve similar results through scripting or by creating custom commands.
1. Using Mappings
You can create mappings that allow you to switch modes in a way that feels more natural, even if there isn't a built-in command for it. For example, to create a command to enter Visual mode, you can add the following to your configuration file:
For init.vim:
command! VisualMode normal v
For init.lua:
User
The thing is, when I try to enter visual block mode, Neovim throws "e716 key not present in dictionary visual block". Is there a way around, either using the command mode or an alternative way of entering visual block mode if there is one?
Assistant
The error you are encountering, "E716: Key not present in dictionary: visual block," indicates that you are trying to use an incorrect command to enter Visual Block mode from command mode directly. Unlike some other modes, Visual Block mode cannot be entered using a standard command like :visual block.
However, you can use the :normal command to simulate entering Visual Block mode. Here’s how you can set it up:
1. Using Normal Mode Command
You can create a custom command in your configuration that allows you to enter Visual Block mode.
User
I tried ":normal! " on the neovim command mode and it doesn't throw any error, but it neither brings me to the visual selection mode. It just changes to normal mode
Assistant
I apologize for the confusion. When you use :normal! <C-v>, it doesn't actually switch the mode as intended because command-line mode (when typing :) operates in a different context than the normal mode, and it doesn't recognize terminal-specific keyboard commands like <C-v> directly when called from command-line mode.
Alternative Solutions
As of now, there isn’t a built-in command you can directly use to enter Visual Block mode from command mode; however, you can use the following methods to achieve your goal:
1. Map a Command to Switch to Visual Block Mode
Instead of using a command-line command, you can create mappings that allow us to switch to Visual Block mode without the command line:
User
I can't just press C-v because it throws the aforementioned E716 error. The same goes with any mapping with C-v
Assistant
The issue you're encountering with Ctrl + v throwing the "E716" error indicates that Visual Block mode isn't being initialized correctly in certain contexts, which may happen if there are some conditions affecting your configuration or terminal.
User
command! VisualBlockMode execute 'normal! <C-v>' just brings me to normal mode, but doesn't recognize C-v
Assistant
I apologize for the confusion earlier. It seems that entering Visual Block mode using the <C-v> binding from command mode isn't behaving as expected in your setup.
Directly Entering Visual Block Mode:
To enter Visual Block mode properly, you can use the terminal’s native capabilities to capture the sequence correctly. However, since it seems pressing directly isn't working for your configuration, let's explore a few alternatives that can help you:
User
No matter what I do, every time Neovim detects C-v directly or in any kind of mapping, it throus the E716 error
Assistant
User
Is there a built-in alternative keybind to enter visal block mode aside from C-v?
Assistant
In Neovim (and Vim), the primary and built-in key binding for entering Visual Block mode is Ctrl + v. Unfortunately, there are no alternative built-in key bindings specifically designated for entering Visual Block mode.
However, you can use other methods or create your own custom mappings to achieve similar functionality. Here are some options:
1. Custom Key Mapping
If Ctrl + v is not working for you due to key binding issues or terminal configurations, you can create a custom mapping to facilitate entering Visual Block mode.
User
I found the root of the error: I had a dictionary in which I associated the modes with their vim built-in codes (n for normal, v for visual and so on). I used this dictionary to print the current mode to the statusbar. It seems that I must refer to the vim codes with "double quotes", not with 'single ones', otherwise Neovim won't interpret "^V" as Visual block mode
Assistant
It's great to hear that you identified the root of the issue! In Vim and Neovim, when working with special key sequences (such as control characters) in a mapping or command, it’s essential to use the correct syntax for the quotes.
Understanding Quote Types
In Vim:
Double Quotes ("): These allow the Vim parser to interpret special characters like ^V as control sequences, which represent the actual keys.
Single Quotes ('): These are treated as literal strings, meaning Vim won't interpret special characters or escape sequences correctly.
User
Another thing: is there a way on Vim or Neovim to run a command every time the completion menu closes?
Assistant
Yes, in Neovim (and Vim), you can execute a command whenever the completion menu closes by utilizing autocommands (also known as autocmds). Specifically, you can listen for the CompleteDone event, which is triggered whenever the completion menu is closed.
Here's how you can set that up:
1. Using Autocommands
You can add an autocommand in your init.vim or file to run a specific command when the completion menu closes.
User
So, what's the simplest way of using a vimscript function to set cmdheight=0 every time the completion menu closes? (by using CompleteDone, I guess)
Assistant
To set cmdheight=0 every time the completion menu closes in Neovim (or Vim), you can define a simple Vimscript function and then use an autocommand to call that function when the event is triggered.
User
Is there an equivalent built-in function to CompleteDone, but for the opposite thing, i.e. one that is called when you enter the completion menu?
Assistant
In Vim and Neovim, while the CompleteDone event is triggered when the completion menu closes, there is no direct built-in event that explicitly triggers when the completion menu opens. However, a common convention is to use the CompleteChanged event, which occurs when the completion menu is opened or when new entries are added to it.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Switch to Select Mode when using the mouse: You can map mouse events to enter Selection Mode. You'll want to add the following to your configuration file:
Using Selection Mode: The above mapping allows you to use the right mouse button (<RightMouse>) to select text in Selection Mode instead of regular visual mode.
Reload configuration: Finally, make sure to reload your Neovim configuration or restart Neovim for the changes to take effect.
After this, you should be able to use the mouse to select text in Selection Mode instead of Visual Mode in Neovim. Adjust the mappings or further customize as needed for your personal workflow!
Press I to enter Insert mode at the beginning of the line.
Press a to enter Insert mode after the cursor.
Press A to enter Insert mode at the end of the line.
Press o to open a new line below the current line and enter Insert mode.
Press O to open a new line above the current line and enter Insert mode.
To Enter Visual Mode:
Press v to enter Visual mode (character-wise selection).
Press V to enter Visual Line mode (line-wise selection).
Press Ctrl+v (or Ctrl+q for some configurations) to enter Visual Block mode (block-wise selection).
To Enter Command-Line Mode:
Press : to enter Command-Line mode where you can enter commands.
Press / to enter Search mode to search for text.
Press ? to enter Reverse Search mode.
To Enter Select Mode:
In Normal mode, you can enter Select mode (similar to Visual mode but behaves differently) by pressing g followed by v.
Switching Between Modes
You can always return to Normal mode from any other mode by pressing Esc.
If you are in Command-Line mode (after typing :), you can return to Normal mode by pressing Esc, or by executing the command with Enter.
In Visual modes (normal visual, visual line, and visual block), pressing Esc will take you back to Normal mode.
Example of Changing Modes
Start Neovim and type some text in Normal mode.
To edit existing text, press i to enter Insert mode.
After editing, press Esc to exit back to Normal mode.
To select some text, press v to enter Visual mode and select the text.
After selecting, press y to yank (copy) the selected text or any other desired action.
Press Esc again to return to Normal mode.
Feel free to explore these commands, and remember you can always refer to Neovim's documentation by typing :help followed by the command or feature you want to learn about, like :help insert for Insert mode.
vim.cmd [[
command! VisualMode normal v
]]
Now, if you run :VisualMode in the command line, it will switch you to Visual mode.
2. Using Function and Commands
You can also define a more complex command with functions if needed to switch to Insert or other modes:
Now, by executing :InsertMode, you will enter Insert mode.
Summary
While there isn't a built-in command like :visual to change modes directly, you can create custom commands to help achieve similar functionality. Just remember that typing :command_name will work as you expect, allowing you to enter the desired mode seamlessly.
If you prefer a particular mode frequently, creating custom commands, as shown, can improve your workflow significantly.
Now, if you execute :VisualBlockMode, it will switch you into Visual Block mode. The normal! command allows you to execute the <C-v> key sequence which is the key binding to enter Visual Block mode.
2. Using Custom Mapping
If you want a quick key press to enter Visual Block mode, consider creating a custom mapping in Normal mode:
In this case, <leader>b would take you to Visual Block mode. The <leader> key is a customizable prefix that can be set in your configuration.
Using the Built-In Shortcut
Alternatively, if you don't want to define custom commands or mappings, you can always enter Visual Block mode using the keyboard shortcut while in Normal mode:
Press Ctrl + v to enter Visual Block mode directly.
Summary
While you cannot enter Visual Block mode via a direct command like :visual block, using the :normal command to simulate <C-v> or creating a mapping can provide a workaround. This should enhance your workflow and allow you to use Visual Block mode more conveniently.
By pressing <leader>v, you would enter Visual Block mode directly from Normal mode.
2. Use Key Combination
The simplest way is to just press Ctrl + v while in Normal mode. This is the default keystroke for entering Visual Block mode.
Conclusion
Currently, the most efficient way to switch modes like entering Visual Block mode is to use direct key mappings or use the keyboard shortcuts while in Normal mode. Adapting mappings using the <leader> key can enhance your experience significantly, making it easier to switch modes as you work without needing to enter command-line mode for certain functions.
If you'd like a quick method through command mode, unfortunately, there's no direct equivalent; your best option is to pick a keyboard shortcut that works for you and stick with it for efficiency.
Workaround to Enter Visual Block Mode
Given your situation, here's an alternative way to enter Visual Block mode:
Using Command Mode with :normal in a Different Context:
If you can't enter Visual Block mode directly because of the <C-v> mapping, then you can execute :normal in a more manual way. Since we need to ensure that you're actually in Normal mode before performing actions, we'll rely on the :execute method to avoid confusion:
Now you should be able to type :VisualBlockMode in command mode to enter Visual Block mode.
Ensure Correctly Configured Terminal
If you encounter issues with Ctrl + v, it might also be indicative of terminal input conflicts or configurations. Please ensure that:
Terminal Focus: Sometimes, if the terminal doesn't have the right focus or if it's shadows over Neovim, it may not interpret key combinations correctly.
Keyboard Input: Check if your terminal emulator is interfering with the Ctrl + v command. You might want to test if the command works in a different terminal application or with the Neovim GUI frontend (like Neovide, for instance).
Plugins and Configurations: Ensure that no plugins or settings in your configuration interfere with the visual selection modes.
Final Approach
Sometimes, the error and issues can stem from conflicts or misconfigurations in the environment being used (whether it's terminal, GUI, or specific adaptations). If the provided methods still do not resolve the situation, consider:
Re-checking your terminal emulator for compatibility.
Testing Neovim in a clean state without any plugins (you can launch it with nvim -u NONE).
Consulting the Neovim documentation using :help visual-mode for any changes or updates related to your version.
If you still run into issues, please consider any additional details about your terminal setup or configurations that might be affecting the behavior, and I’d be glad to assist further!
<C-v>
Option 1: Terminal-Specific Fixes
Verify the terminal settings. Sometimes terminal applications can capture Ctrl + v, causing it not to register in Neovim as expected. To ensure this, try the following:
Check if your terminal has an option for "Use Ctrl key as a modifier" or similar settings.
Confirm that you're running the latest version of your terminal emulator and Neovim.
Option 2: Key Remappings in Neovim
If you're facing challenges with Ctrl + v, you can set up an alternative key sequence for entering Visual Block mode. For example, you can use a simple key combination:
Instead of relying on <C-v>, you can make use of a different mechanism to ensure you can enter Visual Block mode without direct keyboard bindings.
For example:
You can define another command that enters Visual Block mode directly under certain keys:
command! VisualBlockMode normal! gv
Verify the Configuration
Make sure that your mappings are properly set and check whether any other plugin or configuration might be interfering with key bindings.
Summary
Using a leader key or a different command as shown above should help you bypass the limitations with <C-v>. If you continue to experience problems getting into Visual Block mode, please review terminal settings, possible conflicting plugins, or configurations that might be causing the issue.
If these options still do not work, please provide details about your terminal emulator so we can better understand the environment you're working in. Thank you for your patience!
Thank you for the clarification, and I apologize for the confusion. The issue with Ctrl + v throwing the "E716: Key not present in dictionary: visual block" error suggests that there's a deeper issue with how Neovim is handling that key combination for entering Visual Block mode.
Let's explore this from a fresh perspective to ensure you can enter Visual Block mode effectively. The following approaches offer different methods to resolve this:
Use the :normal Command with Escape Sequences
Since the <C-v> command doesn't seem to register correctly in your environment, we can use the full escape sequence approach to simulate entering Visual Block mode through the :execute command in a way that doesn't directly involve mappings or keyboard shortcuts.
Using the Command Mode:
You can try using
:execute "normal! g<C-v>"
Make sure you scan for any earlier configurations or plugins that might disrupt this behavior.
Temporarily Disable Key Mappings
Another trick to diagnose what’s happening is to disable all custom mappings:
Start Neovim with no configurations or plugins loaded to verify if the base functionality works:
nvim -u NONE
From this environment, attempt the following command:
:execute "normal! g<C-v>"
If this works, then the issue lies somewhere in your configuration files or plugins.
Create a Function Wrapper
If the above command works as expected, we should then encapsulate this behavior into a function:
function EnterVisualBlock()
vim.cmd("normal! g<C-v>")
end
vim.api.nvim_create_user_command('VB', EnterVisualBlock, {})
You can then invoke :VB in command mode to enter Visual Block.
Final Verification
If none of these approaches work, there might be terminal emulator issues or deeper environmental configurations affecting keybinding recognition.
Ensure your terminal does not have settings that prevent Ctrl + v from being captured. Try testing in a different terminal or on a different machine if possible.
Alternative Methods
Lastly, you can always enter Visual Block mode by using the mouse or other nearby keystrokes instead, which might avoid the confusion with keyboard combinations entirely.
I hope this more thorough examination of the underlying issue helps you to successfully use Visual Block mode without any further disruption! Let me know if you need additional assistance!
In this example, pressing <leader>vb would simulate the action of Ctrl + v, entering Visual Block mode. You can change <leader> to any key that you prefer.
2. Mouse Selection
If you have mouse support enabled (e.g., set mouse=a in your configuration), you can also use the mouse to select blocks of text. Click and drag to select regions, and Neovim will automatically enter the appropriate selection mode based on your mouse interactions.
3. Using Normal Mode Commands
You can enter Visual Block mode through a sequence of commands if direct key bindings don't work:
You can quickly enter Normal mode and use the g command followed by v to initiate block selection. However, this is essentially just another way to simulate the <C-v> action, rather than being a distinct alternative.
Summary
While there is no built-in alternative key binding for entering Visual Block mode aside from Ctrl + v, customizing your key mappings allows you to work around this limitation. Make sure to create a mapping that aligns with your workflow, and don't hesitate to experiment with different combinations until you find something comfortable for you.
If you continue to have issues or if Ctrl + v is not responsive due to the terminal, the custom mappings would be your best solution.
Example of Proper Usage
If you had a dictionary mapping the modes to their respective Vim codes but were using single quotes, such as:
When using functions or commands, always ensure that any special sequences are enclosed in double quotes if you intend for them to be interpreted as such. For instance, if you're printing the current mode in the status bar, you would want to use something like:
echo "Current mode: " . mode_dict[getmode()]
Using this syntax ensures that the control characters are recognized correctly in the context you want.
Summary
Always using double quotes for key sequences or special escape sequences in your mappings, dictionaries, and commands will help avoid parsing issues in Neovim or Vim, ensuring that your configurations work as intended.
If you have further questions or need assistance with configuring specifics, feel free to ask!
init.lua
For init.vim
augroup CompletionMenuClose
autocmd!
autocmd CompleteDone * :echo "Completion menu closed"
augroup END
In this example, whenever the completion menu closes, it will print "Completion menu closed" to the command line.
For init.lua
vim.cmd [[
augroup CompletionMenuClose
autocmd!
autocmd CompleteDone * echo "Completion menu closed"
augroup END
]]
2. Customizing the Message
You can replace the :echo "Completion menu closed" part with any command you like. Here is another example, where you might want to run a specific function when the completion menu closes:
function! OnCompletionClose()
" Your custom code goes here
echo "The completion menu has closed."
endfunction
augroup CompletionMenuClose
autocmd!
autocmd CompleteDone * call OnCompletionClose()
augroup END
Common Use Cases
A common use case for running commands on completion menu close might include:
Updating the status line.
Logging the user's last completed item.
Triggering additional actions based on the context of what was completed.
Notes
The CompleteDone event is triggered for various types of completion sources, including built-in completion (like keyword completion) or other plugins (like LSP).
Make sure there are no conflicts with other autocommands that could interfere with this behavior.
Using autocommands in this manner will give you flexibility and control to extend your Neovim or Vim workflow effectively when dealing with completion. If you have any further questions or need assistance with specific commands, feel free to ask!
CompleteDone
Here’s how you can do that:
Step 1: Define the Function
You need to create a Vimscript function that sets cmdheight=0. You can do this in your init.vim or in a Vim file loaded during startup.
For init.vim
function! SetCmdHeightZero()
set cmdheight=0
endfunction
Step 2: Create the Autocommand
Next, set up an autocommand that calls your function whenever the completion menu closes.
For init.vim
augroup CompletionSettings
autocmd!
autocmd CompleteDone * call SetCmdHeightZero()
augroup END
Complete init.vim Example
Putting it all together, your init.vim might look like this:
function! SetCmdHeightZero()
set cmdheight=0
endfunction
augroup CompletionSettings
autocmd!
autocmd CompleteDone * call SetCmdHeightZero()
augroup END
Explanation
Function SetCmdHeightZero: This function simply sets the command height to 0.
Autocommand: The autocommand group CompletionSettings ensures that the command is defined once and can be easily cleared or modified later. Whenever the completion menu closes (triggering the CompleteDone event), the function is called, and the command height is set to 0.
Verification
Once you’ve added this to your init.vim, make sure to restart Neovim or source your configuration file (:source $MYVIMRC) to apply the changes. Then, test it by opening the completion menu (e.g., using <C-n> or <C-p>) and closing it to see if the command height is set to 0 as expected.
Note
If you want to revert the command height back after typing or performing other actions, you may need to manage the cmdheight setting more dynamically based on your use case. For example, setting it to 1 when entering insert mode:
function! SetCmdHeightZero()
set cmdheight=0
endfunction
function! RestoreCmdHeight()
set cmdheight=1
endfunction
augroup CompletionSettings
autocmd!
autocmd CompleteDone * call SetCmdHeightZero()
autocmd InsertEnter * call RestoreCmdHeight()
augroup END
This will help manage command height settings more effectively while working with completion. Let me know if you have further questions or need additional assistance!
Using CompleteChanged
You can use CompleteChanged in conjunction with CompleteDone to handle both opening and closing of the completion menu. Here's how you can set an autocommand that reacts when the completion menu is opened:
Example: Adjust Command Height on Completion Menu Open and Close
function! SetCmdHeightZero()
set cmdheight=0
endfunction
function! RestoreCmdHeight()
set cmdheight=1
endfunction
augroup CompletionSettings
autocmd!
autocmd CompleteChanged * call SetCmdHeightZero() " Set to 0 when completion menu opens
autocmd CompleteDone * call RestoreCmdHeight() " Restore cmdheight when completion closes
augroup END
Explanation
CompleteChanged: This event is triggered before the completion menu is displayed or updated. By putting your set cmdheight=0 command here, you ensure that the command line height is adjusted when the user begins selecting completion options.
CompleteDone: As mentioned, this event triggers when the completion menu closes, at which point you can restore the command height.
Notes
Handling Edge Cases: Depending on how you're using this, you might want to check if cmdheight should be set to 0 or restored based on other conditions. You can adjust the logic in the SetCmdHeightZero and RestoreCmdHeight functions, as needed.
Testing: After implementing this setup, test it by triggering the completion menu and observing the changes to cmdheight to ensure that it works as intended.
Summary
While there isn't a dedicated built-in function that directly signals when the completion menu opens beyond CompleteChanged, using that event should serve your purpose well for managing UI behavior during completion. If you have further questions about this or other features, feel free to ask!