Is there a way to count the number of times a command is called during zsh startup? - autocomplete

I was reading through the answer to a related question on SO here and the answer mentioned:
Nope, that's not what your script should be doing. Not your script, but the user should call compinit in their .zshrc file to enable Zsh's completion system. Additionally, it should be called only once for each shell instance.
I'm curious if it is possible to count the number of times that compinit or bashcompinit is being called during zsh startup?
Thanks.

Yes, the zprof module makes this pretty easy: add zmodload zsh/zprof to the top of ~/.zshenv and run zprof once the shell starts up (maybe pipe to grep compinit too). It will list all the functions that were called since the module was loaded, with the number of calls and time they took. See its section in zshmodules(1) for more details.
~/.zshenv will be read immediately after /etc/zshenv, and before anything else. There shouldn't be any compinit calls in /etc/zshenv, since it's also read for non-interactive invocations of zsh like zsh -c ....

Related

fish shell + omf + git plugin: how to customize the prompt in the terminal

I have fish shell with omf with agnoster theme and git-plugin installed.
I would like to tune my prompt a bit. Does anyone here know where/how I do that. I ran fish_config; but that did not show my current prompt properly. So I am reluctant to go that route. I would rather do it by typing it in; but can't figure out where the final prompt is being stored. I tried 'echo $fish_prompt'. Did not help.
Would appreciate some help. Thanks!
fish_prompt is a function. See https://fishshell.com/docs/current/cmds/fish_prompt.html. To see where it is defined run functions --details fish_prompt. There is no "final prompt [is] being stored" as I understand that phrase. There is a function that creates the prompt. Your echo $fish_prompt would only output something useful if the prompt was a literal string (which isn't supported). You can use functions --all fish_prompt to see where it is defined and the content of the function.
When I used Fish I did not use OMF (I'm now an Elvish user). I had a custom function defined in ~/.config/fish/functions/fish_prompt.fish. So I can't explain how to customize the OMF "agnoster" theme prompt. You'll need to read the documentation for that theme to learn what knobs, if any, it provides for customizing its behavior.
Short answer: edit function fish_prompt in file: .local/share/omf/themes/agnoster/functions/fish_prompt.fish.
Explanation:
This exact file may not work in cases with different plugin's and/or different theme.
The way I figured this was to search for all "*prompt.fish" functions in my home directory. Put a distinct print statement with the prompt in each one of them and check which one got printed and modified the fish_prompt.fish function in that file - which is working for me!

How to stop Fish shell store critical information

I really like fish auto completion but I don't want to store any parameters in it, I know I can use --private or -P, but it only remove the history only when the terminal closed.
How to make sure fish doesn't save any temporary parameters? Or maybe you have any good alternative that work fine like fish but doesn't store anything?
I know I can use --private or -P, but it only remove the history only when the terminal closed.
What "--private" does is prevent the history from being stored on disk. It doesn't "remove" the history after exit as much as it's never stored anywhere but your RAM.
There is nothing to keep fish from keeping even interactive history on a global basis.
If you want fish to not remember any particular commandline, start it with a space:
> echo sad # is remembered
> echo happy # is not

How to execute a Sublime plugin command from the command line?

I use the plugin SimpleSession with Sublime Text 3 (but any plugin could be considered). If I save a session with multiple windows, this creates a .simplesession file. How can I open that session file just by clicking on the file? The goal is to avoid having to launch ST3 and use the Command Palette to run the "Load Session" command. Currently, clicking on the .simplesession file causes ST3 to open it as a regular file.
Sublime doesn't know that a simplesession file is important in any way, so double clicking on one is going to open it the same as Sublime would open any other file.
Since it's a plugin that created the file, that plugin is the only thing that knows that it's special and what to do with it. So what you really need is the way to tell the plugin to take the action for you.
All actions in Sublime (including things as simple as inserting text) are taken by executing a command. Here that would be a command in the plugin that created the file in question, which would tell it that you want to carry out the action you would normally take manually, such as loading a session.
To do that from within Sublime you'd do something like bind a keyboard key to the appropriate command, add it to a menu, the command palette, etc. If you want to take the action from outside of Sublime, then you need to communicate that command to Sublime in order to get it to execute.
In core Sublime you can do this by executing the subl program that ships with Sublime and tell it a plugin command that you would like to execute.
Although it's possible to do this, the solution provided here has the requirement that Sublime already be running due to technical limitations within Sublime itself, but more on that in a moment.
This answer will give you the information that you need to formulate the command line that you need to execute in order to get the plugin command to run and carry out the action that you desire.
If you want to run this command in response to double clicking a file of a particular type (here a simplesession file), how you do that is specific to the operating system and file browser that you're using, and is best asked as a separate question.
Assuming you instead want a level of integration where you just have a desktop shortcut, start menu entry, etc that does this, this is more straight forward because such a shortcut is really just a visual wrapper that executes a command of your choosing.
Again, how you would do that is different depending on your OS, but the important part is knowing what full command line you need to give to the shortcut to be able to run it, which is what this answer tells you how to construct.
Important Note: The specific package in your question implements a load_session command, which prompts you for the session to load from a list of sessions you've previously created.
This command doesn't take any argument that would tell it what session to load without asking you to pick one first. As a result, what you want isn't technically possible without more work because there's no way to directly tell the load_session command the file that you want to open.
In order to more fully automate things in this particular case, the underlying package needs to be modified. In particular either the load_session command would need an optional argument which, when given, would cause it to load that session without prompting first, or
a new command would need to be created to do the same thing.
If you're not comfortable or knowledgeable enough to make such modifications to the package directly, you need to either find someone that will do that for you or (even better) discuss it with the package author, since that is a feature that others would probably enjoy as well.
The first thing you need to know is, "What command in the plugin is the one that I need to execute to do what I want?". In some cases you may already know exactly what command you need to use because it's documented, or you have already made a custom key binding for it, and so on.
If you don't know the command you need to use, check the documentation on the package (if any) to see if it mentions them. In your particular case, the README on the package page specifically mentions a list of commands, of which load_session seems like the most appropriate fit.
Lacking any documentation, the next easiest thing to do would be to ask Sublime directly. To do this, select View > Show Console from the menu or press the keyboard shortcut associated with it, Ctrl+`. In the console that appears, enter the following command and press enter.
sublime.log_commands(True)
Now whenever you do anything, this console is going to show you exactly what command Sublime is executing, along with any arguments that it may be passing to the command. This remains in effect until you use the same command with False or restart Sublime.
With logging turned on, select the appropriate command from the command palette and see what the Console says.
For example, with this package installed, I get output like the following:
>>> sublime.log_commands(True)
command: show_overlay {"overlay": "command_palette"}
command: load_session
This is showing two commands; first I opened the command palette which uses the show_overlay command, and then I selected the SimpleSession: Load command, which is the load_session command with no arguments.
In order to get Sublime to execute the command from the command line, you use the --command command line argument to subl. So in order to get Sublime to run the load_session command, you can enter the following command in a command prompt/terminal in your OS. This is also the command you would set in your desktop shortcut.
subl --command "load_session"
This presumes that you've set up Sublime so that it's in the path (how you do that is OS specific). If running subl in a terminal gives you an error about a missing command, either add the Sublime install directory to the path or use a fully qualified file name in place of subl (e,g. "C:\Program Files\Sublime Text 3\subl" if you're on Windows); either requires you to know what location Sublime is installed in.
If you want to use a command that takes arguments you need to include the arguments in the command as well, in the same way as they were displayed in the console above.
It's important that the command name and the arguments all be considered one command line argument, which requires you to wrap the whole thing in quote characters, since otherwise the spaces will make it appear as multiple arguments.
If you forget this, Sublime will respond by opening files named after the different parts of the command and arguments that you tried to open under the mistaken belief that you're giving it files to open.
As a concrete example, to get Sublime to open the command palette from outside of Sublime, the command to do this would look like the following if you were on Linux/MacOS:
subl --command 'show_overlay {"overlay": "command_palette"}'
Note again that we are passing exactly what the console showed above, but the whole thing, command and arguments, are wrapped in single quotes so that the terminal knows that the entire value is one argument.
This makes things a little tricky on Windows, which doesn't allow single quotes. On that platform you need to use double quotes instead. This requires you to "quote" the internal double quotes with a leading \ character so that the command processor knows that they're part of the argument and not the double quote that ends the argument.
For the case of opening the command palette on Windows, the command thus looks like this:
subl --command "show_overlay {\"overlay\": \"command_palette\"}"
With this information in hand, you can set up something like a desktop shortcut to run the appropriate command, or potentially set up the file explorer that you're using to execute a command specifically when you double click on a file of your choosing.
Again, how you would do that is specific to the operating system that you're using, and so I'm not really covering that in depth here in this answer. Just keep in mind that regardless of the OS in question, the part that remains the same is that you need to use subl command like the above.
Now, in your particular case, if the package that you're using provided a command that would let it load the session directly without prompting you first, the command that you use would need to also include the name of the session file as one of the command arguments.
However, as I mentioned above, this package doesn't currently allow that at the moment.
Now, here is the GIANT CAVEAT with this whole thing; this only works if Sublime is already running.
The subl command talks to an existing running copy of Sublime and gives it commands to open a file, directory, run a command as we're doing here, and so on. If Sublime isn't already running, then subl will start Sublime first and then communicate these details to it.
Sublime starts and makes it's interface available to you to work right away, and then starts to load packages and plugins in the background. This is to get you in and working on your files without having to wait for all packages to load first.
An issue with this is that as soon as Sublime starts, subl passes off the appropriate commands and then quits, and since packages aren't loaded yet, the command that you want to execute doesn't exist yet (hasn't been loaded), so nothing actually happens.
Unfortunately there's not really a satisfactory way around this particular issue if you want to start Sublime and also execute commands.
A potential workaround would be use something like a script or batch file that would check to see if Sublime is already running, and if not Start it and delay a little bit to allow plugins to finish loading, then use subl to run the command.
However this would require you to basically guess how long it takes Sublime to finish loading, which is less than ideal.

Complete command from history in ipython

For reasons outside of my control I'm stuck using python 2.6.6 and IPython 0.10.2. I also normally use the tcsh shell, and have gotten quite used to completing a command from the history using <A-p> (i.e. pressing the ALT key and p). However, this doesn't work in IPython. I know I can press <C-r> and then start typing a command, but what inevitably is happening is that I start a command, press <A-p>, get a colon indicating some weird state, then exit out of that state, delete my command, press <C-r> then search for my command. It's getting rather irritating. Is there any way to make <A-p> complete my already started command by relying on the history?
Ouch, this is an old version of IPython, Python (and pip). The bad news is I don't have much experience with such an old version of IPython, the good new is; it was way simpler at that time.
Most of the shortcut and feature are provided using readline, and the python bindings of stdlib. Meaning that most likely what you are trying to configure is readline itself and not only IPython; so you can find more information on that outside of IPython !
The secret is to grep in the source-code for parse_and_bind, then you'll find the following example configuration, leading me to change the ~/.ipython/ipy_user_conf.py to be like so at around line 99 (all indented an extra 4 space to be in the main() function):
import readline
readline.parse_and_bind('set completion-query-items 1000')
readline.parse_and_bind('set page-completions no')
rlopts = """\
tab: complete
"\C-l": possible-completions
set show-all-if-ambiguous on
"\C-o": tab-insert
"\M-i": " "
"\M-o": "\d\d\d\d"
"\M-I": "\d\d\d\d"
"\C-r": reverse-search-history
"\C-s": forward-search-history
"\C-p": history-search-backward
"\C-n": history-search-forward
"\e[A": history-search-backward
"\e[B": history-search-forward
"\C-k": kill-line
"\C-u": unix-line-discard"""
for cmd in rlopts.split('\n'):
readline.parse_and_bind(cmd)
The repetition of commands make me think that what \C,\M or [e mean might be system dependant. I would bet on \C being Control, and \M being Meta (Alt, Opt), but at least one of these line did the trick for me (and also now tab allows to complete). See also man readline for the list of commands you can bind to what, and enjoy! Hoping you can upgrade to Python 3 and IPython 6 at some point.
[Edit]
See Eric Carlsen second comment under this answer for how it was resolved.

Running octave in Emacs

I am using run-octave in Emacs to trigger octave. Something is acting abnormally.
Every time I hit TAB to complete, there would be a tailing ^M; If I edit a .m file using edit a.m, it would start a new frame instead of a new buffer and the prompt is waiting for the closure of that frame so it would not respond to any input. How could I configure .emacs so that run-octave would behave normally?
Any comment is appreciated!
You seem to have two problems. I'm not sure about the trailing ^M, which seems to be caused by some sort of Windows/Unix CR/LF problem, but maybe I can help with the second problem.
The edit command uses the EDITOR environment variable to decide what to run. It seems that yours is either set to emacsclient or has defaulted to it. You haven't said whether you're on Unix or Windows, so I'm going to assume the former: you'll have to change this a bit for Windows.
To avoid the waiting thing, try running octave with a different EDITOR. For example, try out running
EDITOR='emacsclient -n' octave
When you type edit foo, it should bring up an Emacs buffer (if you want a new frame as well, use -c too) but not wait until you're done.
If this fixes things for you, you could change your ~/.bashrc to include the line
export EDITOR='emacsclient -n'